Object-Oriented Programming: From Creating Classes to Exploring Encapsulation and Construction Methods

insert image description here

1. How does the code create the class?

In object-oriented programming, a class is an abstraction of a class of things, including static properties (member variables) and dynamic behaviors (member methods). In Java, the format for creating a class is as follows:

修饰词 class 类名 {
    
    
    // 属性信息
    // 行为信息
}

public class Student {
    
    
    // 属性信息
    String name;
    int age;
    String gender;

    // 行为信息(方法)
    public void eat() {
    
    
        System.out.println("eat()......");
    }

    public void sleep() {
    
    
        System.out.println("sleep()......");
    }
}

2. Create objects using classes

After creating a class, we can use the class to create an object, and access the members of the class through the object. The syntax for creating an object is as follows:

类名 引用名称 = new 类名([参数]);

3. Access the members of the class through the object

Through the object, we can access the properties and methods in the class. Accessing properties requires the use of the dot operator (.) and assigning values ​​to properties. Access methods also use the dot operator, but with parentheses.

// 访问属性并对其赋值
Student student = new Student();
student.name = "张三";
student.age = 18;

// 访问方法
student.eat();
student.sleep();

4. Packaging

In object-oriented programming, encapsulation is an important concept. It does this by making class member variables private and providing public get/set methods. In this way, the specific implementation of the attribute can be hidden, and the operation interface of the attribute can be exposed at the same time, making the use of the class more flexible and safe.

public class Person {
    
    
    private String name;
    private String cardId;
    private String tel;
    private int money;

    public String getName() {
    
    
        return name;
    }

    public void setName(String str) {
    
    
        name = str;
    }
}

5. this keyword

In a class method, use the this keyword to distinguish member variables from local variables. Member variables are located inside the class and outside the method, while local variables are located inside the method. this. Member variables are used to represent member variables, helping us to clearly identify which variable is being used.

6. Construction method

A constructor is a special method that initializes an object while it is being created. Its format is:

public 类名() {
    
    

}

Java provides a no-argument construction method by default. If a construction method is explicitly defined, the default no-argument construction method is no longer provided. The construction method with parameters and the construction method without parameters can be flexibly added according to the needs. The construction method with parameters is usually used to determine the value of attribute information, while the construction method without parameters does not determine the value of attribute information.

public class Person {
    
    
    private String name;
    private int age;

    // 无参构造方法
    public Person() {
    
    

    }

    // 有参构造方法
    public Person(String name, int age) {
    
    
        this.name = name;
        this.age = age;
    }
}

7. Information in the class

A standard Java class needs to contain member variables, constructors, get/set methods and other business methods. Member variables need to be private and accessed through get/set methods. At least two construction methods need to be provided: a no-argument construction method and a full-parameter construction method to meet different needs. At the same time, other methods can be added flexibly according to business needs.

By learning these basic concepts of object-oriented programming, you can better understand the core ideas of Java programming. I hope this article can help you go further and further on the road of programming, and constantly improve your programming skills!

Guess you like

Origin blog.csdn.net/qq_43546721/article/details/131922531