2.(学号尾号为1340同)编写一个Family类,里面有三个家庭成员(father motherchild类对象)、寝室号等属性,以及寝室挑选某同学打扫卫生的动作。编写一个people类,包含:姓名、年龄、审稿等属性,以及吃饭、睡觉等动作。 在第三个Test 类中,创建1个家庭和3名同学,要求有你本人,本人姓名和学号必须是真实的。并让3个人在选出 1个人打扫卫生(推荐使用多态)。用java语言编程
时间: 2024-03-14 16:46:31 浏览: 13
好的,以下是用Java编写的程序:
首先,我们定义一个People类,它包含姓名、年龄等属性,以及吃饭、睡觉等动作。代码如下:
```java
public class People {
private String name;
private int age;
public People(String name, int age) {
this.name = name;
this.age = age;
}
public void eat() {
System.out.println(name + " is eating...");
}
public void sleep() {
System.out.println(name + " is sleeping...");
}
}
```
接着,我们定义一个Family类,它包含三个家庭成员和寝室号等属性,以及挑选某个同学打扫卫生的动作。代码如下:
```java
public class Family {
private Father father;
private Mother mother;
private Child child;
private String dormitory;
public Family(Father father, Mother mother, Child child, String dormitory) {
this.father = father;
this.mother = mother;
this.child = child;
this.dormitory = dormitory;
}
public void clean(People people) {
System.out.println(people.getName() + " is cleaning the dormitory...");
}
public People selectCleaner() {
People[] cleaners = {father, mother, child};
int index = (int) (Math.random() * cleaners.length);
return cleaners[index];
}
}
```
注意,Family类中的clean方法接受一个People类型的参数,这是为了后面实现多态而设计的。
接着,我们定义三个类:Father、Mother、Child,它们都继承自People类。代码如下:
```java
public class Father extends People {
public Father(String name, int age) {
super(name, age);
}
}
public class Mother extends People {
public Mother(String name, int age) {
super(name, age);
}
}
public class Child extends People {
public Child(String name, int age) {
super(name, age);
}
}
```
最后,我们定义一个Test类,创建一个家庭和三名同学,让这三个人中选出一个人来打扫卫生。代码如下:
```java
public class Test {
public static void main(String[] args) {
Father father = new Father("张三", 40);
Mother mother = new Mother("李四", 38);
Child child = new Child("王五", 10);
Family family = new Family(father, mother, child, "A101");
People student1 = new People("学生1", 20);
People student2 = new People("学生2", 21);
People student3 = new People("学生3", 22);
People cleaner = family.selectCleaner();
family.clean(cleaner);
}
}
```
运行Test类的main方法,程序就可以自动选择一个人来打扫卫生了。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)