Home  >  Article  >  Java  >  What are the three major characteristics of object-oriented encapsulation? How to implement encapsulation (code example)

What are the three major characteristics of object-oriented encapsulation? How to implement encapsulation (code example)

青灯夜游
青灯夜游forward
2018-10-27 17:43:353110browse

The content of this article is to introduce what are the three major features of object-oriented encapsulation? Ways to implement encapsulation (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. What is encapsulation? And why encapsulation is necessary?

Usually, you can assign some legal but unreasonable values ​​to member variables. In this case, no error will be reported or any prompt information will be given during the compilation phase and the running phase. Although this value is legal But it is inconsistent with real life; in order to avoid the above problems, member variables need to be sealed and packaged to ensure the legality and rationality of the member variables. This mechanism is called encapsulation. Encapsulation can be thought of as a protective barrier that prevents the code and data of the class from being randomly accessed by code defined by the external class. Access to the code and data of this class must be controlled through strict interfaces.

2. How to encapsulate?

(1) Privatize member variables and use the private keyword to modify them;

(2) Provide public get and set methods, judge reasonable values ​​in the method body, and use the public key Word modification;

(3) Use the set method in the construction method to judge reasonable values;

3. Examples are as follows/*Person.java*/

/*
     编程实现Person类的封装
 */
public class Person{
    //1.私有化成员变量,使用private关键字修饰
    private String name; 
    private int age;
    private String country;
    //使用static关键字修饰成员变量表示提升为类层级只有一份被所有对象共享
    //public static String country;

    //3.在构造方法中调用set方法进行合理值的判断
    public Person(){
    }
    public Person(String name, int age, String country){
        setName(name);
        setAge(age);
        setCountry(country);
    }

    //2.提供公有的get和set方法,在方法体中进行合理值的判断
    public String getName(){
        return name;
    }
    public void setName(String name){
        this.name = name;
    }
    public int getAge(){
        return age;
    }
    public void setAge(int age){
        if(age > 0 && age < 150){
            this.age = age;
        }else{
            System.out.println("年龄不合理!!!");
        }
    }
    public String getCountry(){
        return country;
    }
    public void setCountry(String country){
        this.country = country;
    }

    public void show(){
        System.out.println("我是" + getName() + ",今年" + getAge() + "岁了,来自" + getCountry() + "!");
    }

    //自定义成员方法描述吃饭的行为
    public void eat(String food){
        System.out.println(food + "真好吃!");
    }
    //自定义成员方法描述娱乐的行为
    public void play(String game){
        System.out.println(game + "真好玩!");
    }
}

The above is the detailed content of What are the three major characteristics of object-oriented encapsulation? How to implement encapsulation (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete