6

これについて議論したいのですが、私の場合の答えを推測できませんでした。まだ助けが必要です。

これが私のコードです:

package JustRandomPackage;

public class YetAnotherClass{
    protected int variable = 5;
}
package FirstChapter;

import JustRandomPackage.*;

public class ATypeNameProgram extends YetAnotherClass{
    public static void main(String[] args) {

        YetAnotherClass bill = new YetAnotherClass();
        System.out.println(bill.variable); // error: YetAnotherClass.variable is not visible

    }
}

上記の例は紛らわしいようです。

 1. Subclass is a class that extends another class.
 2. Class members declared as protected can be accessed from 
    the classes in the same package as well as classes in other packages 
    that are subclasses of the declaring class.

質問:int variable = 5サブクラスYetAnotherClassインスタンス (billオブジェクト)からプロテクト メンバー ( ) にアクセスできないのはなぜですか?

4

5 に答える 5

4

宣言クラスのサブクラスである他のパッケージ内のクラスは、独自の継承されたprotectedメンバーにのみアクセスできます。

package FirstChapter;

import JustRandomPackage.*;

public class ATypeNameProgram extends YetAnotherClass{
    public ATypeNameProgram() {
        System.out.println(this.variable); // this.variable is visible
    }
}

...しかし、他のオブジェクトの継承されたprotectedメンバーではありません。

package FirstChapter;

import JustRandomPackage.*;

public class ATypeNameProgram extends YetAnotherClass{
    public ATypeNameProgram() {
        System.out.println(this.variable); // this.variable is visible
    }

    public boolean equals(ATypeNameProgram other) {
        return this.variable == other.variable; // error: YetAnotherClass.variable is not visible
    }
}
于 2013-06-12T05:31:16.273 に答える
2

bill は、サブクラス化された YetAnotherClass の一部ではありません。bill は別の YetAnotherClass です。

int bill = this.variable;(コンストラクター内で) サブクラスのメンバーにアクセスしてみてください。

于 2013-06-12T05:30:18.367 に答える
1

YetAnotherClassと同じパッケージにある場合、コードは機能しATypeNameProgramます。他の人が書いたように、他の場合には機能しません。これが実際の例です。

package my.example;

public class MainClass extends MyAnotherClass {
    public static void main(String[] args) {
        MyAnotherClass bill = new MyAnotherClass();
        System.out.println(bill.value); // this will work
    }
}

package my.example;

public class MyAnotherClass {

    protected int value = 5;

}
于 2013-06-12T05:42:38.077 に答える
1

クラスは、が に割り当て可能である場合にのみFoo、 タイプの保護されたインスタンス メンバーにのみアクセスできます。つまり、次のように書ける場合:BarBarFoo

Foo foo = new Bar(); 

たとえば、次のようにします。

package a;

public class Base {
    protected int protectedField;
}

次に、これを取得できます。

package b;

import a.Base;

public class Parent extends Base {
    void foo() {
        int i = this.protectedField;
    }
    void foo(Parent p) {
        int i = p.protectedField;
    }
    void foo(Child c) {
        int i = c.protectedField;
    }
}

class Child extends Parent { }

protectedFieldすべての は のインスタンスを介してアクセスされるため、これはコンパイルされますParentParent参照はChildインスタンスにできる (つまり、 と書けるParent p = new Child();) ため、 にアクセスできることに注意してくださいc.protectedField

以下はコンパイルされません。

package b;

import a.Base;

public class Parent extends Base {
    void foo(Stepchild sc) {
        int i = sc.protectedField; // ERROR
    }
}

class Stepchild extends Base {}

のインスタンスは のインスタンスでStepchildはないためですParent

少し紛らわしいことに、これもコンパイルされません。

package b;

import a.Base;

public class Parent extends Base {}

class Child extends Parent {
    void foo(Parent p) {
        p.protectedField; // ERROR
    }
}

これは、Parentオブジェクトが のスーパークラスまたはスーパーインターフェースではないため、その保護されたメンバーにアクセスできないChildためです。Child

思い出せない場合は、クラスの型の参照に型を書き込めるかどうかを考えてみてください。たとえば、次のように記述できます。

Parent p = new Child();

しかし、書くことはできません

Child c = new Parent();     // ERROR
Parent p = new Stepchild(); // ERROR

そのため、 の保護されたメンバーへのアクセス権はなく、 の保護されたメンバーへのChildアクセス権もありません。ParentParentStepchild

いくつかの最終的なポイント:

protectedアクセスにより、パッケージ間の可視性が可能になることに注意してください。私の経験では、人々はこれを忘れています。

最後に、protected staticメンバーは継承階層内で常に表示されます。

于 2015-06-22T03:49:23.140 に答える
0

それを拡張するクラスのインスタンスを作成するのではなく、親クラスのインスタンスを作成しています。以下のコードを確認してください。

public class ATypeNameProgram extends YetAnotherClass{
    public static void main(String[] args) {

        YetAnotherClass bill = new YetAnotherClass();
        System.out.println(bill.variable); // error: YetAnotherClass.variable is not visible

        ATypeNameProgram a = new ATypeNameProgram();
        System.out.println(a.variable); //this will work

    }
}
于 2013-06-12T05:33:41.237 に答える