JAVA咖啡工厂系统

首先创建六个类:Coffee,Coffeefactory,Tea,Blackcoffee,Syore以及用户端Client。

通过子类继承父类的特性进行分工来完成客户的需求。

将Coffee定为Tea和Blackcoffee的父类,使其继承Coffee中的getName方法。

 Coffee:

package jsu.mjj.factory;

public abstract class Coffee{
    public abstract String getName();

}

 

 Client:

package jsu.mjj.factory;

public class client {
    public static void main(String[] args) {
        Store store = new Store();
        Coffee coffee = store.orderCoffee("tea");
        System.out.println(coffee.getName());
        System.out.println(coffee);

    }
}

Store:

package jsu.mjj.factory;

public class Store {
   public Coffee orderCoffee(String type)
   {
       coffeefactory factory = new coffeefactory();
       Coffee coffee = factory.creatCoffee(type);
       return coffee;
   }

}

Coffeefactory: 

package jsu.mjj.factory;

public class coffeefactory {
    public Coffee creatCoffee(String type) {
        Coffee coffee = null;
        if ("black".equals(type)) {
            coffee = new blackcoffe();
        }
        else if ("tea".equals(type)) {
            coffee = new Tea();
        }
        return coffee;
    }
}

Tea:

package jsu.mjj.factory;

public class Tea extends Coffee{
    @Override
    public String getName() {
        return "Tea";
    }
}

Blackcoffee:

package jsu.mjj.factory;

public class blackcoffe extends Coffee {
    @Override
    public String getName() {
        return "blackcoffee";
    }
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值