摘要:
用Java类的定义,类的成员变量
参考代码:
package com.gx.demo;
public class Student {
private String name;//姓名
private int pacificScore;//平时成绩
private int totalScore;//总评成绩
//有参构造方法
public Student(String name, int pacificScore, int totalScore) {
this.name=name;
this.pacificScore=pacificScore;
this.totalScore=totalScore;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPacificScore() {
return pacificScore;
}
public void setPacificScore(int pacificScore) {
this.pacificScore = pacificScore;
}
public int getTotalScore() {
return totalScore;
}
public void setTotalScore(int totalScore) {
this.totalScore = totalScore;
}
}
package com.gx.demo;
import java.util.ArrayList;
import java.util.List;
public class Test7 {
/**
* 总评成绩
* @param pacificScore 平时成绩
* @param checkScore 考核成绩
* @return
*/
public static int getTotalScore(int pacificScore, int checkScore) {
int totalScore=(int) (pacificScore*0.5 + checkScore*0.5);
return totalScore;
}
public static void main(String[] args) {
//调用Student类中的构造方法
Student student1=new Student("张三", 80, getTotalScore(80,76));
Student student2=new Student("李四", 76, getTotalScore(76,70));
Student student3=new Student("王五", 84, getTotalScore(84,68));
Student student4=new Student("宋六", 82, getTotalScore(82,60));
Student student5=new Student("钱七", 68, getTotalScore(68,74));
List<Student> list=new ArrayList<Student>();
list.add(student1);
list.add(student2);
list.add(student3);
list.add(student4);
list.add(student5);
for (Student student : list) {
String name = student.getName();
int pacificScore = student.getPacificScore();
int totalScore = student.getTotalScore();
System.out.println("姓名:"+name+";\t平时成绩:"+pacificScore+";\t总评成绩:"+totalScore);
}
}
}
输出结果:
姓名:张三; 平时成绩:80; 总评成绩:78
姓名:李四; 平时成绩:76; 总评成绩:73
姓名:王五; 平时成绩:84; 总评成绩:76
姓名:宋六; 平时成绩:82; 总评成绩:71
姓名:钱七; 平时成绩:68; 总评成绩:71