これは正常で、あなたのコードでは、あなたのアナンドクラスはPrimitiveClassのインスタンスに拘束されることを定義する、すなわち、あなたはインスタンスを持つことができますPrimitiveClassのインスタンスからのクラスを返します。それが私たちが内部クラスと呼ぶものです。 コードを動作させるには、anand(およびbabu1)クラスを静的にすることができます。ここ はコードです:
public class PrimitiveCasting {
static class anand {
int a = 90;
void anand1() {
System.out.println("anand is having anand1");
}
}
static class babu extends anand {
int c, b = 88;
void babu1() {
System.out.println("babu is having babu1");
}
}
public static void main(String[] args) {
System.out.println("**********");
anand z1= new anand();
z1.anand1();
babu b1= new babu();
b1.anand1();
b1.babu1();
System.out.println("********");
}
}
それとも、PrimitiveClassのインスタンスを作成し、このようなアナンドまたはバブー、インスタンス化することができます
public class PrimitiveCasting {
class anand {
int a = 90;
void anand1() {
System.out.println("anand is having anand1");
}
}
class babu extends anand {
int c, b = 88;
void babu1() {
System.out.println("babu is having babu1");
}
}
public static void main(String[] args) {
System.out.println("**********");
PrimitiveCasting primitiveCasting = new PrimitiveCasting();
anand z1= primitiveCasting.new anand();
z1.anand1();
babu b1= primitiveCasting.new babu();
b1.anand1();
b1.babu1();
System.out.println("********");
}
}
希望これを助けて!
'anhand'のインスタンスを作成するには、内部クラスを静的にする必要があります。 (「バブ」と同じ)。さもなければ、これらのクラスは、周囲の周辺クラスのインスタンス内にのみ存在できます。 – SomeJavaGuy
私は解決策を得ました。しかし、もし私が内部クラスの前に静的を追加したくなければ、どうすればいいですか? – Anand
同時に私はaとbの値を加えたいと思っています。 – Anand