封装:
将属性和方法封装为一个整体,属性和方法都有权限限制
class Person { private int age;//私有属性,只能在本类中访问 public void setAge(int age) { if (age < 0 || age > 200) age = 0; else { this.age = age; } } public int getAge() { return age; } } public class Enc { public static void main(String args[]) { Person per = new Person(); //per.age = -1;//age是person类私有的,不能在这里访问 per.setAge(-1);//只能通过共有的方法访问person类中私有的属性 System.out.println(per.getAge()); } }
标签:封装,int,age,per,Person,java,public From: https://www.cnblogs.com/zj-studyrecoding/p/17441770.html