星月泪眼
2015-05-07 11:57
采纳率: 100%
浏览 1.9k
已采纳

Java新手,求大神帮小弟看看这段代码哪里错了,多谢!!!

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();
}
}
}

7条回答 默认 最新

相关推荐 更多相似问题