Object-oriented, constructor, static, final, this statement summary

\\The purpose of software to describe the real world
with computing language;
use computer to solve real world problems;

object-oriented thinking >--description--> object-oriented world;

\\the benefits of object-oriented design and development of programs;
communication Smoother;
improve design and development efficiency;
abstract "class" according to "object";

//construction method
When a class is instantiated (object creation), the system will assign a no-argument constructor by default;
role: assign member properties by default Initial value;
the constructor has no return value. (including void) If there is, it is not a constructor.

Syntax:
          Note: The method name has the same name as the class name.
Access modifier constructor name () {
    //initialization code
}

Method overloading:
there is a parameter constructor, which is a parameterless constructor On the basis of the method, multiple parameters are added in parentheses. These multiple parameters are called local variables in the method;
if the method name is the same, the parameter items in the method can be different (different numbers, different types, different orders), called Method overloading; (not related to access modifiers, return values)
     Note that it is used in a class.
If you customize a parameterized constructor, the system no longer provides a default parameterless constructor, and you must manually add the definition of the parameterless constructor;
the difference between overloading and overriding:
overloading involves a method with the same name in the same class (must In the same class), the method name is required to be the same, the parameter list is different, and it has nothing to do with the return value and access modifier;
When rewriting involves a method with the same name between the parent class and the child class, the method name is required to be the same, the parameter list is the same, the return type is the same, and the access modifier cannot be greater than the parent class;



syntax: (as shown in Figure 1)
          Note: method name The same name as the class name
Access modifier Constructor method name (parameter) {
    //initialization code
}
Figure 1:


this statement:
when the parameter variable and member variable have the same name, you must use this to specify the member variable;
refer to the object in this class, this .Variable name (in order to distinguish member variables and local variables)



static statement:
static can modify methods or properties;
static method name (); class method;
static type property name; class variable;
static variables cannot be defined within a method.
Non-static cannot call static methods;

there are two ways to call static:
1. Instantiate an object with object.method(); or object.attribute;
2. Directly use class name.method(); or class name.attribute;



Note:
It is not allowed to use this and super statements at the same time in a constructor to call the constructor (otherwise only the first of the two will be executed (that is, the first one will be executed));
in a class method (static modified method) The this or super keyword

final statement is not allowed:
constants in java are decorated with the final keyword, and the constant name is usually uppercase;
Constants will not change during the running of the program, they can only be referenced, and cannot be reassigned, that is, their values ​​can only be modified when they are defined;
final can only modify property methods and classes cannot modify constructors;

syntax:
final type attribute name ;

Guess you like

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