主类main方法里面可以有不带public的子类 但是不能有接口


1.//还可以写在main外面     
2. interface product{  
3.            public void getName();  
4.            }  
5.     class productA implements product {  
6.            @Override  
7.            public void getName() {  
8.                // TODO Auto-generated method stub  
9.                System.out.println("++++A++++");  
10.            }  
11.              
12.        }  
13.         class productB implements product{  
14.            @Override  
15.            public void getName() {  
16.                // TODO Auto-generated method stub  
17.            System.out.println("++++++B++++++");      
18.            }  
19.        }  
20.         class createProduct{  
21.            public product create(String type) {  
22.                if("A".equals(type))  
23.                    return new productA();  
24.                else if("B".equals(type))  
25.                    return new productB();  
26.                else  
27.                return null;  
28.            }  
29.        }  
30.public class factory {  
31.    public static void main(String args[]){  
32.      
33.        createProduct create = new createProduct();  
34.        create.create("A").getName();  
35.        create.create("B").getName();  
36.          
37.    }  
38.      
39.      
40.}   


主类main方法里面可以有不带public的子类  但是不能有接口

接口写在主类的外面  但是不能带public   interface product{

1.            public void getName();  
2.            }  
3.              
4.public class factory {  
5.    public static void main(String args[]){  
6.         class productA implements product {  
7.                @Override  
8.                public void getName() {  
9.                    // TODO Auto-generated method stub  
10.                    System.out.println("++++A++++");  
11.                }  
12.                  
13.            }  
14.             class productB implements product{  
15.                @Override  
16.                public void getName() {  
17.                    // TODO Auto-generated method stub  
18.                System.out.println("++++++B++++++");      
19.                }  
20.            }  
21.             class createProduct{  
22.                public product create(String type) {  
23.                    if("A".equals(type))  
24.                        return new productA();  
25.                    else if("B".equals(type))  
26.                        return new productB();  
27.                    else  
28.                    return null;  
29.                }  
30.            }  
31.        createProduct create = new createProduct();  
32.        create.create("A").getName();  
33.        create.create("B").getName();  
34.          
35.    }  
36.      
37.      
38.}  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值