zoukankan      html  css  js  c++  java
  • 项目三(2)——简单工厂模式

    请采用采用简单工厂设计模式,为某个汽车销售店设计汽车销售系统,接口car至少有方法print(),三个汽车类:宝马、奥迪、大众 (属性:品牌,价格),在测试类中根据客户要求购买的汽车品牌,通过接口car为客户提供相应的汽车对象。

    package pro4;
    
    public class Car01 {
    	public static void main(String[] args) { 
    		Car c1=null;
    		c1=Factory.getInstance("Audi");
    		if(c1!=null){
    			c1.print();
    		}
    	}
    }
    interface Car{
    	public void print();
    }
    class Factory{
    	public static Car getInstance(String name){
    		Car c1=null;
    		if("BMW".equals(name)){
    			c1=new BMW("BMW 740Li",1200000);
    		}
    		if("Audi".equals(name)){
    			c1=new Audi("Audi A8",1000000);
    		}
    		if("VW".equals(name)){
    			c1=new VW("Volkswagen Pharton",1888888);
    		}
    		return c1;
    	}
    }
    class BMW implements Car{
    	private String name;
    	private double money;
    	public BMW(String name,double money){
    		this.setName(name);
    		this.setMoney(money);
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public double getMoney() {
    		return money;
    	}
    	public void setMoney(double money) {
    		this.money = money;
    	}
    	public void print() {
    		System.out.println("BMW--"+name+"的价格为"+money+"¥");
    	}	
    }
    class Audi implements Car{
    	private String name;
    	private double money;
    	public Audi(String name,double money){
    		this.setName(name);
    		this.setMoney(money);
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public double getMoney() {
    		return money;
    	}
    	public void setMoney(double money) {
    		this.money = money;
    	}
    	public void print() {
    		System.out.println("Audi--"+name+"的价格为"+money+"¥");
    	}	
    }
    class VW implements Car{
    	private String name;
    	public VW(String name,double money){
    		this.setName(name);
    		this.setMoney(money);
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public double getMoney() {
    		return money;
    	}
    	public void setMoney(double money) {
    		this.money = money;
    	}
    	private double money;
    	public void print() {
    		System.out.println("VW--"+name+"的价格为"+money+"¥");
    	}	
    }
    

      

  • 相关阅读:
    mybatis公用代码抽取到单独的mapper.xml文件
    mysql与oracle常用函数及数据类型对比00持续补充
    人民币在岸 离岸 中间价的含义与关系
    mysql hang and srv_error_monitor_thread using 100% cpu(已解决)
    long和BigDecimal引发的管理思考
    mybatis 3的TypeHandler深入解析(及null值的处理)
    mysql 5.7.17发布
    rabbitmq connection/channel/consumer/queue的数量关系详细分析
    rabbitMQ publish丢包分析
    INFO: task java:27465 blocked for more than 120 seconds不一定是cache太大的问题
  • 原文地址:https://www.cnblogs.com/hxtblogs/p/7649569.html
Copyright © 2011-2022 走看看