首页 > 如何根据 Student 类创建对象并测试其方法

如何根据 Student 类创建对象并测试其方法

定义一个表示学生的类Student,包括域:学号、姓名、性别、年龄;方法:获得学号、姓名、性别、年龄;修改年龄。并书写java程序创建Student类 的对象及测试方法其方法的功能
求大侠贴出源代码


public class Student {
  private int id;

  private String name;

  private int sex;

  private int age;

  public Student(int id, String name, int sex, int age) {
    this.id = id;
    this.name = name;
    this.sex = sex;
    this.age = age;
  }

  public void updateAge(int age) {
    this.age = age;
  }

  public int getId() {
    return this.id;
  }

  public String getName() {
    return this.name;
  }

  public int getSex() {
    return this.sex;
  }

  public int getAge() {
    return this.age;
  }
}

Student a = new Student(1001, "Jay", 0, 18);
a.updateAge(19);
【热门文章】
【热门文章】