Test2.Inn INN1 = test2.newイン(4)を次のようにインポート文を削除することができます。
ので、全体のコードは
public class Test1 {
public static void main(String[] args) {
int sum = 1;
Test2 test2 = new Test2(2);
Test2.Inn inn = test2.new Inn(3);
Test2.Inn inn1 = test2.new Inn(4);
for (int i = 0; i < 9; i++){
sum = (sum + 1) * 2;
}
System.out.println(sum);
}
}
class Test2 {
private int test;
public Test2(int test) {
this.test = test;
}
class Inn{
private int inn;
public Inn(int inn) {
this.inn = inn;
}
}
}
は私があなたに
がTest1のクラス
public class Test1 {
public static void main(String[] args) {
int sum = 1;
Test2 test2 = new Test2(2);
Test2.Inn inn = test2.new Inn(3);
Test2.Inn inn1 = test2.new Inn(4);
for (int i = 0; i < 9; i++){
sum = (sum + 1) * 2;
}
System.out.println(sum);
}
class Test2 {
private int test;
public Test2(int test) {
this.test = test;
}
class Inn{
private int inn;
public Inn(int inn) {
this.inn = inn;
}
}
}
}
のTest2を内部クラスを作成し、他の場合でも、コードウォンをお見せしましょう、ルックスかもしれませんTest1クラスの静的メソッドでTest2クラスにアクセスできないため、まだコンパイルできません。
だから、あなたは今、この
静的クラスTest2を
を行うことができ、あなたはTest2.Inn
public class Test1 {
public static void main(String[] args) {
int sum = 1;
Test2 test2 = new Test2(2);
Test2.Inn inn = test2.new Inn(3);
Test2.Inn inn1 = test2.new Inn(4);
for (int i = 0; i < 9; i++){
sum = (sum + 1) * 2;
}
System.out.println(sum);
}
static class Test2 {
private int test;
public Test2(int test) {
this.test = test;
}
class Inn{
private int inn;
public Inn(int inn) {
this.inn = inn;
}
}
}
}
を宣言することにより、Test1を、その内部クラスインの静的メンバーにTest2クラスにアクセスすることができます
次に、上記のコードは少し醜いので、私は最終的に自分のコードを作ることができます
import Test1.Test2.Inn;
public class Test1 {
public static void main(String[] args) {
int sum = 1;
Test2 test2 = new Test2(2);
Inn inn = new Inn(3);
Inn inn1 = new Inn(4);
for (int i = 0; i < 9; i++){
sum = (sum + 1) * 2;
}
System.out.println(sum);
}
static class Test2 {
private int test;
public Test2(int test) {
this.test = test;
}
static class Inn{
private int inn;
public Inn(int inn) {
this.inn = inn;
}
}
}
}
私はそれがクラスのスコープまたはアクセシビリティに関するすべてだと思います。
内部クラスのメンバーは内部クラスのスコープ内でのみ認識され、外部クラスでは使用できません。 Test2クラス以外のコードがクラスInnをインスタンス化しようとすると、Javaコンパイラによってエラーメッセージが生成されます。一般化すると、ネストされたクラスは、他のプログラム要素と変わることはありません。囲みスコープ内でのみ認識されます。最終的なメソッドを追加したいクラスを記述してください。 – Tehmina
Test2.Inn inn1 = test2.new Inn(4)を使用します。 – tommybee