如何用JAVA编写一个学生类,要求类中包括学生姓名,年龄,学号,班级,成绩,要求用到重载,覆盖.

解决方案 »

  1.   

    我觉得覆盖好象谈不上  重载的话应该是重载构造器吧..!
    估计LZ刚学封装..
    把所有属性设为私有.为每个属性设置get/set方法.  定义构造器的时候需要重载
      

  2.   


    public class Student {
    private String name, age, sno, cls, score;

    public Student() { }
    // 重载构造方法
    public Student(String name) {
    this(name, null);
    }
    // 重载构造方法
    public Student(String name, String cls) {
    this.setName(name);
    this.setCls(cls);
    } // 覆盖 Object 的 toString() 方法
    @Override
    public String toString() {
    return "我是" + this.getName() + ",我是" + this.getCls() + "班的学生。";
    } public String getName() {
    return this.name;
    } public void setName(String name) {
    this.name = name;
    } public String getAge() {
    return this.age;
    } public void setAge(String age) {
    this.age = age;
    } public String getSno() {
    return this.sno;
    } public void setSno(String sno) {
    this.sno = sno;
    } public String getCls() {
    return this.cls;
    } public void setCls(String cls) {
    this.cls = cls;
    } public String getScore() {
    return this.score;
    } public void setScore(String score) {
    this.score = score;
    }
    }