我怎样才能创建一个仅在子 class 内部具有调用访问权限的方法?

How can i make a method with call access only inside the child class?

我想给一个方法一个只有子 classes 和当前 class 可以调用的方法。 例如:

public class Parent{ 
something void mySetterMethod(int input){
     this.something = input;
   }
}

public class Child extends Parent{
     mySetterMethod(otherInput);
}

但这不适用于:

public class SomeClassInSamePackage(){
    public static void main(String[] args){
       Parent parent = new Parent();
       parent.mySetterMethod(input);
}

作为一般建议优先使用组合而不是继承

public class Parent { 
    public void mySetterMethod(int input){
     this.something = input;
   }
}

public class Child {
    private final Parent parent = new Parent();
    ...
        parent.mySetterMethod(otherInput);
    ...
}

你不能从同一个包中调用它。

作为历史记录,Java 1.0 有 private protected,尽管显然它有问题。