实现Java代码,定义一个StudentInfo类,包含属性:姓名(stuName)、年龄(stuAge)、性别(stuSex)、成绩(stuScore);使用构造函数初始化所有属性,然后定义showStuInfo()方法,显示学员的详细信息,并写一个测试类Test
时间: 2023-03-26 13:02:59 浏览: 106
StudentInfo类的代码如下:
```
public class StudentInfo {
private String stuName;
private int stuAge;
private String stuSex;
private double stuScore;
public StudentInfo(String name, int age, String sex, double score) {
this.stuName = name;
this.stuAge = age;
this.stuSex = sex;
this.stuScore = score;
}
public void showStuInfo() {
System.out.println("姓名:" + stuName);
System.out.println("年龄:" + stuAge);
System.out.println("性别:" + stuSex);
System.out.println("成绩:" + stuScore);
}
}
```
测试类Test的代码如下:
```
public class Test {
public static void main(String[] args) {
StudentInfo student = new StudentInfo("张三", 18, "男", 90.5);
student.showStuInfo();
}
}
```
输出结果为:
```
姓名:张三
年龄:18
性别:男
成绩:90.5
```
阅读全文