What is object-oriented ?
All objective things are objects .( Everything is object )
Composition of objects :
(1) Properties of the object : What are the external characteristics of the representative object , characteristic Attribute belongs to static part of object ; Using variables to store attribute information in actual development ( attribute -> variable ) (2)
Method of object : What functions does the representative object have , What can I do Method belongs to the dynamic part of object ; Utilization method in actual development ( function ) express
Relationship between classes and objects : A class can create multiple objects ( example ).
Classes are templates for objects .
Objects are instances of classes .
Components of classes :
class Class name { // mobile phone ( Class name =Phone) // 1. attribute : Describe object characteristics String name; // Name of mobile phone String color;
// Color of mobile phone // 2. method : Describe object functions , Can what public void run(){ System.out.println(" phone "); } }
Properties of class :
(1) attribute : Also known as Member variables (2) position : Defined within class , Beyond method (3) grammar : data type Attribute name ; data type Attribute name = value ;
be careful : The declaration and assignment must be completed together . Wrong writing : class Student{ double score; score = 60.0; // Compilation error }
(4) be careful : Actual development , Number of attributes ( What attributes are required ) Depends on the needs of the project (5) Member variables have default values : The system automatically assigns a value of the corresponding type according to the data type of the attribute integer :0
decimal :0.0 Boolean :false character : Empty character \u0000 object :null (6) Scope of action : At least valid in this category
be careful : In the member method of this class, you can directly pass the attribute name / Member variable name usage ; If you access properties in a class outside the class, you need to pass Object name . Attribute name / object . Member variables
be careful : Attributes can be defined above methods , Below the definition method (7) Attribute naming conflict : Attributes with the same name are not allowed in the same class ; However, member variables can be named the same as local variables ;
Same name , When accessing a variable with the same name in a method that defines a local variable , Local variables are used preferentially .
Differences between local variables and member variables ? ( Can answer 4 Just click )
Method of class :
(1) method : Called member method (2) position : Defined within class , Other methods (3) grammar : class Class name { // attribute // Member method public
return type Method name ( parameter list ){ // Implementation of method , Method body } } be careful : Method is no longer written static, Distinguish between subsequent blog explanations (4)
Methods are defined as : Method declaration and method implementation a. Declaration of method : What can the representative do Modifier return type Method name ( parameter list ) abnormal
be careful : The modifier of a method can have multiple , There is no position order requirement ; The method name needs to be literal , Adopt hump nomenclature ; Abnormal follow-up blog explanation b.
Implementation of method : How to realize the declared function of the representative object , How to do it specifically {}
Method overloading : (overload)
(1) understand : Define multiple methods with the same name in a class , But the formal parameter list is different (2) Requirements for method overloading : a. Same method name b.
The formal parameter list is different ( Number of parameters , order , Different data types ) c. Method modifier , return type , Abnormal no requirements (3) be careful : If only the parameter names are different , It does not constitute a parameter list public
void m1(int n){} public void m1(int m){} // Compilation error (4) use : Object name . Method name ( Argument );
be careful : Actual compile time , Match the corresponding method according to the actual parameters passed by the caller ; If the method corresponding to the formal parameter type is not found for the given actual parameter , Then the nearest up match ; But there should be no matching confusion . (5)
Application scenario : In an object, the same function has different implementation processes due to different parameters ; To facilitate developers to flexibly use the same function , Usually set to a method name , Give different parameters
Constructor in class :
(1) Construction method : Is a special method , Also known as constructor (construtor) (2) position : Defined within class , Other methods (3) Grammatical features of construction methods : a.
The method name of the constructor must be consistent with the class name b. Constructor has no return value type ( even void none ) Modifier Class name (){} c.
Constructor allows method overloading ( Multiple construction methods can be defined in a class ) be careful : At most one parameterless constructor can be defined in a class ; However, multiple structures with parameters can be defined at the same time
Manufacturing method ( The parameter list is different ) d. Constructor cannot be called manually , out of commission Object name . Formal application e.
Construction method when creating objects ,jvm Decide which construction method to use to complete the creation of objects according to the actual parameters passed f. An object can only use the constructor once when creating an object
be careful : A construction method can only be used once for an object ( When created ); However, a constructor can create multiple objects (4) Function of construction method : a. Used to create objects : b.
Assign values to attributes with the help of parameterized construction methods (5) be careful : a. If no constructor is defined in a class , be jvm Automatically provide a public , Parameterless construction method b.
If any constructor with parameters is provided in the class , be jvm No longer provide default Expose parameterless construction methods (6) Summary of actual development of construction method : a. In actual development, a class usually provides
2 Construction methods : I. A parameterless construction method II. Construction method with parameters : Number of parameters , order , Data type depends on attribute b. Parameterless construction is provided to facilitate the preparation of later framework development
How to create objects :
1. grammar : Class name Object name =new Class name (); ( Take students as an example :)// Student s = new Student(); 2. Use object : (1)
Attribute assignment : Object name . Attribute name = value ; be careful : The value type assigned depends on the data type of the attribute (2) Access to properties : Object name . Attribute name (3) Call the method of the object : Object name . Method name ( Argument );
be careful : If the called method has a return value , Pay attention to the processing of return value
this Application of :
this.
(1) this : Represents the current object ,this. It can be used to call the properties of the current object and the member methods of the current object (2) this. Attribute name Call the properties of the current object
this. Method name ( Argument ); Call the member method of the current object (3) this. It can be applied to construction methods or member methods in this class , Access current object properties and member methods (4)
be careful : During actual development this. Usually, it can be omitted , But when the names of member variables and local variables conflict , use this. Distinguish member variables class A{ int n= 2; //
Member variables public void test(){ int n = 6; System.out.println(n);//
Name conflicts between member variables and local variables. Local variables take precedence System.out.println(this.n); // Member variables ,this. The accessed must be member variables } }
this()
(1) this() It can only be used in the constructor of this class , On behalf of calling other construction methods of this class (2) this(): Represents calling the parameterless constructor of this class
this( Argument ): Represents calling the constructor with parameters of this class (3) be careful :this()/this( Argument ) Must be applied in a valid line of the constructor ; and
this()/this( Argument ) Not the default addition , Developers need to add it where there are corresponding requirements (4)
stay this()/this( Argument ) When applied , The first line of every constructor cannot appear this() Application of , Avoid loopback calls
quote : Variables of object type are called references .
Class name Reference name = new Class name (); Type of reference Type of object be careful :Java Is a strongly typed programming language , The data of the corresponding type is stored in the variable of the corresponding type ;
The referenced data type needs to be followed by new The object types of are consistent Student studnet=new Student
null Represents empty address , Does not point to any object in heap space .
During actual development , Usually available null Initialize for reference .
Student student = null; // initialization
If the content stored in the reference is null, When using references to access properties or member methods in an object , Compilation passed ; But the operation reports an error , The error message is :
Java.lang.NullPointerException( Null pointer exception )
Technology
Daily Recommendation