1.定义一个工厂f,该工厂可以获取对象


public class FactoryPeople {
    private FactoryPeople(){};

    public static People getPeople(String tyle){

        if (tyle.equalsIgnoreCase("Woman")) {
            return new Woman();
        }else{
            return null;
        }

    }

}

2.定义一个抽象类x,供所有对象继承

public abstract class People {
    public  abstract void eat();
}

3.定义对象类,继承抽象类x

public class Woman extends People {

    @Override
    public void eat() {
        System.out.println("woman 吃饭");
    }

}

测试:

public class Test {
public static void main(String[] args) {
    People woman=FactoryPeople.getPeople("woman");
    woman.eat();
}
}

输出:
woman 吃饭
posted on 2017-04-07 02:53  2637282556  阅读(89)  评论(0编辑  收藏  举报