public static void main(String[] args) {
// TODO Auto-generated method stub
Person p1=new Person();
p1.setAge(25);
p1.setName("Anbi");
p1.setTel("13099999999");
System.out.print("Age:"+p1.getAge()+"\tname:"+p1.getName()+"\ttel:"+p1.getTel());
System.out.println();
}
}
/*用private关键字封装,成员属性不能由对象直接访问,只能通过成员函数访问*/
class Person{
private String name;
private int age;
private String tel;
/*不带参构造函数*/
public Person() {
}
/*带参构造函数*/
public Person(String name, int age, String tel) {
super();
this.name = name;
this.age = age;
this.tel = tel;
}
/*成员方法*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
}
封装private实例
最新推荐文章于 2021-03-28 15:54:35 发布