Object-Oriented Programming (2)

(6) Abstract, abstract is basically nothing, just pay attention to four points:

One: Abstract classes are meant to derive subclasses, and all interfaces have abstract properties.

Two: abstract classes can declare instances, but cannot create instances.

Three: only abstract classes, abstract functions, no abstract members.

Four: Abstract classes do not necessarily need to have abstract functions, but they still cannot be instantiated. A class with abstract functions must be an abstract class. A subclass of an abstract class must override all abstract methods of the parent class , otherwise the subclass is still an abstract class.

例:abstract public shape

{int A;

abstract public int getArea();}

(7) Static static, classes generally do not have static attributes (except inner classes), generally static fields and static methods.

Each instance member has a set of independent member fields without static attributes, and static members generally belong to classes, but any instance object expression or class name can be used to access static member methods or fields.

Example: Shape s1=new Shape(); Shape s2=new Shape(); Then s1.number==s2.number==Shape.number, where number is a static member field in the Shape class, and so is the static member method .

Non-static member methods access non-static member fields or member methods of this class or parent class, call the name directly or modify it with this (normally, note that when the subclass does not have a member field with the same name as the parent class, both this and super can call the parent class member name).

When a non-static member method accesses a static member method or member field of this class or parent class, use the name directly or use the class name.member field (or method, this cannot be used).

When a static member method accesses a static member field or member method of this class or its parent class, it uses the name directly or uses the class name. Member field (or method).

Generally speaking, static member methods can only access static member fields and static member methods, but if you want to access non-static member methods of a certain class, you can access them by creating an instance object of that class (for example, main accesses member methods of other classes).

 

Static block: that is, a program block in the form of static{//} in a class will run only once when the class is loaded (because a class will only be loaded once), it can be regarded as a block without a name and no return Value, no parameters, and three non-static methods that do not need to be called. It has the characteristics of ordinary static methods: 1. Non-static variables or non-static member methods cannot be called. 2 Cannot use the this and super keywords, because this belongs to the class object. 3 Static variables cannot be declared in it. In fact, ordinary member methods cannot declare static variables, because static variables belong to classes and can only be declared in the class body. 4 Keywords such as public, private, protected, etc. cannot be used, only the default can be used. The same other methods cannot declare variables with access attributes, but can only be declared in the class body.

About the timing of class loading: (1) Call the Class.forName() method. (2) Instantiate an object or a subclass object. (3) Call the static member field. (4) Call the static member method. There are some things to pay attention to without loading the class. If the static variable called is of final type, then the class does not need to be loaded, and the static statement block will not be executed. If you just create a reference, you don't need to load the class. If the subclass calls the static method of the parent class, and whether the subclass overrides the parent class method, the subclass will not be loaded , only the parent class will be loaded, otherwise it will be loaded.

The execution order of multiple static blocks, including and static member fields, are executed in order, and take precedence over other call statements including constructors.

 

(8) Final is used to modify non-abstract classes, non-abstract member methods of classes, member fields of interfaces, and member fields of classes, and cannot modify all interface member methods, abstract member methods of classes, constructors, abstract classes, interface.

If used to decorate a non-abstract class, the class cannot be inherited.

If a non-abstract member method of a modified class is not overridden, the class cannot be overridden. If a member method with the same name as the parent class is declared in a subclass, a compilation error will occur.

If the member fields of an interface or class are modified, and there are static properties at the same time, they can only be assigned at the time of definition, and cannot be modified afterwards, becoming constants. If there is only a final type and no static type, then it can be defined at the time of definition or in the constructor. Assignment.

(9) Interface interface, because Java does not allow inheriting multiple parent classes, but can implement multiple interfaces.

Inteface can have public and default modes. If it is public mode, the file name is the same as the interface name (rules of the same type), and can be used by all software packages (different packages are imported with import, the same type). If it is the default mode, it can only be used by this package. The interface generally does not have private and protected attributes, which is the same as the class.

In addition, interfaces generally have abstract properties, so adding abstract is basically useless, so interfaces can also declare objects but not create objects - the same as abstract classes.

An interface can inherit other interfaces, and also become a parent interface and a child interface. The difference is that a subinterface can inherit multiple parent interfaces, while a subclass can only inherit one parent class, and a class can also implement multiple interfaces. E.g:

class A extends B implements C,D,E{}     interface F extends G,H,I{}

If class A is not an abstract method, it must implement all abstract methods in C, D, and E, but if there is a function with the same name and parameter, only one can be implemented. If you implement the interface of F, you need to implement the abstract methods of all its parent interfaces, that is to say, this class must implement all the abstract methods of all interfaces of implements and all of its parent interfaces. However, if A is declared as an abstract class, only part of it can be implemented, but the subclass must implement all the remaining methods, otherwise it must still be declared as an abstract class.

If there is a parent-child relationship in the interface implemented by the class, it is equivalent to implementing the lowest-level sub-interface. If the parent class (which can be an abstract class) implements the interface, it is meaningless for the sub-class to implement this interface or its parent interface, but it will not If an error is reported, the subclass and the parent class work together to implement the interface and its parent interface.

All member fields of an interface have public, static, and final attributes, and all member methods of an interface have public and abstract attributes, regardless of whether you add the above modifiers or not. You can use the class name, interface name and instance object name when accessing the member field. Because of the static and final attributes, the member field must be initialized and assigned and cannot be changed. The class name refers to the name of all classes that implement this interface or their subclasses. The interface name refers to the name of the interface defining the member domain and its sub-interface, and the instance object name refers to the instance object name of the above interface and class (even abstract classes and interfaces can be declared). Member methods can only be called using the instance object name because they are not static methods.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324898015&siteId=291194637