public abstract class People {
public abstract void doSth();
}
public class Man extends People{
@Override
public void doSth() {
System.out.println("I'm a man,I'm coding.");
}
}
public class Girl extends People{
@Override
public void doSth() {
System.out.println("I'm a girl,I'm eating.");
}
}
public class PeopleFactory {
public static People getSpecificPeople(String type){
if("A-Man".equals(type)){
return new Man();
}else if("B-Girl".equals(type)){
return new Girl();
}else {
return null;
}
}
}
public class PeopleTestDemo {
public static void main(String[] args) {
People man = PeopleFactory.getSpecificPeople("A-Man");
Objects.requireNonNull(man,"对象不存在.");
man.doSth();
People girl = PeopleFactory.getSpecificPeople("B-Girl");
Objects.requireNonNull(girl,"对象不存在");
girl.doSth();
People foodie = PeopleFactory.getSpecificPeople("Foodie");
Objects.requireNonNull(foodie,"对象不存在");
foodie.doSth();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有