按照以下要求设计一个学生类Student,并进行测试。 1 Student类中包括姓名、成绩两个属性,并实现封装。private 2 分别给这两个属性定义两个方法,一个方法用于设置值,另一个用于获取值。 3 Student类中定义一个无参构造方法和一个接受两个参数的构造方法,两个参数分别为姓名和成绩属性赋值。 4 在测试类中创建两个Student对象,一个使用无参的构造方法,然后调用set方法给姓名和成绩赋值,另一个使用有参的构造方法,在构造方法中给姓名和成绩赋值,并输出学生信息。
时间: 2024-03-12 21:49:17 浏览: 52
好的,这个任务我已经完成了,以下是代码:
```
public class Student {
private String name;
private double score;
public Student() {
}
public Student(String name, double score) {
this.name = name;
this.score = score;
}
public void setName(String name) {
this.name = name;
}
public void setScore(double score) {
this.score = score;
}
public String getName() {
return name;
}
public double getScore() {
return score;
}
}
```
测试类代码:
```
public class Test {
public static void main(String[] args) {
Student student1 = new Student();
student1.setName("张三");
student1.setScore(80.5);
System.out.println("学生1的姓名为:" + student1.getName() + ",成绩为:" + student1.getScore());
Student student2 = new Student("李四", 90.0);
System.out.println("学生2的姓名为:" + student2.getName() + ",成绩为:" + student2.getScore());
}
}
```
输出结果为:
```
学生1的姓名为:张三,成绩为:80.5
学生2的姓名为:李四,成绩为:90.0
```
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)