Java - 扩展问题

发布于 2024-10-06 15:29:21 字数 982 浏览 0 评论 0原文

我在我正在处理的问题集中遇到了一个“扩展”问题,这让我度过了一段痛苦的时光 - 我想我只是遇到了一个障碍,因为它是故意写成令人困惑的。这是我遇到的问题:

class A {
    int x;
    A(int a) {System.out.println(" class A");}
}

class B extends A {
   int x;
   B() {System.out.println(" class B");}
   public static void main (String [] args) {
    A a = new B();
    }
}

当我编译时,我从控制台中踢出了以下错误:

cannot find symbol
symbol  : constructor A()
location: class A
   B() {System.out.println(" class B");}
       ^

并且我应该能够在不触及 A 类的情况下修复 B 类中的此错误。我显然错过了一些东西愚蠢地显而易见,但我已经尝试了一个小时内我能想到的所有内容的排列,但没有任何效果。

到目前为止,我已经尝试过:

  • 在 B() 前面抛出一个 void - 同样的错误。
  • B() 变成 A(int a) - 后者返回一个无效的方法声明错误(在它前面添加一个 void 让我回到原来的“找不到符号”错误)
  • 将 ) - 同样的错误,加上抛出一个额外的“找不到符号”错误,因为我现在已经丢弃了 B() 声明。
  • 使 B 类扩展 A 为 B 类扩展 A(int A) - 抛出九个错误。显然不是答案。
  • 将 A 的类名更改为 Apple 只是为了看看这是否会给我一个答案 - 这要求我在 Apple 类中的 A(int a) 前面添加一个 void,但我不能这样做。

我到底想念什么?这不完全是一门高级课程,所以它不会太复杂,但这让我完全疯了。任何帮助将不胜感激。

I am having a hell of a time with an "extends" issue with a problem in a problem set I'm working on - I think I'm just having a block because it's written to be purposefully confusing. Here's the problem as I was given it:

class A {
    int x;
    A(int a) {System.out.println(" class A");}
}

class B extends A {
   int x;
   B() {System.out.println(" class B");}
   public static void main (String [] args) {
    A a = new B();
    }
}

When I compile, I get the following error kicked out from the console:

cannot find symbol
symbol  : constructor A()
location: class A
   B() {System.out.println(" class B");}
       ^

and I'm supposed to be able to fix this error in class B without touching class A. I'm clearly missing something stupidly obvious, but I've tried permutations of everything I can think of for an hour and nothing's working.

So far, I've tried:

  • throwing a void in front of B() - same error.
  • making B() into A(int a) - the latter comes back with an invalid method declaration error (adding a void in front of it kicks me back to the original "cannot find symbol" error)
  • making B() B(int a) - same error, plus throws an additional "cannot find symbol" error since I've now trashed the B() declaration.
  • making class B extends A into class B extends A(int A) - throws nine errors. Clearly not the answer.
  • Changing the class name of A to Apple just to see if that would give me an answer - this wants me to add a void in front of A(int a) in the Apple class, which I can't do.

What the hell am I missing here? This is not exactly an advanced class so it can't be anything terribly complicated, but this is making me completely insane. Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

像你 2024-10-13 15:29:21

在派生类中,需要调用基类的构造函数。如果您不显式执行此操作,编译器将尝试插入对无参数构造函数的调用 - 如果基类中不存在该调用,则会出现该错误。

解决方案是使用某个值显式调用基类构造函数:

B() {
    super(0);
    System.out.println(" class B");
}

In a derived class, you need to call the base class constructor. If you don't do this explicitly, the compiler will try and insert a call to the no-argument constructor - if none exists in the base class, you get that error.

The solution is to explicitly call the base class constructor with some value:

B() {
    super(0);
    System.out.println(" class B");
}
终遇你 2024-10-13 15:29:21

B 的构造函数中的第一个调用必须是 super(a);
a 是您想要输入到 B 的构造函数中的任何内容。

your first call in B's constructor needs to be super(a);
a being whatever you want to input into B's constructor.

夜访吸血鬼 2024-10-13 15:29:21

1) 当您扩展一个类时,您的子类隐式包含一块数据,其中包含用于创建超类实例的所有数据。您还必须初始化该数据。如果您没有说明它将如何初始化,则假定您需要默认构造函数,即 AA()。但这个构造函数不存在——你只有AA(int)

要执行此初始化,作为 B 构造函数中的第一行,您可以使用如下特殊语法调用 A 构造函数:

B() {
    super(42); 
    // because we are calling the constructor that takes an int, we must supply
    // one. It's up to you to figure out what values should be supplied. Maybe 
    // you wanted to take an int in the B constructor, and pass it along?
    System.out.println(" class B");
}

2) 在类 A 和 中都声明了 int x; B 类。这可能不是您想要的。如前所述,每个 B 实例已经包含一个 int x - 这是它自动获取的一个,因为 B 扩展了 A。

1) When you extend a class, your subclass implicitly contains a chunk of data that consists of all the data used to make an instance of the superclass. You have to initialize that data, too. If you don't say how it will be initialized, it is assumed you want the default constructor, i.e. A.A(). But this constructor does not exist - you only have A.A(int).

To do this initialization, as the first line in the B constructor, you make a call to the A constructor, with special syntax as follows:

B() {
    super(42); 
    // because we are calling the constructor that takes an int, we must supply
    // one. It's up to you to figure out what values should be supplied. Maybe 
    // you wanted to take an int in the B constructor, and pass it along?
    System.out.println(" class B");
}

2) You have an int x; declared in both class A and class B. This is probably not what you want. As noted, each B instance already contains an int x - the one that it automatically gets because B extends A.

拥抱影子 2024-10-13 15:29:21

A 类没有默认构造函数,因为您没有编写默认构造函数(但您确实编写了一个采用 int 参数的构造函数)。

B 类有一个无参数的默认构造函数。它需要调用超类构造函数,但它不能,因为类 A 中没有这样的东西。

Class A does not have a default constructor because you didn't write one (but you did write one that takes an int parameter).

Class B has a no-arg default constructor. It needs to call a superclass constructor, but it can't because there is no such thing in class A.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文