被误解的protected

父类:

1 None.gif package  com.quitgame.common;
2 None.gif
3 ExpandedBlockStart.gifContractedBlock.gif public   class  Parent  dot.gif {    
4InBlock.gif    protected int i;
5ExpandedBlockEnd.gif}

同包子类

 1 None.gif package  com.quitgame.common;
 2 None.gif
 3 ExpandedBlockStart.gifContractedBlock.gif public   class  Son  extends  Parent dot.gif {
 4InBlock.gif
 5ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
 6InBlock.gif     * @param args
 7ExpandedSubBlockEnd.gif     */

 8ExpandedSubBlockStart.gifContractedSubBlock.gif    public static void main(String[] args) dot.gif{
 9InBlock.gif        Son s = new Son();
10InBlock.gif        s.i += 7//OK,in the son class.
11InBlock.gif        
12InBlock.gif        Parent p = new Parent();
13InBlock.gif        p.i ++//OK,in the same package.
14ExpandedSubBlockEnd.gif    }

15InBlock.gif    
16ExpandedSubBlockStart.gifContractedSubBlock.gif    public void testProtected()dot.gif{
17InBlock.gif        this.i ++//OK,in the son class.
18ExpandedSubBlockEnd.gif    }

19ExpandedBlockEnd.gif}

同包非子类

 1 None.gif package  com.quitgame.common;
 2 None.gif
 3 ExpandedBlockStart.gifContractedBlock.gif public   class  ClassInSamePackage  dot.gif {
 4ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
 5InBlock.gif     * @param args
 6ExpandedSubBlockEnd.gif     */
 7ExpandedSubBlockStart.gifContractedSubBlock.gif    public static void main(String[] args) dot.gif{
 8InBlock.gif        Son c = new Son();
 9InBlock.gif        c.i ++;   //OK, in the same package .
10ExpandedSubBlockEnd.gif    }

11ExpandedBlockEnd.gif}

非同包子类

 1 None.gif package  test;
 2 None.gif
 3 None.gif import  com.quitgame.common.Parent;
 4 None.gif
 5 ExpandedBlockStart.gifContractedBlock.gif public   class  SonInAnotherPackage  extends  Parent dot.gif {
 6InBlock.gif
 7ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
 8InBlock.gif     * @param args
 9ExpandedSubBlockEnd.gif     */

10ExpandedSubBlockStart.gifContractedSubBlock.gif    public static void main(String[] args) dot.gif{        
11InBlock.gif        Parent p = new Parent();
12InBlock.gif        //p.i ++; //error , not in the same package , not access througth this son class.
13ExpandedSubBlockEnd.gif    }

14InBlock.gif    
15ExpandedSubBlockStart.gifContractedSubBlock.gif    public void test()dot.gif{
16InBlock.gif        super.i ++//OK,in the son class.
17InBlock.gif        this.i ++;  //OK,in the son class.
18ExpandedSubBlockEnd.gif    }

19InBlock.gif
20ExpandedBlockEnd.gif}

21 None.gif

public > protected > package > private

protected 除了可以在子类中访问,还可以在同包中访问。
这次IT大比武,栽在这个修饰符上了。

转载于:https://www.cnblogs.com/QuitGame/archive/2007/06/03/769247.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值