컴퓨터 지식 네트워크 - 컴퓨터 교육 - 고득점을 위한 두 가지 간단한 JAVA 디자인 소스 코드

고득점을 위한 두 가지 간단한 JAVA 디자인 소스 코드

위의 wuzhikun12 님이 작성한 내용은 좋지만 아직 실행이 가능할 것 같지도 않고 완벽하지도 않습니다.

작동하는 것을 하나 드리겠습니다: (참고: 파일 이름은 Test.java입니다.)

//객체 간 비교를 수행하려면 CompareTo 메서드가 있는 Comparable 인터페이스를 구현해야 합니다.

//Comparable은 제네릭을 사용하는 것이 가장 좋으므로 속도와 코드 양이 모두 줄어듭니다.

@SuppressWarnings("unchecked")

class Student는 Comparable을 구현합니다 {

private String StudentNo; //학생번호

private String StudentName //이름

private double englishScore; p>private doublecomputerScore; //컴퓨터 점수

private double mathScore; //수학 점수

private double totalScore; 생성자

public Student() {}

//생성자

public Student(String StudentNo,String StudentName,double englishSocre,doublecomputerScore,double mathScore ) {

this.studentNo = 학생번호;

this.studentName = 학생이름;

this.englishScore = englishSocre; ComputerScore;

this.mathScore = mathScore

}

//총점 계산

public double sum() {

this.totalScore = englishScore+computerScore+mathScore;

return totalScore;

}

//평가 점수 계산

public double testScore() {

return sum()/3

}

//compareTO 메소드 구현

@Override

public int CompareTo(학생 학생) {

double StudentTotal = Student.getTotalScore()

return totalScore==studentTotal?0:( totalScore>studentTotal?1:-1);

}

//toString 메서드 재정의

public String toString(){

return "학생 ID: "+this.getStudentNo()+" 이름: "+this.getStudentName()+" 영어 점수: "+this.getEnglishScore()+" 수학 점수: "+this.getMathScore()+" 컴퓨터 점수: "+this.get

ComputerScore()+" 총 점수: "+this.getTotalScore();

}

//equals 메소드 재정의

public boolean equals(Object obj ) {

if(obj == null){

false 반환

}

if(!(obj 인스턴스of Student) ){

false를 반환

}

학생 학생 = (학생)obj

if(this.studentNo.equals( Student.getStudentName())) { //실제로 같은 학생인지 비교하려면 학번이 같은지 확인하면 됩니다.

return true; } else {

return false;

}

}

/*다음은 개인적으로 생각하는 get 및 set 메소드입니다. 그 totalScore set 메소드는 다른 점수로부터 계산되기 때문에 불필요합니다

set 메소드에서는 값을 한 번 설정하지 않고 sum 메소드를 한 번만 호출하여 총점을 다시 계산합니다

p>

*/

public String getStudentNo() {

return StudentNo;

}

public void setStudentNo(String StudentNo) {

p>

this.studentNo = StudentNo;

sum()

}

public String getStudentName; () {

return StudentName;

}

public void setStudentName(String StudentName) {

this.studentName = StudentName; /p>

sum();

}

public double getEnglishScore() {

return englishScore; /p>

public void setEnglishScore( double englishScore) {

this.englishScore = englishScore

sum()

}

public double getComputerScore() {

computerScore 반환

}

public void setComputerScore(doublecomputerScore) {

this .computerScore = 컴퓨터 점수;

sum();

}

public double getMathScore() {

return mathScore; >

}

public void setMathScore(double mathScore) {

this.mathScore = mathS

코어;

sum();

}

public double getTotalScore() {

return totalScore; >}

}

//학생 하위 클래스 학습 위원회 클래스 구현

class StudentXW는 Student {

//testScore를 다시 작성합니다. () 상위 클래스 Student의 메소드

@Override

public double testScore() {

return sum()/3+3;

}

public StudentXW() {}

//StudentXW의 생성자

public StudentXW(String StudentNo,String StudentName,double englishSocre,doublecomputerScore ,double mathScore) {

super(studentNo,studentName,englishSocre,computerScore,mathScore)

}

}

/ / Student 하위 클래스 모니터 클래스 구현

class StudentBZ는 Student {

//상위 클래스 Student의 testScore() 메서드를 재정의합니다.

@Override p>

public double testScore() {

return sum()/3+5

}

public StudentBZ() {}

p>

//StudentXW 생성자

public StudentBZ(String StudentNo,String StudentName,double englishSocre,doublecomputerScore,double mathScore) {

super(studentNo,studentName, englishSocre,computerScore,mathScore)

}

}

//테스트 클래스

공개 클래스 테스트 {

public static void main(String[] args) {

//여러 학생 클래스, StudentXW 클래스 및 StudentBZ 클래스 생성

Student Student1 = new 학생("s001", "장산",70.5,50,88.5)

학생 학생2 = 신입생("s002","lee思",88,65,88.5); >

학생 Student3 = new Student("s003","王五",67,77,90)

StudentXW Student4 = new StudentXW("s004","lee六",99, 88,99.5);

StudentBZ 학생5 = new StudentBZ("s005","주치",56,65.6,43.5)

학생

nts = {student1,student2,student3,student4,student5};

for(int i = 0 ; i

double avgScore = 학생[i ].testScore();

System.out.println(students[i].getStudentName()+"학생의 평가 점수: "+ avgScore+"points")

}

}

}

작업 결과:

Zhang San 학생의 평가 점수: 69.66666666666667점

Li Si 학생 평가 점수: 80.5점

Wang Wu 학생 평가 점수: 78.0점

Li Liu 학생 평가 점수: 98.5 포인트

Zhuqi 학생들의 평가 점수: 60.03333333333333 포인트

上篇: Diamond Claw 노트북은 무명 브랜드인가요? 下篇: 고등 지리학 추천 교재는 무엇입니까?
관련 내용