import java.text.DecimalFormat;
public class Student {
public static String schoolName;
public String stuNumber;
public String stuName;
private double mathScore;
private double netScore;
private double javaScore;
static {
System.out.println("Student类被加载了。。");
}
//无参构造方法
public Student() {};
//初始化学号姓名的有参构造方法
public Student(String stuNumber, String stuName) {
this.stuNumber = stuNumber;
this.stuName = stuName;
}
//初始化所有属性,除了学校的有参构造方法
public Student(String stuNumber, String stuName, double mathScore, double netScore, double javaScore) {
this.stuNumber = stuNumber;
this.stuName = stuName;
this.mathScore = mathScore;
this.netScore = netScore;
this.javaScore = javaScore;
}
//静态有参方法,求分数平均值
public static double scoreAvg(double a ,double b , double c ) {
double num = (a+b+c)/3;
DecimalFormat df = new DecimalFormat("#.00");//保留两位小数
String mat = df.format(num);
return Double.parseDouble(mat);
}
//非静态无参方法,自我介绍
public void showMe() {
double avgScore = scoreAvg(mathScore, netScore, javaScore);
System.out.println("大家好,我是"+ schoolName+ "的学生,学号:"+ stuNumber
+",在这次考试中,数学 "+ mathScore+" 分,网络 "+netScore+" 分,java "+ javaScore+" 分,平均分"+ avgScore);
}
//-------封装三门课的成绩--start------------------------
public double getMathScore() {
return mathScore;
}
public void setMathScore(double mathScore) {
//确保成绩在0到100之间
if(mathScore>=0 && mathScore<=100)
this.mathScore = mathScore;
else
this.mathScore= 0;
}
public double getNetScore() {
return netScore;
}
public void setNetScore(double netScore) {
//确保成绩在0到100之间
if(netScore>=0 && netScore <=100)
this.netScore = netScore;
else
this.netScore = 0;
}
public double getJavaScore() {
return javaScore;
}
public void setJavaScore(double javaScore) {
//确保成绩在0到100之间
if(javaScore>=0 && javaScore<=100)
this.javaScore = javaScore;
else
this.javaScore = 0;
}
//-------封装三门课的成绩--end----------------------
//测试运行
public static void main(String[] args) {
Student s1 = new Student("A001","张三",87,79,93);
Student s2 = new Student("B242","李四",65,86,81);
Student.schoolName = "华中科技大学";
s1.showMe();
s2.showMe();
}
}