class Student
{
//姓名
private String name;
//年齡
private int age;
//構造方法
public Student(){
}
public Student(String name, int age){
this.name = name;
this.age = age;
}
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 void show(){
System.out.println(name+"----"+age);
}
//測試類
class StudentTest
{
public static void main(String[]args){
//方式一給成員變量賦值
//無參構造+setXxx()
Student s1 = new Student();
s1.setName("林青霞");
s1.setAge(27);
//輸出值
System.out.println(s1.getName()+"----"+s1.getAge());
s1.show();
System.out.println("-----------------------");
//方式二給成員變量賦值
Student s2 = new Student("劉一",30);
System.out.println(s2.getName()+"----"+s2.getAge());
s2.show();
}
}
}
class StudentTest 這個不對吧
要加 public
親測