Object-Oriented Programming (1)

(1) Foundation: There is at most one public class in a Java file, and the file name must be the same as the public class name. If there is no public class, the file name can be the same as any class name.

Garbage collection mechanism: If an instance object does not have any reference points, the memory it occupies will become garbage, but the JVM generally does not recycle it immediately, and will recycle garbage at a suitable time. Guaranteed to be recycled first. If you want to manually recycle, you can use the System.gc() method, which will call the finalize() method of the class, but the system will not call it after the program has finished recycling.

(2) Inheritance: one of the three major characteristics, a class inherits at most one direct parent class.

super() and this() methods: super() is called in the constructor of the subclass to initialize the class members and must be written in the first sentence of the constructor. If it does not contain any parameters, it can be omitted. It is worth noting that if the parent class has no constructor, a default no-parameter constructor will be created; if the parent class only has a parameter constructor, the default constructor will not be generated, then the subclass must explicitly call super and must contain parameters;

  Implicit call to /super() without arguments super() has parameters
parameterless constructor / no constructor correct mistake
parameterized constructor mistake correct
have correct correct

The this() method also appears in the constructor and must be written on the first line, so it cannot be used at the same time with super() to call other overloaded constructors.

Upcasting implicit call: when the subclass object is copied to the superclass object, it becomes upcasting. For example, the subclass Square inherits the superclass Shape, and the reference of the superclass can be pointed to the subclass object, for example: Shape sh=new Square();

Downcasting explicit call: Following the above statement, Square sq=(Square)sh; is called downcasting (dwoncasting), but this sentence cannot be used directly, because only sh can be downcasted when it points to a subclass object, otherwise the compilation will not error, but running will fail.

instanceof: Determines whether the object is an instance of a certain class. For no transformation, the subclass is an instance of all superclasses and itself, and returns true, such as Square sq=new Square(); then sq instanceof Square, sq instanceof Shape and even sq instanceof Object returns true; and the parent class will not be an instance of the subclass, such as Shape sh=new Shape(); sh instanceof Shape returns true, but sh instanceof Square returns false;

For upcasting, the parent class is the object of the subclass, for example Shape sh=new Square(); then sh instanceof Square returns true; or Square sq=new Square(); Shape sh=sq; then sh instanceof Square, sh instanceof Shape, sq instanceof Square, sq instanceof Shape all return true;

For downcasting, since the parent class is definitely upcasting, it is not discussed, and the subclass is the same as without conversion.

(3) Polymorphism: one of the three major characteristics of object-oriented, divided into static polymorphism, dynamic polymorphism (ie static binding and dynamic binding in C++)

Static polymorphism: overloading, a method with the same name but different parameters, such as individual constructors. Note that the same parameter type but different parameter names are not overloaded, they are still the same function.

Dynamic polymorphism: that is, override (override), which means that the parent class and subclass method parameter types and method names are exactly the same, but the JVM will identify which call it is. The subclass object calls the subclass method, the parent class object calls the parent class method, the parent class of the upward transformation calls the subclass method, and the subclass of the downward transformation also calls the subclass method.

super and this keywords: Note that it is different from the previous super() and this() methods. super can be used in non-static member methods in subclasses to access members of the supertype. The usage of this is the same as super, both are super. a, this.b, super.A(), this.B() form, but to call members of this class, it is mainly used to distinguish local variables and member fields (with the same name), for example, in many set methods, there will be this .a=a similar statement.

Also static methods do not have dynamic polymorphism.

(4) Package: The package keyword package must be the first sentence of the file;

There are three types of import package statements:

import wym.*; Import the interfaces, classes, and enumerations in the entire wym package. Types of subpackages are not imported.

import wym.Eat; Import the Eat type (one of interface, class, enumeration) in the wym package.

import static wym.Eat.main; import the main static method.

The first one will increase program overhead and reduce compilation efficiency, so try to use the latter two.

(5) Package: one of the three major characteristics

Class access control methods: public and default (that is, no modifiers are added), public classes can be used by all packages (different packages need to be imported with import, and visible in the same package), and default classes can only be used in packages use (visible in the same package).

Class member access control methods: four types of public, protected, default, private.

For a public class member, all type definitions that can access the class can access the member.

For protected class members, if the class is in the default mode, members of other packages cannot be accessed. If the class is public, more subclass methods of other packages can access it than default.

For default class members, the same package can access, but different packages must not access.

For private class members, only member methods of this class can be accessed.

Permissions when the class is public:

access control mode within the same class In the same package Subclass All classes
public allow allow allow allow
Protect allow allow allow  
default allow allow    
private allow      

Permissions when the class is the default:

 

access control mode within the same class In the same package Subclass All classes
public allow allow    
Protect allow allow    
default allow allow    
private allow      

Guess you like

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