Java object-oriented basics (Day 5)

1.Constructor

1. What is the role of constructor?
Used to initialize an object of a class and return the address of the object. (Car c = new Car());This Car(); is a simple constructor.
2. What is the definition format of the constructor?
Modifier class name (formal parameter list) { } 3. Classification of constructors (1) Parameterless constructor (existing by default): When initializing an object, the data of member variables adopt default values. (2) Parameter constructor: When initializing the object, you can assign values ​​to the object at the same time. for example:





public class Car {
    
    
    String CarName;
    double price;

    //  无参数构造器
    public Car(){
    
    
         //这里定义的内容
    }
    // 有参数构造器
    public Car(String n, String b){
    
    
        // 这里输入内容
    }
}

4. Format of initialized object
Type variable name = new constructor;
Car c = new Car( );
5. Notes
(1) Any class defined will have a parameterless constructor by default, whether written or not.
(2) Once the parameterized constructor is defined, the parameterless constructor will no longer exist. At this time, you need to write a parameterless constructor yourself.

2. Summary

1. What is the role of constructor?
Answer: Initialize the object of the class and return the address of the object.
2. How many types of constructors are there, and what are their functions?
Answer: Parameterless constructor: When initializing the object, the data of the member variables adopt default values.
Parameterized constructor: When initializing the object, you can also assign values ​​to the object.
3. What are the precautions for constructors?
Any class defined will have a parameterless constructor by default, whether it is written or not.
Once the parameterized constructor is defined, the parameterless constructor is gone. At this time, you need to write your own parameterless constructor.

3. Encapsulation

The three major characteristics of object-oriented: encapsulation, integration, and polymorphism.
What is encapsulation? Hide implementation details and expose appropriate access methods. (Reasonable concealment, reasonable exposure)
You may ask here, why use encapsulation?
Let's look at an example:
Insert image description here
If there is no need for encapsulation here, then it is still okay for us to assign an age of -23 to the age.
1. Encapsulation implementation steps
(1) Generally, member variables are hidden using the private keyword modification. After private modification, the member variable can only be accessed in the current class.
(2) Provide public modified public getter and setter methods to expose their values ​​and assignments.

public class Student {
    
    
    private int age;
    public int getAge(){
    
    
        return age;
    }
    public void setAge(int age){
    
    
        if (age >= 0 && age <= 200){
    
    
            this.age = age;
        }else{
    
    
            System.out.println("请检查年龄数值");
        }
    }
}

Benefits of encapsulation:
Enhanced program code security.
Proper encapsulation can improve development efficiency and make the program easier to understand and maintain.

4. Summary

1. What is packaging, and how is general packaging reflected?
(1) One of the three major characteristics of object-oriented, reasonable hiding and reasonable exposure
(2) Generally, member variables are hidden using private.
(3) Expose its access through getter and setter methods.
2. What are the benefits of encapsulation?
(1) Strengthen the security of program code.
(2) Proper encapsulation can improve development efficiency and make the program easier to understand and maintain.

5.JavaBean

It can also be understood as an entity class, and its objects can be used to encapsulate data in programs.
1. Standard JavaBean must meet the following requirements:
(1) Member variables should be modified with private.
(2) Provide setXxx()/getXxx() corresponding to each member variable.
(3) A parameterless constructor must be provided.

public class User {
    
    
    // 1、成员变量私有
    private String name;
    private double height;
    private double salary;

    // 2、必须提供成套的getter和setter方法暴露成员变量的取值和赋值

    public void setName(String name) {
    
    
        this.name = name;
    }

    public void setHeight(double height) {
    
    
        this.height = height;
    }

    public void setSalary(double salary) {
    
    
        this.salary = salary;
    }

    public String getName() {
    
    
        return name;
    }

    public double getHeight() {
    
    
        return height;
    }

    public double getSalary() {
    
    
        return salary;
    }
}

6. Create objects using parameterized and parameterless constructors

public class Test1 {
    
    
    public static void main(String[] args) {
    
    

        //1、调用无参数构造器创建对象
        User U = new User();
        u.setName("吴彦祖");
        u.setHeight(185.2);
        u.setSalary(1000);
        System.out.println(u.getName());
        System.out.println(u.getHeight());
        System.out.println(u.getSalary());

        // 2、调用有参数构造器创建对象
        User u2 = new User(name "anganlbaby", height 169.5 ,sarary 1000);
        System.out.println(u2.getName());
        System.out.println(u2.getHeight());
        System.out.println(u2.getSarary());
    }
}

Summary: On the first day of Dark Horse Programmer's Java course, he will update the knowledge he learns every day from now on.

Guess you like

Origin blog.csdn.net/tyloonulinuli/article/details/121621853