JAVA基础问题求解😭

编写一个学生类:
(1)成员变量:
考生的name(姓名)、id(学
号)、score(综合成绩)sports(体育成绩);
(2)构造方法:
无参数 构造 方法;拷贝 构造 方法;
带参数 (考生 姓名、考号、综合 成绩、体育成绩)的构造 方法
(3)获取和设置学生综合成绩和体育成绩的方法。


public class Student {
    // 成员变量
    private String name;        // 姓名
    private String id;          // 学号
    private double score;       // 综合成绩
    private double sports;      // 体育成绩

    // 无参数构造方法
    public Student() {
    }

    // 拷贝构造方法
    public Student(Student other) {
        this.name = other.name;
        this.id = other.id;
        this.score = other.score;
        this.sports = other.sports;
    }

    // 带参数的构造方法
    public Student(String name, String id, double score, double sports) {
        this.name = name;
        this.id = id;
        this.score = score;
        this.sports = sports;
    }

    // 获取综合成绩
    public double getScore() {
        return score;
    }

    // 设置综合成绩
    public void setScore(double score) {
        this.score = score;
    }

    // 获取体育成绩
    public double getSports() {
        return sports;
    }

    // 设置体育成绩
    public void setSports(double sports) {
        this.sports = sports;
    }

    // 主函数用于测试
    public static void main(String[] args) {
        // 创建一个学生对象
        Student student1 = new Student("张三", "2021001", 90.5, 85.0);

        // 使用拷贝构造方法创建另一个学生对象
        Student student2 = new Student(student1);

        // 输出学生信息
        System.out.println("学生1信息:");
        System.out.println("姓名:" + student1.name);
        System.out.println("学号:" + student1.id);
        System.out.println("综合成绩:" + student1.getScore());
        System.out.println("体育成绩:" + student1.getSports());

        System.out.println();

        System.out.println("学生2信息:");
        System.out.println("姓名:" + student2.name);
        System.out.println("学号:" + student2.id);
        System.out.println("综合成绩:" + student2.getScore());
        System.out.println("体育成绩:" + student2.getSports());
    }
}

public class 学生类 {
    private String 姓名; 
    private String 学号;
    private double 综合成绩; 
    private double 体育成绩;

    public 学生类() {
    }

    public 学生类(学生类 other) {
        this.姓名 = other.姓名;
        this.学号 = other.学号;
        this.综合成绩 = other.综合成绩;
        this.体育成绩 = other.体育成绩;
    }

    public 学生类(String 姓名, String 学号, double 综合成绩, double 体育成绩) {
        this.姓名 = 姓名;
        this.学号 = 学号;
        this.综合成绩 = 综合成绩;
        this.体育成绩 = 体育成绩;
    }

    public double get综合成绩() {
        return 综合成绩;
    }

    public void set综合成绩(double 综合成绩) {
        this.综合成绩 = 综合成绩;
    }

    public double get体育成绩() {
        return 体育成绩;
    }

    public void set体育成绩(double 体育成绩) {
        this.体育成绩 = 体育成绩;
    }
}

【以下回答由 GPT 生成】

public class Student {
  private String name;
  private int id;
  private int score;
  private int sports;

  // 无参数构造方法
  public Student() {
  }

  // 拷贝构造方法
  public Student(Student student) {
    this.name = student.name;
    this.id = student.id;
    this.score = student.score;
    this.sports = student.sports;
  }

  // 带参数的构造方法
  public Student(String name, int id, int score, int sports) {
    this.name = name;
    this.id = id;
    this.score = score;
    this.sports = sports;
  }

  // 获取学生综合成绩
  public int getScore() {
    return score;
  }

  // 设置学生综合成绩
  public void setScore(int score) {
    this.score = score;
  }

  // 获取学生体育成绩
  public int getSports() {
    return sports;
  }

  // 设置学生体育成绩
  public void setSports(int sports) {
    this.sports = sports;
  }
}

正确答案应该如上所示。该学生类满足题目要求,具备了四个成员变量和对应的构造方法、获取和设置学生综合成绩和体育成绩的方法。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^