class Demo2_Student
{
public static void main(String[] args)
{
Student s1=new Student();//使用空参构造
s1.setName("张三");
s1.setAge(23);
System.out.println("姓名是:"+s1.getName()+" 年龄是:"+s1.getAge());
Student s2=new Student("李四",24);
s2.show();//只是为了显示属性值
}
}
class Student
{
private String name;
private int age;
public Student(){}
public Student(String name,int age){
this.name=name;
this.age=age;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setAge(int age){
this.age=age;
}
public int getAge(){
return age;
}
public void show()
{
System.out.println("姓名是:"+name+" 年龄是:"+age);
}
} |
|