public
class
Grade {
private
double
english;
private
double
math;
private
double
sql;
public
Grade(
double
english,
double
math,
double
sql) {
this
.english = english;
this
.math = math;
this
.sql = sql;
}
public
double
total() {
return
english + math + sql;
}
public
void
printTotal() {
System.out.println(total());
}
public
double
average() {
return
total() /
3
;
}
public
void
printAverage() {
System.out.println(average());
}
public
double
getEnglish() {
return
english;
}
public
void
setEnglish(
double
english) {
this
.english = english;
}
public
double
getMath() {
return
math;
}
public
void
setMath(
double
math) {
this
.math = math;
}
public
double
getSql() {
return
sql;
}
public
void
setSql(
double
sql) {
this
.sql = sql;
}
}
class Student {
String name;
Grade grade;
public Student(String name, Grade grade) {
this.name = name;
this.grade = grade;
}
public static void main(String[] args) {
Student s = new Student("Cici", new Grade(100, 94, 90));
s.grade.printAverage();
s.grade.printTotal();
}
}
结果:
94.66666
284.0
原文地址:https://www.cnblogs.com/WCL00000/p/9102552.html