Java创建两个类源代码 编一个java程序有两个类( 二 )


}
public void setStudentNo(String studentNo) {
this.studentNo = studentNo;
sum();
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
sum();
}
public double getEnglishScore() {
return englishScore;
}
public void setEnglishScore(double englishScore) {
this.englishScore = englishScore;
sum();
}
public double getComputerScore() {
return computerScore;
}
public void setComputerScore(double computerScore) {
this.computerScore = computerScore;
sum();
}
public double getMathScore() {
return mathScore;
}
public void setMathScore(double mathScore) {
this.mathScore = mathScore;
sum();
}
public double getTotalScore() {
return totalScore;
}
}
//Student子类学习委员类的实现
class StudentXW extends Student {
//重写父类Student的testScore()方法
@Override
public double testScore() {
return sum()/3+3;
}
public StudentXW() {}
//StudentXW的构造函数
public StudentXW(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {
super(studentNo,studentName,englishSocre,computerScore,mathScore);
}
}
//Student子类班长类的实现
class StudentBZ extends Student {
//重写父类Student的testScore()方法
@Override
public double testScore() {
return sum()/3+5;
}
public StudentBZ() {}
//StudentXW的构造函数
public StudentBZ(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {
super(studentNo,studentName,englishSocre,computerScore,mathScore);
}
}
//测试类
public class Test {
public static void main(String[] args) {
//生成若干个student类、StudentXW类、StudentBZ类
Student student1 = new Student("s001","张三",70.5,50,88.5);
Student student2 = new Student("s002","李四",88,65,88.5);
Student student3 = new Student("s003","王五",67,77,90);
StudentXW student4 = new StudentXW("s004","李六",99,88,99.5);
StudentBZ student5 = new StudentBZ("s005","朱漆",56,65.6,43.5);
Student[] students = {student1,student2,student3,student4,student5};
for(int i = 0 ; istudents.length; i++){
double avgScore = students[i].testScore();
System.out.println(students[i].getStudentName()+"学生的评测成绩为:"+ avgScore+"分");
}
}
}
运行结果为:
张三学生的评测成绩为:69.66666666666667分
李四学生的评测成绩为:80.5分
王五学生的评测成绩为:78.0分
李六学生的评测成绩为:98.5分
朱漆学生的评测成绩为:60.03333333333333分
用java写两个类?以下是实现要求的Person和Student类的示例代码:csharp?Copy codepublic class Person {private Student student; // Person类的成员变量public Person(String studentName, int studentAge) {// 构造方法中完成必要的初始化内容this.student = new Student(studentName, studentAge);}public void doSomething() {// Person类的成员方法中调用Student类型成员变量的一个成员方法this.student.study();}}public class Student {private String name;private int age;public Student(String name, int age) {this.name = name;this.age = age;}public void study() {System.out.println(this.name + "正在学习");}}public class Main {public static void main(String[] args) {Person person = new Person("张三", 18);person.doSomething(); // 对Person类的成员方法的调用}}以上代码中 , Person类中有一个Student类型的成员变量,未初始化 , 构造方法中完成了该成员变量的初始化,Person类的某个成员方法中调用了Student类型成员变量的一个成员方法,而在主方法中完成了对该成员方法的调用

推荐阅读