//还可以写在main外面
interface product{
public void getName();
}
class productA implements product {
@Override
public void getName() {
// TODO Auto-generated method stub
System.out.println("++++A++++");
}
}
class productB implements product{
@Override
public void getName() {
// TODO Auto-generated method stub
System.out.println("++++++B++++++");
}
}
class createProduct{
public product create(String type) {
if("A".equals(type))
return new productA();
else if("B".equals(type))
return new productB();
else
return null;
}
}
public class factory {
public static void main(String args[]){
createProduct create = new createProduct();
create.create("A").getName();
create.create("B").getName();
}
}
主类main方法里面可以有不带public的子类 但是不能有接口
接口写在主类的外面 但是不能带public interface product{
public void getName();
}
public class factory {
public static void main(String args[]){
class productA implements product {
@Override
public void getName() {
// TODO Auto-generated method stub
System.out.println("++++A++++");
}
}
class productB implements product{
@Override
public void getName() {
// TODO Auto-generated method stub
System.out.println("++++++B++++++");
}
}
class createProduct{
public product create(String type) {
if("A".equals(type))
return new productA();
else if("B".equals(type))
return new productB();
else
return null;
}
}
createProduct create = new createProduct();
create.create("A").getName();
create.create("B").getName();
}
}