【问题标题】:Java - Access Modifiers - inheritance of public methods outside packageJava - 访问修饰符 - 包外公共方法的继承
【发布时间】:2017-07-11 17:26:52
【问题描述】:

我是java初学者。

我了解公共方法是全局访问的。在访问修饰符表格矩阵中,给出如下

            Within Class  Within Package  outside Package     outside package
                                            by sub-class         class              
protected     Y                Y               Y                    N

public        Y                Y               Y                    Y

现在我的查询是关于与外部包类相关的公共访问。

假设具有公共方法的公共类 1 在包 1 中,类 2 在包 2 中。

如果我必须从 Class2 访问 Class 1 方法,则必须使用“extends”关键字(据我所知)

因此,在使用“扩展”时,类 2 成为子类,这理想地意味着正在从符合“子类外部包”标准的不同包的子类访问 Class1

那么“子类外包”和“外包类”的条件有什么不同呢?

您可以参考我正在访问不同包的公共和受保护方法的附加语法。请澄清

package second; // different package

public class SubClass extends abnpackage.Play {// abnpackage is another package and Class Play having public & protected
    //methods - prot & pub.

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SubClass SC = new SubClass();
        SC.prot(); // protected method is shown
        SC.pub(); // public method is shown

    }

}

【问题讨论】:

  • 你为什么说“然后必须使用'extends'关键字”来访问一个类的方法?那是你做的吗? "something".length() 呢?我是否必须在该代码示例中使用extends String 才能访问java.lang.String 中声明的public 方法length()
  • 请更新您的进度
  • @alex,谢谢朋友,你的澄清。已经理解了逻辑,可以进一步进行。
  • 不要忘记接受正确的答案,以便其他人可以看到您的问题已解决。

标签: java oop inheritance encapsulation


【解决方案1】:

那么如何条件“子类的外部包”和“外部 包类”有什么不同?

第一个条件的意思是,如果类在包外并且不是继承自目标类,则不能调用protected成员。它应该扩展目标类,以便从它自己的实例中调用protected 成员。

第二种情况允许总是调用public 方法。不管继承是否存在。

例子:

package com.example

public class Super {

    public void pub() {   
    }

    protected void prot() {
    }
}

儿童班:

package com.other

public class Sub extends Super {

    public static void main(String[] args) {
        Sub sub = new Sub();
        sub.pub(); // this is legitimate call 
        sub.prot(); // this is legitimate call 

        Super su = new Super();
        su.pub(); // this is legitimate call 
        su.prot(); // this one will not compile because it's not visible
    } 
}

不是孩子:

package com.other

public class Other {

    public static void main(String[] args) {
        Super su = Super();
        su.pub(); // this is legitimate call 
        su.prot(); // this one will not compile 
    } 
}

将其他类移动到com.example 包(Super 所在的位置):

儿童班:

package com.example

public class Sub extends Super {

    public static void main(String[] args) {
        Sub sub = new Sub();
        sub.pub(); // this is legitimate call 
        sub.prot(); // this is legitimate call 

        Super su = new Super();
        su.pub(); // this is legitimate call 
        su.prot(); // this is legitimate call now
    } 
}

不是孩子:

package com.example

public class Other {

    public static void main(String[] args) {
        Super s = Super();
        s.pub(); // this is legitimate call 
        s.prot(); // this is legitimate call now 
    } 
}

以上示例的一般结论:

  • 如果类在同一个包中,您可以创建目标类的实例(本例中为Super)并从创建的实例中调用该类的public/protected/package-private 成员。

  • 如果类在不同的包中,你可以创建目标类的实例,只调用这个实例中的public成员。但是如果你从目标类继承,后代(Sub)可以调用受保护的方法,但只能从Sub的实例中调用。你仍然没有机会从目标类的实例中调用受保护的方法。

【讨论】:

  • 嗨,朋友,感谢您的快速回复。那么由不同包的类访问的公共方法的语法是什么?也许您也可以使用我的语法?
  • @badri 我扩展了答案
猜你喜欢
  • 2017-08-17
  • 1970-01-01
  • 1970-01-01
  • 2016-01-29
  • 2015-02-09
  • 2013-09-27
  • 2011-04-21
  • 2019-07-15
  • 1970-01-01
相关资源
最近更新 更多