package com.company;
/*
* 类的封装
* 1 成员变量必须私有化 private 修饰
* 1 使用 getter setter 提供对外的访问方法
* */
public class Demo {
public String name ;
public int age ;
//设置值
public void setName(String name ){
this.name = name ;
}
public String getName(){
return this.name;
}
// 获取值
public void setAge(int age ){
this.age = age ;
}
public int getAge(){
return this.age;
}
}
//
package com.company;
public class testDemo {
public static void main(String[] args) {
/* Demo demo = new Demo();
demo.name = "小明";
demo.age = 99 ;
System.out.println(demo.name);
System.out.println(demo.age);*/
Demo demo = new Demo();
demo.setName("小明");
demo.setAge(19);
System.out.println(demo.getName());
System.out.println(demo.getAge());
}
}
```2