简体   繁体   English

受保护的为什么不能在不同的包子类中访问?

[英]protected can't access in different package subclass why?

Error: m1() has protected access in A 错误: m1() has protected access in A

When I try to using superclass reference variable ( A obj=new B() ) 当我尝试使用超类引用变量( A obj=new B() )时

This is a first class in package pkg1; 这是pkg1包中的头等舱;

package pkg1;
public class A {
    protected void m1() {
        System.out.println("protected method");
    }
}

This is a second class which is in another package pkg2 importing pkg1; 这是第二类,在另一个软件包pkg2中导入pkg1。

package pkg2;

import pkg1.A;
public class B extends A {
    @Override
    protected void m1() {
        System.out.println("override m1");
    }

    public static void main(String ar[]) {
        A obj=new B();
        obj.m1();
    }
}

I feel like you're still slightly confused with why protected m1() isn't visible. 我觉得您仍然对为什么看不到protected m1()感到有些困惑。

You understand that main is a method in B and B is a subclass of A , therefore you feel like it should be accessible. 您了解mainB的方法,而BA的子类,因此您认为应该可以访问它。

The key is obj is cast to be interpreted of type A . 关键是将obj强制转换为类型A解释。 Then you try to call instance method m1 from static context in B (see this link above for why this is important). 然后,您尝试调用实例方法 m1静态上下文中B (见这个上面为什么这是重要的一环)。 By class A definition, m1 is accessible through: 按照AA定义,可以通过以下方式访问m1

  1. Class definition itself on a given instance of itself. 类定义本身在给定的实例上。
//example 
package pkg1;

public class C {
    protected void m1() {
        System.out.println("protected method");
    }

   public static void callM1(C anInstanceOfC){
        anInstanceOfC.m1();
   }
}
package pkg2;

import pkg1.C;
public class D extends C {
    @Override
    public void m1() {
        System.out.println("override m1");
    }

    public static void main(String ar[]) {
        C obj=new D();
        C.callM1(obj);//output would be "override m1"
    }
}
  1. Instance methods (of course) of the class within itself and of subclasses. (当然)类本身和子类中的实例方法。
//example 
package pkg1;

public class C {
    protected void m1() {
        System.out.println("protected method");
    }

    public void m2() {
        m1();//call to instance method m1
    }

   public static void callM1(C anInstanceOfC){
        anInstanceOfC.m2();
   }
}
package pkg2;

import pkg1.C;
public class D extends C {
    @Override
    public void m1() {
        System.out.println("override m1");
    }

    public void m3() {
        //super.m2()
        m2();//call to superclass method
    }

    public static void main(String ar[]) {
        D obj=new D();
        D.m3(); // output "override m1"
    }
}

protected keyword means that you can access protected-defined data from child classes. protected关键字意味着您可以从子类访问受保护的定义的数据。 In example, you try to access protected data from non-child static context. 例如,您尝试从非子级静态上下文访问受保护的数据。 You should probably try this: 您可能应该尝试这样:

package pkg2;

import pkg1.A;
public class B extends A {
    @Override
    public void m1() {
        System.out.println("override m1");
    }

    public static void main(String ar[]) {
        B obj=new B();
        obj.m1();
    }
}

The access level of m1() is protected. m1()的访问级别受保护。 Protected methods can only be accessed by subclasses or by other classes in the same package . 受保护的方法只能由子类或同一包中的其他类访问。 From your static 'main' method, you can not call a protected instance method of a class in a different package. 静态的 “ main”方法中,您不能在其他程序包中调用类的受保护实例方法。 If class B was in the same package as class A, then you will have no error and your code would work fine. 如果B类与A类位于同一包中,那么您将没有错误,并且代码可以正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当子类位于不同的包中时,为什么我的子类不能访问其超类的受保护变量? - Why can't my subclass access a protected variable of its superclass, when it's in a different package? 子类访问中的受保护方法,它位于不同的包中 - Protected methods in subclass access which is in different package 为什么不同包中的子类无法通过超类实例访问其在另一个包中的超类的受保护字段? - Why a subclass in a different package is unable to access the protected fields of its superclass in another package through superclass instance? 为什么继承类的实例无法访问不同包中的基类的受保护成员 - Why an instance of inherited class can't access to protected member of base class in different package 为什么另一个 package 中的子类无法访问受保护的方法? - Why subclass in another package cannot access a protected method? 为什么protected对于不同的子类包中的其他类变得私有 - why protected becomes private to other classes in different package of subclass 如何在子类中访问超类的“protected static”变量,其中子类位于不同的包中..? - How can ‘protected static’ variable of superclass be accessed in the subclass, where subclass resides in different package..? 无法从不同jar中的同一个包访问超类的受保护成员 - Can't access protected member of superclass from same package in different jar 类无法使用来自不同包的反射来访问其自己的受保护的成员变量 - Class can't access its own protected member variable using reflection from different package Java访问不同包中子类中的受保护成员,使用父类型的对象引用 - Java access to protected member in subclass in different package, using object reference of parent type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM