/*
封装的意义
1.提高程序安全性,保护数据
2.隐藏代码的细节
3.通以接口形成规范
4.提高系统的可维护性
5.属性私有 get set
*/
package com.opp;
import com.opp.demo04.Student;
/*
封装的意义
1.提高程序安全性,保护数据
2.隐藏代码的细节
3.通以接口形成规范
4.提高系统的可维护性
5.属性私有 get set
*/
public class Application {
public static void main(String[] args) {
Student s1 = new Student();
s1.setName("秦疆");
System.out.println(s1.getName());
s1.setAge(999);
System.out.println(s1.getAge());
}
}
package com.opp.demo04;
//类
public class Student {
//属性私有
private String name;
private int id;
private char sex;
private int age;
//提供一些可以操作这个属性的方法
//提供一些 “public 的get set方法”
//get 获得这个数据
public String getName(){
return this.name;
}
//set 给这个数据设置值
public void setName(String name){
this.name = name;
}
//alt + insert
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
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;
}
}
}