abstract
Notes on the use of abstract:
1.abstract cannot be used to modify structures such as attributes and constructors
If the constructor is abstract, subclasses must be overridden, but the constructor cannot be overridden and can only be overloaded.
2.abstract Cannot be used to modify private methods, static methods final Methods final Class of ① abstract Cannot be used to modify private methods: because abstract The method of modification must be rewritten, but private After modifying a method, it is not considered that this method can be overridden. Subclass can't see this method and can't override it. ②: Declare as static The method of is not considered to be rewritten. See the rewriting chapter for details. ③: final:Final version: modified methods cannot be overridden. ④: final Decorated classes cannot be inherited. But I did abstract Your class must want to be inherited, Because if you do not inherit, the abstract class itself cannot create objects, and the class will be obsolete.
Code block
Class: code block (or initialization block)
-
Function of code block: used to initialize classes and objects
-
If the code block is decorated, only static can be used
-
Classification: static code block vs non static code block
-
Static code block
There can be output statements inside
Executes as the class loads, and only once
Function: initialize class information
If multiple static code blocks are defined in a class, they are executed in the order of declaration
The execution of static code blocks takes precedence over the execution of non static code blocks
Static code blocks can only call static attributes and static methods, not non static structures
- Non static code block
There can be output statements inside
Executes as the object is created
Each time an object is created, a non static block of code is executed
Function: you can initialize the properties of the object when creating the object
If multiple non static code blocks are defined in a class, they will be executed in the order of declaration * > static attributes, static methods, or non static attributes and non static methods can be called in non static code blocks
The above two functions are used in the development of code blocks. This is equivalent to one more way to assign values to attributes.
Where attributes can be assigned:
① Default initialization
② Explicit initialization
③ Initialization in constructor
④ After you have an object, you can assign values through "object. Attribute" or "object. Method"
⑤ Assignment in code block
①The code block has no name and cannot be called actively. It is executed with class loading or object creation ②because"Each time an object is created, a non static block of code is executed",So let's think: can non static code blocks do something for us
final
Final keyword: final, final
1. final can be used to modify structures: classes, methods, and variables
2. final Used to decorate a class:This class cannot be inherited by other classes. For example: String Class, system,StringBuffer class
3.final is used to modify a method: indicating that the secondary method cannot be overridden.
-
final is used to modify variables: at this time, the "variable" is called a constant
4.1 final modification attribute: the positions where assignment can be considered are: explicit initialization, initialization in code block and initialization in constructor.Special note: assignment with constructor: assignment must be made in each constructor, because it is not sure which constructor to use to create the object. If there is no assignment in a constructor and the constructor is called to create an object, the property is not initialized. 4.2 final Modify local variables: Especially use final When you modify a parameter, it indicates that the parameter is a constant. When we call this method, we assign an argument to the constant formal parameter. Once assigned Later, this parameter can only be used in the method body, but it cannot be re assigned.
static final It is used to modify attributes, which means that they are loaded with the loading of classes and cannot be changed into global constants
static
Use of static keyword
1.static: static
2.static can be used to modify attributes, methods, code blocks and internal classes
3. Use static to modify attributes: static variables (or class variables)
(static cannot be used to modify local variables)
3.1 attribute: according to whether static modification is used, it can be divided into static attribute (class variable: classified all) and non static attribute (instance variable: owned by a specific object)
Instance variable: we have created multiple objects of the class, and each object independently has a set of non static properties in the class. When modifying one of the objects
Non static attributes do not result in the modification of the same attribute value in other objects.
Static variable: we create multiple objects of the class, and multiple objects share the same static variable. When a static variable is modified through an object, it will cause
When other objects call this static variable, it is modified.
Instance variable: loaded as the object is created. Class variable: loaded with class loading. Classes are loaded earlier than objects: because constructors are used to create objects, classes must be loaded into memory before constructors are used. 3.2 static Additional description of modified attributes: ① Static variables are loaded as the class is loaded. Can pass"class.Static variable"Called in the same way ② Static variables are loaded before objects are created. ③ Since the class will only be loaded once, only one copy of the static variable will exist in memory: in the static field of the method area. ④ Class variable Instance variable class yes no object yes yes 3.3 Examples of static attributes: System.out; Math.PI; (out PI All static Modified structure) 4.use static Modification method: static method ① Load as the class is loaded. You can use the"class.Static method"Called in the same way ② Static method(Class method) Non static method(Example method) class yes no object yes yes ③ In static methods, only static methods or properties can be called In non static methods, you can call either non static methods or properties or static methods or properties 5. static Note: 5.1 Cannot be used within a static method this Keywords super keyword 5.2 We all understand the use of static attributes and static methods from the perspective of life cycle. (Those born late can be adjusted to those born early. Those born early cannot be adjusted to those born late) 6. ① In development, how to determine whether a property should be declared as static of You can declare a property as static > Attributes can be shared by multiple objects and will not vary with different objects. > Constants in classes are also often declared as static ② In development, how to determine whether a method should be declared as static of > The method of manipulating static properties, usually set to static of > Methods in tool classes are traditionally declared as static of For example: Math,Arrays,Collections (Because in this way, there is no need to create an object, and it is not declared as static,These methods cannot be called until the object is created)