定义Student类成员变量有nameagemathChineseEnglish 后面三个变量是学生各科成绩。 定义构造方法 StudentString nameint ageint Englishint mathint Chinese 给成员变量赋值。定义printStudentInfo方法输出学生的所有信息。 创建TestStudent类 创建姓名为张天年龄为12成绩分别为928587的学生对
Student类的代码如下:
public class Student {
private String name;
private int age;
private int math;
private int Chinese;
private int English;
public Student(String name, int age, int English, int math, int Chinese) {
this.name = name;
this.age = age;
this.English = English;
this.math = math;
this.Chinese = Chinese;
}
public void printStudentInfo() {
System.out.println("姓名:" + name);
System.out.println("年龄:" + age);
System.out.println("数学成绩:" + math);
System.out.println("语文成绩:" + Chinese);
System.out.println("英语成绩:" + English);
}
}
TestStudent类的代码如下:
public class TestStudent {
public static void main(String[] args) {
Student student1 = new Student("张天", 12, 92, 85, 87);
student1.printStudentInfo();
Student student2 = student1;
student2.printStudentInfo();
student2 = new Student("李四", 13, 80, 90, 95);
student1.printStudentInfo();
}
}
输出结果如下:
姓名:张天
年龄:12
数学成绩:92
语文成绩:85
英语成绩:87
姓名:张天
年龄:12
数学成绩:92
语文成绩:85
英语成绩:87
姓名:张天
年龄:12
数学成绩:92
语文成绩:85
英语成绩:87
原文地址: https://www.cveoy.top/t/topic/NjY 著作权归作者所有。请勿转载和采集!