面向对象的程序设计三大特性:
- 封装性
- 继承性
- 多态性
封装性
不仅仅是把属性和方法封装到一起,还有不同的关键字表示权限
private:
class Person { //习惯大写
private int age; //private 只可以在类内访问
public void setAge(int age){
this.age = age;
}
public int getAge(){
return this.age;
}
};
public class Oop { //必须大写和文件名一致
public static void main(String args[]){
Person p1 = new Person(); //使用new创建类
p1.setAge(6);
System.out.println(p1.getAge());
}
}
- age只能通过setAge方法设置,和getAge方法获取,不能直接在类外直接调用,避免误操作