The Foundation of Java] Realize the Object-Oriented Programming Method

Table of contents

Standard JavaBean class

Design objects and use

object encapsulation

this keyword

Construction method


We must know that objects are things that can help us solve problems one after another, but these things do not appear out of thin air, we need to manufacture them according to the design drawings, and these design drawings one by one are classes one by one.

Standard JavaBean class

1) The class name needs to be known by the name

2) Member variables are modified with private

3) Provide at least two construction methods (no parameter construction method, construction method with all parameters)

4) Member methods (provide get and set methods corresponding to each member variable, if there are other behaviors, also need to be written)

Class (design diagram): It is a description of the common characteristics of objects; objects are concrete things that actually exist. In Java, classes must be designed before objects can be obtained.

Design objects and use

How to define a class in Java? as follows:

public class Phone {
    // 属性
    String brand;
    double price;
    
    // 行为
    public void call(){
        System.out.println("小米手机");
    }
    public void playGame(){
        System.out.println("打游戏");
    }
}

How to get the object of the class in Java? as follows:

类名 对象名 = new 类名();
Phone p = new Phone();

How to use objects in Java? as follows:

Access properties: object name. member variable

Access Behavior: ObjectName.MethodName(...)

code show as below:

Note :

1) It is recommended to capitalize the first letter of the class name, you need to see the name and know the meaning, hump mode.

2) Multiple classes can be defined in a Java file, and only one class can be public modified, and the public modified class name must become the code file name. In actual development, it is recommended that a file define a class class.

3) The complete definition format of a member variable is: modifier data type variable name = initialization value; generally there is no need to specify an initialization value, and there is a default value.

object encapsulation

Encapsulation tells us how to correctly design the properties and methods of an object. What the object represents must encapsulate the corresponding data and provide the behavior corresponding to the data.

private keyword : It is a permission modifier ( private ), which can modify member variables and methods, and members modified by private can only be accessed in this class.

public class Phone {
    // 属性
    private String brand;
    private double price;
    // 针对每一个私有化的成员变量,都要提供get和set方法
    // set方法:给成员变量赋值
    public void setBrand(String n){
        brand = n;
    }
    public void setPrice(double m){
        if(m >= 1000 && m<= 3000){
            price = m;
        }else{
            System.out.println("价格太高,我不允许!");
        }
    }
    // get方法:对外提供成员变量的值
    public String getBrand(){
        return brand;
    }
    public double getPrice(){
        return price;
    }
    // 行为
    public void call(){
        System.out.println("小米手机");
    }
    public void playGame(){
        System.out.println("打游戏");
    }
}

For member variables modified by private, if they need to be used by other classes, the corresponding operations above should be provided. The set method is used to assign values ​​to member variables, and the get method is used to obtain the value of member variables:

public class PhoneTest {
    public static void main(String[] args) {
        Phone p = new Phone();
        p.setBrand("小米");
        p.setPrice(1999.98);
        // 获取手机对象中的值
        System.out.println(p.getBrand());
        System.out.println(p.getPrice());
        // 调用手机中的方法
        p.call();
        p.playGame();
    }
}

this keyword

When we do not use the this keyword, the value of the code adopts the principle of proximity as follows:

When we use the this keyword, we access global variables instead of local variables:

Construction method

Construction methods are also called constructors, constructors. Function: When creating an object, it is automatically called by the virtual machine to initialize and assign values ​​to member variables.

The format of the construction method is as follows, and its specific characteristics are:

1) The method name is the same as the class name, with the same capitalization

2) There is no return value type, not even void

3) There is no specific return value (the result data cannot be brought back by return)

Construction method :

1) No parameter construction method: When initializing the object, the data of the member variables adopts the default value

2) There is a parameter construction method: when the object is initialized, the object can be assigned a value at the same time

public class Student {
    修饰符 类名(参数){
        方法体;
    }
}

Constructor Notes

Constructor definition :

1) If no constructor is defined, the system will give a default parameterless constructor

2) If a constructor is defined, the system will no longer provide a default constructor

Overloading of the constructor :

1) The constructor with parameters and the constructor without parameters have the same method name but different parameters. This is called overloading of the constructor

Recommended usage :

Regardless of whether it is used or not, manually write the no-argument construction method and the construction method with all parameters

If you want to quickly generate the construction method, you can also use shortcut keys ( alt + insert ) for one-click generation, as follows:

Of course, you can also use the plug-in PTG to generate standard JavaBean in 1 second, as follows:

Guess you like

Origin blog.csdn.net/qq_53123067/article/details/130663195