封装的标准代码:
class Teacher
{
private String name;
private int age;
public Teacher()
{
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setAge(int a)
{
this.age = age;
}
public int getAge()
{
return age;
}
}
class TeacherTest
{
public static void main(String[] args)
{
//创建一个Teacher类的对象,分别给姓名和年龄赋值,然后输出结果
Teacher t = new Teacher();
t.setName("林青霞");
t.setAge(26);
System.out.println(t.getName()+"***"+t.getAge());
}
}
每次都要写public Teacher()
{
}
这个代码么 也可以不用吧 |
|