The first modifier abstract? ( Abstract )
one ,abstract Can decorate classes
(1) cover abstract The modified class is called an abstract class (2) grammar : abstract class Class name {} (3) characteristic : Abstract classes cannot create objects alone , But you can declare references
Abstract class name Reference name ; (4) Abstract classes can define member variables and member methods (5) Abstract classes have construction methods , When used to create subclass objects ,jvm Create a parent object by default ;
Abstract construction methods are applied in jvm Apply when creating a parent object .
two ,abstract Can modify method
(1) cover asbtract Decorated methods are called abstract methods (2) grammar : Access modifier abstract return type Method name ( parameter list );
be careful :abstract And access modifiers have no sequence requirements (3) characteristic : Abstract methods have only declarative parts , There is no implementation part of the method ( even {} none , with ; ending ) (4)
be careful : Abstract methods can only be defined in abstract classes ; However, abstract methods and non abstract methods can be defined in abstract classes
Subclasses of abstract classes :
(1) grammar : class Subclass class name extends Abstract class name {} (2)
requirement : If subclasses do not want to become abstract classes , You must override all abstract methods in the parent class of the abstract class ( objective : Add the abstract method implementation part );
If the subclass does not override all abstract methods in the parent class , Must be defined as an abstract class , Cannot create objects at the same time (3) application : Abstract classes embody the application of polymorphism Abstract class name Reference name = new Subclass class name ();
// The reference of the parent type stores objects of the child type
The second modifier static? ( Static ) — ( This is very important ~)
one ,static Can decorate attributes
(1) cover static Decorated attributes are called static attributes , Static variable , Class variable be careful : Member variables are divided into : Instance variables and static variables ( Or static attribute , Class variable ) (2)
position : Defined within class , Beyond method , cover static modification (3) grammar : Access modifier static data type Variable name ; Access modifier static data type Variable name = value ;
be careful : Access modifiers and static There is no sequence requirement between , But it must be in front of the data type (4) characteristic : Static attributes exist based on classes , It doesn't matter how many objects are created , Shared by all objects
(5) use : a. adopt Object name . Static attribute name b. Directly through Class name . Static attribute name ——> proposal be careful : Instance variables must pass Object name . Instance variable name Visit
two ,static Can modify method
(1) cover static Decorated methods are called static methods (2) grammar : Access modifier static return type Method name ( parameter list ){ // Method implementation , Method body }
be careful : Access modifiers and static There is no sequence requirement between (3) use : a. Directly through Class name . Static method name ( Argument ); --》 proposal b.
By object name . Static method ( Argument ); --> Not recommended (4) Static method syntax details : a. Only static members of this class can be accessed in static methods ( Static properties and static methods ) b.
Non static members of this class cannot be accessed directly in static methods ( Instance variable + Non static method ) c. Cannot in static method this/super keyword d. Static methods can be inherited by subclasses e.
Static methods can only be overridden by static methods , Static methods do not reflect the application of polymorphism (5) Static method application scenarios : Methods in tool classes are usually set as static methods , For the convenience of users
three ,static Can decorate initialization code block
(1) cover static Decorated initialization code block is called static code block (2) Location of static code blocks : Defined within class , Method thinks , cover static Embellished {} class Class name {
static{ // Static code block } } (3) effect : When the class is loaded , Complete the initialization of static attributes in the order of and static attribute definitions (4) Class loading : a.
concept :jvm When using a class for the first time , adopt classPath Find the corresponding .class file ; And yes .class File to read this kind of information ;
( Package name , Class name , Parent class , attribute , Construction method , Member method, etc ); Save read information to jvm In memory , A class is loaded only once . b. Timing of class loading :( What is it jvm Use a class for the first time )
I. The first to access the static member of this class ( Static properties and static methods ) II. Create this kind of object for the first time : Complete class loading first ; Then complete the creation of the object III.
Subclass class loading , Cause its parent class to load first : Add to parent class first , Reload subclasses ① Call subclass static property or static method for the first time ② Create subclass objects for the first time : First line class loading , Then complete the creation of the object
load : Finish loading the parent class first , Then complete the class loading of subclasses create object : Complete the creation of the parent object first , Creation of subclass objects
The third modifier final? ( Final )
one ,final Variables can be modified ( local variable , Member variables —> Instance variables and static variables )
(1) characteristic : cover final Decorated variable , Constant in scope , Only one assignment is allowed , It can be used more often be careful :final Decorated variable once assigned , Cannot modify (2) grammar : Access modifier
final data type Variable name = value ; (3) final Decorated instances no longer have default values , The opportunities for developers to assign values to them are as follows : a. Initialize it when defining , assignment final
int a = 3; b. Use the construction method to complete the assignment class A{ final int a ; public A(int a){ this.a=a; } }
(4) final Decorated static variables no longer have default values , The opportunities for developers to assign values to them are as follows : a. Initialize it at definition time , assignment b. Initialize it with static code blocks class A{
final static int n; static{ n = 5; } } (5) final Decorated references , The storage object in the reference cannot be changed
two ,final Can modify method
Can be inherited by subclasses , But subclass override is not allowed .
three ,final Can decorate attributes
cover final Decorated classes cannot be inherited , That is, there are no subclasses .
Technology