package 玩具工厂;
/**
* Your object will be instantiated and called as such:
* ToyFactory tf = new ToyFactory();
* Toy toy = tf.getToy(type);
* toy.talk();
*/
interface Toy {
void talk();
}
class Dog implements Toy {
@Override
public void talk() {
System.out.println("Meow");
}
// Write your code here
}
class Cat implements Toy {
@Override
public void talk() {
System.out.println("Wow");
}
// Write your code here
}
public class ToyFactory {
/**
* @param type a string
* @return Get object of the type
*/
public Toy getToy(String type) {
// Write your code here
if(type.equals("Dog")){
return new Dog();
}
return new Cat();
}
public static void main(String[] args){
}
}
lintcode-->玩具工厂
最新推荐文章于 2021-03-24 23:55:32 发布