`

反射工厂

    博客分类:
  • Java
阅读更多
interface Telphone{
	public void connect() ; 
}
class Phont implements Telphone{
	public void connect() {
		System.out.println("玩手机");
	}
}
class Watch implements Telphone{
	public void connect() {
		System.out.println("看时间");
	}
}
class Factroy{
	public static Telphone getInstance(Class<? extends Telphone> cls){
		try {
			return cls.newInstance() ;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null ; 
	}
}
public class Demo {
	public static void main(String[] args) {
		Telphone tel  = Factroy.getInstance(Watch.class) ; 
		tel.connect();
	}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics