我有以下代码:

class Hello {
    class Thing {
        public int size;

        Thing() {
            size = 0;
        }
    }

    public static void main(String[] args) {
        Thing thing1 = new Thing();
        System.out.println("Hello, World!");
    }
}

我知道Thing什么也不做,但是我的Hello, World程序没有它编译也很好。只有我定义的类不及格。

它拒绝编译。在创建一个新Thing的行中,我得到了Hello类型的No封闭实例。我猜是:

我有系统级的问题(在DrJava或我的Java安装)或 我对如何在java中构建一个工作程序有一些基本的误解。

什么好主意吗?


当前回答

让我们用下面的简单例子来理解它。 这是因为它是非静态的内部类。您应该需要外部类的实例。

 public class PQ {

    public static void main(String[] args) {

        // create dog object here
        Dog dog = new PQ().new Dog();
        //OR
        PQ pq = new PQ();
        Dog dog1 = pq.new Dog();
    }

    abstract class Animal {
        abstract void checkup();
    }

    class Dog extends Animal {
        @Override
        void checkup() {
            System.out.println("Dog checkup");

        }
    }

    class Cat extends Animal {
        @Override
        void checkup() {
            System.out.println("Cat Checkup");

        }
    }
}

其他回答

将INNER类Thing声明为静态的,它将毫无问题地工作。

我记得当我将内部类Dog声明为类Dog {only时,我也有同样的问题。我和你有同样的问题。有两种解决方案:

1-将内部类Dog声明为静态。或

2-移动内部类狗自己到一个新的类。

下面是例子:

公共类ReturnDemo {

public static void main(String[] args) {
    
    int z = ReturnDemo.calculate(10, 12);
    System.out.println("z = " + z);
    
    ReturnDemo.Dog dog = new Dog("Bosh", " Doggy");
    System.out.println( dog.getDog());
}


public static int calculate (int x, int y) {
    return x + y;
}

public void print( ) {
    System.out.println("void method");
    return;
}

public String getString() {
    return "Retrun String type value";
}


static class Dog {
    
private String breed;
private String name;

public Dog(String breed, String name) {
    super();
    this.breed = breed;
    this.name = name;
}

public Dog getDog() {
    // return Dog type;
    return this;
    
}

public String toString() {
    return "breed" + breed.concat("name: " + name);
}
}

}

在我的例子中,这是因为多了一个'}'

实际上你不需要将任何东西声明为静态的,但是你必须指定一些外围实例,可以隐式地在同一个类中,显式地在new之前,或者显式地在构造函数中。

class A {

    class B { 
        final A a = A.this;
    }

    class C extends B { }

    final B b = new B();
    final C c = new C();

}

class D extends A.B {

    public D(A a) {
        a.super();
    }

}

class E { 

    final A   a =     new A();
    final A.B b =   a.new B();
    final A.C c = b.a.new C();
    final D   d =     new D(a);

}

让我们用下面的简单例子来理解它。 这是因为它是非静态的内部类。您应该需要外部类的实例。

 public class PQ {

    public static void main(String[] args) {

        // create dog object here
        Dog dog = new PQ().new Dog();
        //OR
        PQ pq = new PQ();
        Dog dog1 = pq.new Dog();
    }

    abstract class Animal {
        abstract void checkup();
    }

    class Dog extends Animal {
        @Override
        void checkup() {
            System.out.println("Dog checkup");

        }
    }

    class Cat extends Animal {
        @Override
        void checkup() {
            System.out.println("Cat Checkup");

        }
    }
}

静态类Thing将使您的程序工作。

实际上,您已经将Thing作为内部类,它(根据定义)与Hello的特定实例相关联(即使它从未使用或引用它),这意味着说new Thing()是一个错误;而没有特定的Hello实例。

如果将其声明为静态类,则它是一个“嵌套”类,不需要特定的Hello实例。