Java中的类继承问题,带和不带参数的构造函数 [英] Class inheritance problem in Java, constructors with and without params

查看:193
本文介绍了Java中的类继承问题,带和不带参数的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在学习Java(第二年的IT学生),我有一个小问题。具有继承精确。代码如下:

I'm learning Java (2nd year IT student) and I'm having a little problem. With inheritance to be precise. Here's the code:

class Bazowa
    {
    public Bazowa(int i)
        {
        System.out.println("konstruktor bazowy 1");
        }

    public Bazowa(int j, int k)
        {
        System.out.println("konstruktor bazowy 2");
        }
    }

class Pochodna extends Bazowa
    {
    /* Pochodna()
        {
        System.out.println("konstruktor pochodny bez parametru");
        } */
    Pochodna(int i)
        {
        super(i);
        System.out.println("konstruktor pochodny z parametrem");
        }
    }


$ b $ p Pochodna类扩展了Bazowa类,我的练习是做一个超类只有构造函数的参数和一个子类有两种类型(有和没有)。

So, the Pochodna class extends the Bazowa class, and my exercise is to make a superclass that has only constructors with parameters and a subclass that has both types (with and without).

当我评论第一个构造函数在Pochodna类,一切工作正常,但我真的不知道如何使它工作没有评论的那部分。我想我不得不从第一个调用构造函数,但是没有想法如何做。

When I comment the first constructor in Pochodna class, everything works fine, but I don't really know how to make it work without commenting that part. I guess that I have to somehow call the constructor from the first one, but don't have an idea how to do that.

任何帮助将不胜感激,
Paul

Any help would be appreciated, Paul

推荐答案

您的第一个构造函数来自 Pochodna code> super(),你在 Bazowa 中没有的构造函数。

Your first constructor from Pochodna calls by default super(), a constructor which you do not have in Bazowa.

您应该在 Pochodna()中用1或2个参数调用其中一个基础构造函数,在你的基类中没有参数。

You should either call one of the base constructors with 1 or 2 params in Pochodna(), or create a constructor with no parameters in your base class.

编辑:因为你说你在学习Java,我会在我的答案中添加一些额外的解释。

Since you said you are learning Java, I will add some extra explanations to my answer.

每个类都必须有一个构造函数,所以当你不明确声明一个时,编译器会为你创建一个没有参数的默认构造函数。如果您明确声明构造函数,则不会添加此构造函数。

Every class must have a constructor, so when you do not declare one explicitly, the compiler does so for you, creating a default constructor with no parameters. This constructor won’t be added if YOU declare constructors explicitly.

在继承中,子类是父类的特殊化。这意味着子类包含父类的属性和行为并在它们上延伸。但是你不要再次声明父元素(除非你真的想覆盖东西)。所以,当你创建一个子实例,不知何故从父的元素也必须初始化。为此,你有 super(...)构造。

In inheritance, the child class is a "specialization" of the parent. That means that the child class contains the attributes and behavior of the parent class and extends on them. But you do not declare the parent elements again (unless you really want to overwrite stuff). So, when you create an instance of the child, somehow the elements taken from the parent must also be initialized. For this you have the super(...) construct.

孩子的第一件事构造函数是对 super(...)的调用,使得从父代中获取的元素在子代尝试对其执行某些操作之前已正确初始化(您也可以调用另一个子构造函数 this(...) - 在这种情况下,调用链中的最后一个子构造函数将调用 super )

The first thing that must be in a child constructor is a call to super(...) so that the elements taken from the parent are properly initialized before the child tries to do something with them (you can also have a call to another of child’s constructor this(...) – in this case, the last child constructor in the calling chain will call super(...) ).

因此,编译器将再次添加一个默认调用 super()

Because of this, the compiler will again add a default call to super() – with no parameters – for you when you do not do so yourself in the child.

Pochodna的第一个构造函数中,code>因为你没有调用 super(i) super(j,k)自己,默认情况下会调用 super()。但是在父类中,你显式指定了构造函数,因此默认不是由编译器创建的。从这里的异常,你最终调用一个不存在的构造函数。

In the first constructor of Pochodna, since you did not call super(i) or super(j, k) yourself, a call to super() was placed by default. But in the parent class you explicitly specified constructors, so the default was not created by the compiler. And from here the exception, you end up calling a constructor that does not exist.

希望这使得更容易学习继承。干杯。

Hope this makes it easier to learn Inheritance. Cheers.

这篇关于Java中的类继承问题,带和不带参数的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆