验证中...
Languages: Java
Categories: 编程语言基础
Latest update 2018-12-09 10:09
gistfile1.txt
Raw Copy
interface IFood { // 定义一个食物标准
public void eat() ; // 吃
}
class Bread implements IFood { // 定义一种食物
public void eat() {
System.out.println("吃面包。") ;
}
}
class Milk implements IFood { // 定义一种食物
public void eat() {
System.out.println("喝牛奶。") ;
}
}
class Factory {
public static IFood getInstance(String className) {
if ("bread".equals(className)) {
return new Bread() ;
} else if ("milk".equals(className)) {
return new Milk() ;
} else {
return null ;
}
}
}
public class JavaDemo {
public static void main(String args[]) {
IFood food = Factory.getInstance(args[0]) ;
food.eat() ;// 吃面包
}
}

Comment list( 0 )

You need to Sign in for post a comment

Help Search

Gitee_sixth 5th_float_left_close