Keywords final, this, super, static

final: means final and immutable

Final can modify methods, attributes, and types. When modifying methods, methods of the parent class can only be called by subclasses and cannot be overridden. When modifying attributes, you need to assign values, or assign values ​​to attributes modified by final in the constructor. Because the construction method in the parent class must be executed, the final modified attribute can be assigned in the construction method. Once assigned, it cannot be changed. When the final modified class is not allowed to be inherited, because final means the final immutable, and inheritance means that extends is extendable, so it is not allowed to be inherited in the final modified class, in other classes The attributes are immutable! ! ! !

The attribute can be directly assigned while being declared or assigned in the constructor

Final temporary variables can be assigned at the same time as they are declared or assigned before the first use

Note: Once the final type variable is assigned, it is not allowed to modify, but if it is a complex type, it is not allowed to modify the address, but the attribute can be modified

The final method means that this method is not allowed to be redefined in the subclass (overwrite\rewrite)

The final class indicates that this class is not allowed to be inherited

this and super

this is used to refer to the current object

super is used to refer to the members defined in the parent class of the current object. Whether super can access depends on the scope qualifier

1. From a semantic point of view, this is used to refer to the current object; super is used to refer to the parent object

1. This() means to call another constructor, super() means to call a constructor in the parent class, and the parameters in () determine which constructor is called

2. This. member attribute is used to represent a member of the current object, and is generally used in scenarios where the names of local variables and attributes are consistent. The super. member attribute is used to represent a certain attribute defined in the parent class, and is generally used in the scenario where a certain parent class attribute is overridden in the subclass.

3. This. member method () is used to indicate a member method of the current object; super. member method () is used to indicate a member method defined in the parent class of the current class, and is generally used to override the definition [principle of proximity ].

4. Keywords such as this/super are not allowed in static methods

interface (interface): java class does not allow multiple inheritance and only allows single inheritance, but multiple inheritance is allowed in the interface, the class can also implement multiple interfaces when implementing the interface, the methods in the interface are abstract by default, so in the implementation The abstract method needs to be concreted in the interface. If the abstract method in the two interfaces of the class, the method name, parameter, and return value type are the same, only one method needs to be implemented in the class.

static

	用于修饰成员,包括成员属性【类属性或者静态属性】、成员方法【类方法或者静态方法】
  • As the class loads, as the class disappears
  • Take precedence over objects, use the class name to access directly

Modified by static, it can be called without the object. It will be executed automatically as the class is loaded. It takes precedence over the object and can be called directly by the class name

The static attribute is a common attribute shared by all objects of the current class (there is only one, and each object of the common attribute has its own, isolated from each other). Any object of the current class changes this attribute, and this attribute of all other objects will be affected.

Execution order: static attribute-attribute-constructor

Access method
can be accessed using "class name. static attribute name" or "object name. static attribute name"

Guess you like

Origin blog.csdn.net/qq_45874107/article/details/112691991