为什么protected可以在没有java继承的同一个包中访问? [英] Why protected can be access in same Package without inheritance in java?

查看:344
本文介绍了为什么protected可以在没有java继承的同一个包中访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Modifier        Class     Package   Subclass  World
  public          Y         Y         Y         Y
  protected       Y         Y         Y         N
  no modifier     Y         Y         N         N
  private         Y         N         N         N


  public class a {
  protected int x;
  }

  public class b {
        b() {
              a A=new a();
              A.x=9;//why we can access this field ?
        }
  }

请帮助我了解受保护的具体工作Java

please help my to know the specific work of protected in Java

推荐答案

为什么?因为这就是Java编程语言的设计方式。它没有更多的东西。

Why? Because that's how the Java programming language was designed. There's not much more to it.

可以从


  • 类本身,

  • 同一个包中的类(如果它们是子类,则无关紧要),

  • 子类(如果它们在同一个包中则无关紧要)。

这是与C ++不同,但Java不是C ++,因此它不一定以相同的方式工作。

This is different from C++, but Java is not C++, so it doesn't necessarily work in the same way.

这篇关于为什么protected可以在没有java继承的同一个包中访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆