题目
写一个学生类,计算平均成绩
一个grade();是在main方法里写的直接调用
一个cj();是在实体类写的打点调用,因为算是成员方法
注意理解
位置
代码
Student.java
package com.zhang.opp;
/**
* @Author: ggdpzhk
* @CreateTime: 2024-08-05
*/
public class Student {
String name;
int chinese,math;
public Student(String name, int chinese, int math) {
this.name = name;
this.chinese = chinese;
this.math = math;
}
public Student() {
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", chinese=" + chinese +
", math=" + math +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public double cj() {
return (chinese+math)/2;
}
}
Test.java
package com.zhang.opp;
/**
* @Author: ggdpzhk
* @CreateTime: 2024-08-05
*/
public class Test {
public static double grade(Student st){
return (st.chinese+st.math)/2;
}
public static void main(String[] args) {
Student s1 = new Student("张三",100,98);
System.out.println(s1);
double grade = grade(s1);
System.out.println(grade);
Student s2 = new Student();
s2.name = "李四";
s2.chinese = 90;
s2.math = 80;
System.out.println(s2);
double cj = s2.cj();
System.out.println(cj);
}
}