- package com.home.fdemo1;
- interface Car{
- public void run();
- public void stop();
- }
- class Banz implements Car
- {
- @Override
- public void run() {
- System.out.println("奔驰 汽车已启动!");
- }
- @Override
- public void stop() {
- System.out.println("奔驰汽车已经停止了!");
- }
- }
- class Toyota implements Car{
- @Override
- public void run() {
- System.out.println("Toyota已经开启了");
- }
- @Override
- public void stop() {
- System.out.println("Toyota已经停开了");
- }
- }
- //简单工厂
- class Factory{
- public static Car getInstances(String typeName)
- {
- Car c=null;
- try {
- c=(Car)Class.forName("com.home.fdemo1."+typeName).newInstance();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return c;
- }
- }
- public class Demo1 {
- /**
- * @param args
- */
- public static void main(String[] args) {
- Car c=Factory.getInstances("Toyota3");
- if(c!=null)
- {
- c.run();
- c.stop();
- }else
- {
- System.out.println("工厂不具备造这种车!");
- }
- }
- }
本文出自 “编程哪些事” 博客,请务必保留此出处http://2688160.blog.51cto.com/2678160/485918