属性私有,get/set
public class Application {
public static void main(String[] args) {
/*
* 1.提高程序的安全性,保护数据
* 2.隐藏代码的实现细节
* 3.统一接口
* 4.提高了系统的维护性
* */
Student s1 = new Student();//实例化类,非静态方法
s1.setName("张三");
System.out.println(s1.getName());
s1.setAge(567);
System.out.println(s1.getAge());
}
}
类
public class Student {标签:封装,name,int,age,public,id,void From: https://www.cnblogs.com/huangjiangfei/p/17968055
private String name;
private int id;
private char sex;
private int age;
// alt+insert 设置get/set
//提供一些public 的get set 方法
//get 获得这个数据
public String getName() {
return this.name;
}
//set 给这个数据设置值
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public char getSex(){
return this.sex;
}
public void setSex(){
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if(age>120 || age<0){
this.age = 3;
}else {
this.age = age;
}
}
}