一、java面向对象三大特别:
1、封装
2、继承
3、多态
 
二、封装的作用
1、属性私有化(private)
2、对外提供简单的入口
如公开的set()与get()方法,并且都不带static
 
三、举例说明
public class TestBase11Encapsulation {
    private int age;
    public static void main(String[] args) {
        TestBase11Encapsulation mytest=new TestBase11Encapsulation();
        mytest.setAge(15);
        System.out.println(mytest.getAge());
    }

    public int getAge() {
        return age;
    }

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

版权声明:本文为mrwhite2020原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/mrwhite2020/p/13383036.html