标签:play 软件设计 void System Person live public
[实验任务一]:女娲造人
使用简单工厂模式模拟女娲(Nvwa)造人(Person),如果传入参数M,则返回一个Man对象,如果传入参数W,则返回一个Woman对象,如果传入参数R,则返回一个Robot对象。请用程序设计实现上述场景。
实验要求:
1. 画出对应的类图;
2. 提交源代码;
1
package nvwa;
public abstract class Person {
public void live() {
}
public void play() {
}
}
2
package nvwa;
public class Man extends Person {
@Override
public void live() {
System.out.println("女娲造人————男人");
super.live();
}
public void play() {
System.out.println("男人玩耍中.....");
super.play();
}
}
3
package nvwa;
public class Woman extends Person {
@Override
public void live() {
System.out.println("女娲造人————女人");
super.live();
}
public void play() {
System.out.println("女人玩耍中.....");
super.play();
}
}
4
package nvwa;
public class Robot extends Person{
@Override
public void live() {
System.out.println("女娲造人————机器人");
super.live();
}
public void play() {
System.out.println("机器人玩耍中.....");
super.play();
}
}
5
package nvwa;
public class Nvwa {
public static Person getPerson(String people){
Person person=null ;
if(people.equalsIgnoreCase("M")){
person = new Man();
}else if(people.equalsIgnoreCase("W")){
person = new Woman();
}else if(people.equalsIgnoreCase("R")){
person = new Robot();
}
return person;
}
}
6
package nvwa;
public class NvwaMakePeople {
public static void main(String[] args) {
Person Person;
Person = Nvwa.getPerson("M"); //女娲造男人
Person.live();
Person.play();
System.out.println("**********************");
Person = Nvwa.getPerson("W"); //女娲造女人
Person.live();
Person.play();
System.out.println("**********************");
Person = Nvwa.getPerson("R"); //女娲造机器人
Person.live();
Person.play();
}
}
3.注意编程规范。
标签:play,
软件设计,
void,
System,
Person,
live,
public
From: https://www.cnblogs.com/renliqin/p/17811835.html