如何实例化抽象类的子类?

时间:2021-06-11 00:07:12

I am stumped at the end of my program. I am making a bank account java program and am having issues integrating an abstract class and a subclass together.

我在节目结束时感到难过。我正在制作一个银行帐户java程序,并且在将抽象类和子类集成在一起时遇到了问题。

public abstract class Account {
    public static void main(String args[]) {
        CheckingAccount Account1 = new CheckingAccount();
    }
    public Account() {
        //
    }
    public class CheckingAccount extends Account {
        //
    }
}

I have tried the CheckingAccount class outside and taking away the public from the class however, then I get the error message that it cannot find the main for the Account class.

我已经在外面尝试了CheckingAccount类,然后从类中删除了公共,然后我收到错误消息,它找不到Account类的main。

The same error comes up as stated above if I add static to the CheckingAccount class.

如果我向CheckingAccount类添加静态,则会出现与上述相同的错误。

3 个解决方案

#1


3  

You can't instantiate an instance class (CheckingAccount) from a static method (main). You have to make the inner class static:

您无法从静态方法(main)实例化实例类(CheckingAccount)。你必须使内部类静态:

public abstract class Account {
    public static void main(String args[]) {
        CheckingAccount Account1 = new CheckingAccount();
    }
    public Account() {
        //
    }
    public static class CheckingAccount extends Account {
        //
    }
}

By the way, this is a quite strange design. I would avoid to:

顺便说一下,这是一个非常奇怪的设计。我会避免:

  • mix your main method with your classes, just make a new separated Account class instead
  • 将您的main方法与您的类混合,只需创建一个新的分隔Account类

  • declare an inner class as an extends of the container class, use two classes (in two different classes) instead.
  • 将内部类声明为容器类的扩展,使用两个类(在两个不同的类中)。

#2


1  

The problem with inner classes is - you should only use it when you absolutely know that you need it. Else you would face such issues.

内部类的问题是 - 只有当你完全知道自己需要它时才应该使用它。否则你会遇到这样的问题。

So what's the issue? Basically an inner class instance always encloses an instance of the enclosing class. So to access them, you need an instance of enclosing class. But, you are trying to instantiate the CheckingAccount class from a static method. And since static method doesn't have a reference to this instance, that way of instantiation is illegal.

那么问题是什么?基本上,内部类实例始终包含封闭类的实例。因此,要访问它们,您需要一个封闭类的实例。但是,您正尝试从静态方法实例化CheckingAccount类。由于静态方法没有对此实例的引用,因此这种实例化方式是非法的。

Problem might go if you change that instantiation to this:

如果您将实例化更改为此问题可能会出现问题:

public static void main(String args[]) {
    CheckingAccount Account1 = new Account().new CheckingAccount();
}

But that wouldn't work either, as Account class is abstract. So, you've should do two immediate changes here:

但这也不会起作用,因为Account类是抽象的。所以,你应该在这里做两个立即的改变:

  1. Move the main() method outside the Account class. Ideally you shouldn't have main() method in your domain classes. Create a separate class for that.
  2. 将main()方法移到Account类之外。理想情况下,您的域类中不应该有main()方法。为此创建一个单独的类。

  3. Move CheckingAccount class outside the Account class. It will be easier for you.
  4. 将CheckingAccount类移到Account类之外。这对你来说会更容易。

#3


0  

You can't have two public classes in one java class file. And, the Java file should be named with the public class in it.

一个java类文件中不能有两个公共类。并且,Java文件应该以其中的公共类命名。

#1


3  

You can't instantiate an instance class (CheckingAccount) from a static method (main). You have to make the inner class static:

您无法从静态方法(main)实例化实例类(CheckingAccount)。你必须使内部类静态:

public abstract class Account {
    public static void main(String args[]) {
        CheckingAccount Account1 = new CheckingAccount();
    }
    public Account() {
        //
    }
    public static class CheckingAccount extends Account {
        //
    }
}

By the way, this is a quite strange design. I would avoid to:

顺便说一下,这是一个非常奇怪的设计。我会避免:

  • mix your main method with your classes, just make a new separated Account class instead
  • 将您的main方法与您的类混合,只需创建一个新的分隔Account类

  • declare an inner class as an extends of the container class, use two classes (in two different classes) instead.
  • 将内部类声明为容器类的扩展,使用两个类(在两个不同的类中)。

#2


1  

The problem with inner classes is - you should only use it when you absolutely know that you need it. Else you would face such issues.

内部类的问题是 - 只有当你完全知道自己需要它时才应该使用它。否则你会遇到这样的问题。

So what's the issue? Basically an inner class instance always encloses an instance of the enclosing class. So to access them, you need an instance of enclosing class. But, you are trying to instantiate the CheckingAccount class from a static method. And since static method doesn't have a reference to this instance, that way of instantiation is illegal.

那么问题是什么?基本上,内部类实例始终包含封闭类的实例。因此,要访问它们,您需要一个封闭类的实例。但是,您正尝试从静态方法实例化CheckingAccount类。由于静态方法没有对此实例的引用,因此这种实例化方式是非法的。

Problem might go if you change that instantiation to this:

如果您将实例化更改为此问题可能会出现问题:

public static void main(String args[]) {
    CheckingAccount Account1 = new Account().new CheckingAccount();
}

But that wouldn't work either, as Account class is abstract. So, you've should do two immediate changes here:

但这也不会起作用,因为Account类是抽象的。所以,你应该在这里做两个立即的改变:

  1. Move the main() method outside the Account class. Ideally you shouldn't have main() method in your domain classes. Create a separate class for that.
  2. 将main()方法移到Account类之外。理想情况下,您的域类中不应该有main()方法。为此创建一个单独的类。

  3. Move CheckingAccount class outside the Account class. It will be easier for you.
  4. 将CheckingAccount类移到Account类之外。这对你来说会更容易。

#3


0  

You can't have two public classes in one java class file. And, the Java file should be named with the public class in it.

一个java类文件中不能有两个公共类。并且,Java文件应该以其中的公共类命名。