Java object-oriented packaging and related knowledge collection


1. Encapsulation

1. Relevant concepts of encapsulation

  • In the object-oriented programming method, encapsulation refers to one kind 将抽象性函式接口的实现细节部分包装、隐藏起来的方法.
    Encapsulation can be thought of as a protective barrier 防止该类的代码和数据被外部类定义的代码随机访问.
    To access the code and data of this type, you must pass strict interface control.
    The main function of encapsulation is that we can modify our own implementation code without modifying the program fragments that call our code.
    适当的封装You can make code 更容易理解与维护, but also 加强了code 安全性. 1

2. The advantages of packaging

  1. Good packaging can reduce coupling.
  2. The internal structure of the class can be modified freely.
  3. You can control member variables more precisely.
  4. Hidden information, implementation details.

3. Encapsulation realization steps

1. Modify the access permission of the attribute as private

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

2. 属性设置为private(私有)After that, attributes 只能在本类中访问and other classes can't be accessed, so the hiding of information is realized. It must 使其它类继续访问be a private property if necessary 添加setter和getter方法.

public class Person{
    
    
    private String name;
    private int age;public void setAge(int age){
    
    
      this.age = age;
      //this.属性 指的是成员变量
      //this关键字可以解决成员变量和局部变量重名冲突问题
    }public int getAge(){
    
    
      return age;
    }public void setName(String name){
    
    
      this.name = name;
    }
    
    public String getName(){
    
    
      return name;
    }
    
}

3. With the Person class, create a new test class PersonTest class, explain how the privately modified member variables in the Person class can be accessed through other classes.

public class PersonTest {
    
    
    public static void main(String[] args) {
    
    
    	//创建对象Person类的对象
        Person p = new Person();
        //使用对象名.setter方法对成员变量赋值
        p.setName("李四");
        p.setAge(18);
        //使用对象名.getter方法取出成员变量的值
        System.out.println("姓名:"+p.getName()+",年龄:"+p.getAge());
    }
}

The results of the program are as follows:

姓名:李四,年龄:18

【tips】
The method is also a kind of encapsulation.

Two, private keywords

1. The role of private

  • Is a permission modifier, used to modify member variables and member methods.
  • Private is for the class, not for the object.
  • The privatized members are only valid in this class.

2.Private use case

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

After being modified by private, member variables can be used directly in this class, and setter and getter methods must be used in other classes

    public static void main(String[] args) {
    
    
        System.out.println("姓名:"+name+'\n'+"年龄:"+age);
    }

setter方法Must 有参数, and the type is consistent with the member variable
getter方法Must 有返回值, and the return value type is consistent with the member variable

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

    public String getName(){
    
    
        return name;
    }
    
    public void setAge(int age) {
    
    
       this.age = age;
    }

    public int getAge(){
    
    
        return age;
    }

【tips】
The rationality of the data can be defined in the setter and getter methods.
For example , the rationality of the age value can be defined in setAge()

    public void setAge(int age) {
    
    
        if (age >= 0 && age <120) {
    
    
            this.age = age;
        }else{
    
    
            System.out.println("数据不合理!");
        }
    }

Use private modified variables in the Person class in other classes

public class PersonTest {
    
    
    public static void main(String[] args) {
    
    
    	//创建对象Person类的对象
        Person p = new Person();
        //使用对象名.setter方法对成员变量赋值
        p.setName("李四");
        p.setAge(20);
        //使用对象名.getter方法取出成员变量的值
        System.out.println("姓名:"+p.getName()+",年龄:"+p.getAge());
    }
}

The results of the program are as follows:

姓名:李四,年龄:20

Three, this keyword

1. The scope of application of this

1. This keyword 指向is 当前对象的引用
2. The use of this keyword must be 在方法内部
3. this不能用于静态方法

2. Use of this keyword

variable

When the local variable and member variable of the method have the same name, the local variable will be used first (the principle of proximity)
Use this. attribute, it means the use of member variables

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

method

(1) this. method name
access to the member methods of this class

(2) This()
accesses the construction method of this class.
Note:

  • 1. If there are parameters in (), if there are parameters, the specified parameter structure is called
  • 2.this() can only be written in the constructor
  • 3. Must be the first statement in the construction method

Four, construction method

The construction method is a method specifically used to create an object. When the keyword new来创建对象时is passed , it is 是调用构造方法.

Construction method definition format:

public 类名称([参数类型 参数名称,参数类型 参数名称,...]{
    
    
			//参数可有可无
			//参数个数和类型可以根据需要定义
	方法体;
}

The name must be exactly the same as the name of the class it is in.
There is no return value type.
The compiler has a no-argument constructor by default.
Once at least one constructor is written, the default no-argument constructor disappears.

Five, define a standard class (Java Bean)

1. The composition of the standard class

  • 所有成员变量All使用private关键字修饰
  • 每一个成员变量都需要有setter和getter方法
  • write一个无参构造方法
  • write一个全参构造方法

2. Standard class example

import java.util.Date;

public class Person {
    
    
    private String name;
    private String sex;
    private Date birthday;
    private int age;

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

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

	//setter和getter方法
    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }
	
	//setter和getter方法
    public String getSex() {
    
    
        return sex;
    }

    public void setSex(String sex) {
    
    
        this.sex = sex;
    }
	
	//setter和getter方法
    public Date getBirthday() {
    
    
        return birthday;
    }

    public void setBirthday(Date birthday) {
    
    
        this.birthday = birthday;
    }
	
	//setter和getter方法
    public int getAge() {
    
    
        return age;
    }

    public void setAge(int age) {
    
    
        this.age = age;
    }
}


  1. Novice Tutorial-Java Package↩︎

Guess you like

Origin blog.csdn.net/weixin_44580492/article/details/105855912