<java>把大象装进冰箱(继承)

按类图模拟把大象(Elephant)装入冰箱(Refriger),按输入实现把动物Animal【大象Elephant或狮子Lion】装进家用电器Electric【冰箱Refriger或微波炉Microwave】

在这里插入图片描述
输入格式:

Put the 动物类名 动物名字 into the 电器类名 电器名字。

例如:

Put the Lion Jack into the Microvate Robert。

输出格式:

create 电器类名 电器名字
create 动物类名 动物名字
电器类名 电器名字 is open 动物类名 动物名字 is in 电器类名 电器名字
电器类名 电器名字 is close

输入样例:
在这里给出一组输入。例如:

Put the Lion Jack into the Microwave Robert

输出样例:
在这里给出相应的输出。例如:

create Microwave Robert
create Lion Jack
Microwave Robert is open Lion
Jack is in Microwave Robert
Microwave Robert is close

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.*;
public class Main {
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        String tmp=input.nextLine();
        String regex="Put\\s+the\\s+(\\w+)\\s+(\\w+)\\s+into\\s+the\\s+(\\w+)\\s+(\\w+)";
        Pattern pattern=Pattern.compile(regex);
        Matcher matcher=pattern.matcher(tmp);
        if(matcher.find())
        {
            String animalclass=matcher.group(1);//动物类名
            String animalName=matcher.group(2);//动物名字
            String electricclass=matcher.group(3);//电器类名
            String electricName = matcher.group(4);//电器名字
            Electric electric;
            if(electricclass.equals("Refriger"))
            {
                electric = new Refriger(electricName);
            }
            else
                electric=new Microwave(electricName);
            Animal animal;
            if(animalclass.equals("Elephant"))
            {
                animal = new Elephant(animalName);
            }
            else
                animal=new Lion(animalName);
            electric.openDoor();
            animal.enterElectric(electric);
            electric.closeDoor();
        }
    }
}
class Refriger extends Electric {
    Refriger(String electricName){
        super(electricName);
        super.setElectricclass("Refriger");
        System.out.println("create Refriger "+super.getElectricName());
    }
    Refriger(){
    }
}
class Microwave extends Electric {
    Microwave(String electricName){
        super(electricName);
        super.setElectricclass("Microwave");
        System.out.println("create Microwave "+super.getElectricName());
    }
    Microwave(){
    }
}
class Electric{
    private String electricName;
    private String electricclass;
    Electric(String electricName)
    {
        this.electricName=electricName;
    }
    Electric(){
    }
    public String getElectricName() {
        return electricName;
    }
    public void setElectricclass(String electricclass) {
        this.electricclass = electricclass;
    }
    public String getElectricclass() {
        return electricclass;
    }
    public void openDoor()
    {
        System.out.println(electricclass+" "+electricName+" is open");
    }
    public void closeDoor()
    {
        System.out.println(electricclass+" "+electricName+" is close");
    }
}
class Animal{
    Electric electric;
    private String animalName;
    private String animalclass;
    Animal(){
    }
    Animal(String animalName){
        this.animalName=animalName;
    }
    public void setAnimalclass(String animalclass) {
        this.animalclass = animalclass;
    }
    public String getAnimalclass() {
        return animalclass;
    }
    public String getAnimalName() {
        return animalName;
    }
    public void enterElectric(Electric electric){
        System.out.println(animalclass+" "+animalName+" is in "+electric.getElectricclass()+" "+electric.getElectricName());
    }
}
class Elephant extends Animal{
    Elephant(String animalName){
        super(animalName);
        super.setAnimalclass("Elephant");
        System.out.println("create Elephant "+super.getAnimalName());
    }
    Elephant(){
    }
}
class Lion extends Animal{
    Lion(String animalName){
        super(animalName);
        super.setAnimalclass("Lion");
        System.out.println("create Lion "+super.getAnimalName());
    }
    Lion(){
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值