06-Java classes and objects

Object Oriented Foundation

Object Oriented Programming is centered on the object , distinguishing the internal organization of the object from the external environment, and isolating the internal attribute data that characterizes the object from the outside. Its behavior and attributes form a whole, while the system functions It is expressed as a sequence of interactions between a series of objects, which can simulate or express the real world more vividly.

1. Class and Object

  • Class: An abstract description of a class of things with the same characteristics.
  • Object: an individual, instance, concrete existence of a class.
  • Classes are design templates for objects

1.1 Class declaration

【修饰符】 class 类名{
    
    
    成员列表:属性、方法、构造器、代码块、内部类
}

1.2 Object creation

new 类名();  //匿名对象

类名 对象名 = new 类名(); //有名对象

Anonymous object:

Without defining the object, but directly calling the method of the object, such an object is called an anonymous object.

Usage:

  • If only one method call is required for an object, then anonymous objects can be used.

  • An anonymous object can be passed as an argument to a method call.

2. Class members: attributes

2.1 Attribute declaration

Declaration position : outside the method in the class

【修饰符】 class 类名{
    【修饰符】 数据类型  属性名;    //属性有默认值
    【修饰符】 数据类型  属性名 = 值; //属性有初始值
}

Note: The type of attribute can be any type of Java, including basic data types and reference data types (classes, interfaces, arrays, etc.)

2.2 attribute assignment

  1. Explicitly assign values ​​when declaring properties
【修饰符】 class 类名{
    【修饰符】 数据类型  属性名 = 值; //属性有初始值
}
  1. Assign value after object creation
【修饰符】 class 类名{
    
    
    【修饰符】 数据类型  属性名; //属性有默认值
}

//创建对象
类名 对象名 = new  类名();

//为对象的属性赋值
对象名.属性名 =;

2.3 Property access mechanism

  1. In this category

The methods in this class can directly access the member variables in this class.
Special : static members access non-static members, compilation fails

  1. Cross-class

If you want to access the members of other classes, you must pass "object. member"

After the user creates an object with the new operator, he can use the "object. member" to obtain the properties and behavior of the object.

2.4 Characteristics of attributes

  1. Properties have default values

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-4f3Ur0vI-1605863575082)(C:\Users\徐晓\Desktop\111.png)]

  1. The properties of each object are independent and do not interfere with each other

2.5 Memory graph of object properties

class Student{
    
    
    String name;
    char gender = '男';//显式赋值
}

class TestStudent{
    
    
    public static void main(String[] args){
    
    
        Student s1 = new Student();
        System.out.println("姓名:" + s1.name);//null
        System.out.println("性别:" + s1.gender);//男
        
        s1.name = "小薇";
        s1.gender = '女';
        System.out.println("姓名:" + s1.name);//小薇
        System.out.println("性别:" + s1.gender);//女
        
        Student s2 = new Student();
        System.out.println("姓名:" + s2.name);//null
        System.out.println("性别:" + s2.gender);//男
    }
}

Insert picture description here

 

3. Method parameter passing mechanism

Method parameter passing mechanism in Java: value passing

  1. When the formal parameter is a basic data type, the actual parameter passes the data value to the formal parameter in the form of copy, and the modification of the value by the formal parameter does not affect the actual parameter.
  2. When the formal parameter is a reference data type, the actual parameter passes the address value to the formal parameter, and the modification of the attribute of the object by the formal parameter will affect the attribute value of the actual parameter object, because the formal parameter and the actual parameter point to the same object.

4.this keyword

1. This keyword:

Meaning: current object

(1) If it appears in the constructor: the object being created

(2) If it appears in a member method: the object that is calling this method

2. The usage of this:

(1) this. attribute

When the local variable and the member variable have the same name, you can add "this." in front of the member variable to distinguish

If you don't use this to distinguish, follow the principle of proximity.

(2) this. method

Call the member method of the current object, you can omit "this."

5. The difference between instance variables and local variables

  • The position of declaration is different: instance variables are outside methods in the class, local variables are inside methods or code blocks
  • Data storage locations are different: instance variables are in the heap, local variables are in the stack
  • The initial value is different: instance variables have default values, and local variables must be initialized manually
  • Modifiers: instance variables can have multiple modifiers, and local variables can only use final at most
  • Scope: instance variables (accessed directly in this class, other classes are accessed through objects), local variables start from the declaration and end when they belong
  • Life cycle: Instance variables are born with the creation of objects, and the objects are recovered by the garbage collection mechanism. A local variable is when the code execution exceeds the scope of the scope, the local variable becomes invalid.

Guess you like

Origin blog.csdn.net/m0_46988935/article/details/109855402
Recommended