怎样用Java编写一个Student类,包含name,math,english,physics成员变量,包含计算平均值(average)、总成绩(sum)的成员方法
public class Student {
private String name;
private double math;
private double english;
private double physics;
public Student(String name, double math, double english, double physics) {
this.name = name;
this.math = math;
this.english = english;
this.physics = physics;
}
public double average() {
return this.sum() / 3.0;
}
public double sum() {
return math + english + physics;
}
}
@AllArgsConstructor
@Data
public class Student {
private String name;
private Double math;
private Double english;
private Double physics;
public Double sum(){
return math + english + physics;
}
public Double average(){
return sum() / 3;
}
}
你好题主,你可以参考各位长辈的答案,综合考虑,应该会有更大的收获,欢迎及时反馈。
public class Student {
// 成员变量
private String name;
private int math;
private int english;
private int physics;
// 构造函数
public Student(String name, int math, int english, int physics) {
this.name = name;
this.math = math;
this.english = english;
this.physics = physics;
}
// 成员方法
// 计算平均值
public double average() {
return (math + english + physics) / 3.0;
}
// 计算总成绩
public int sum() {
return math + english + physics;
}
}