简体   繁体   English

Java:将对象扩展到另一个对象

[英]Java: extending an object to another

For example I have this: 例如我有这个:

class A{
    private int mine = 0; //Some field...
    public A(int a){mine+=a; /*Some operation1...*/}
}


class B extends A{
    private int mine = 0; //Some field...
    public B(int a){mine-=a; /*Some operation2...*/}
}

and I get: 我得到:

error: constructor A in class A cannot be applied to given types;
    public B(int a){}
    required: int
    found: no arguments
    reason: actual and formal argument lists differ in length
    1 errors

I don't understand the error? 我不明白这个错误? What is telling me to do? 什么告诉我做什么?

Though, the code works if Constructor of "A" has no arguments. 但是,如果“A”的构造函数没有参数,则代码可以工作。
But I need to do the Operation1 (aka mine+=a; ), so I need A's arguments, but then I fail. 但我需要做Operation1(又名mine+=a; ),所以我需要A的参数,但后来我失败了。

I'm closed in this magic circle. 我在这个神奇的圈子里关闭了。 What shall I do? 我该怎么办?

The first instruction of every constructor is always to invoke one of its superclass constructor. 每个构造函数的第一条指令总是调用它的一个超类构造函数。 If you don't do it explicitely, the compiler inserts this instraction for you. 如果您没有明确地执行此操作,编译器会为您插入此实例。 The constructor 构造函数

 public B(int a) {
     mine-=a; 
     /*Some operation2...*/
 }

is thus equivalent to 因此相当于

public B(int a) {
    super(); // invoke the no-arg super constructor
    mine-=a; 
    /*Some operation2...*/
}

Since A doesn't have a no-arg constructor, the compilation fails. 由于A没有no-arg构造函数,因此编译失败。 In this case, you must explicitely invoke one of the super constructors: 在这种情况下,您必须明确地调用其中一个超级构造函数:

public B(int a) {
    super(a);
    mine-=a; 
    /*Some operation2...*/
}

Every constructor must call a superclass constructor as the first thing it does. 每个构造函数都必须调用超类构造函数作为它的第一件事。 If you do not do it explicitly via super() , the compiler will implicitly call the no-arts constructor in the superclass. 如果你没有通过super()显式地执行它,编译器将隐式调用超类中的no-arts构造函数。 But in your case, the superclass does not have a no-args constructor, at which point the compiler gives up. 但在你的情况下,超类没有no-args构造函数,此时编译器放弃了。

By the way, it's generally a very bad idea to re-declare a field in a subclass, because it will hide the superclass field, but there will still be two fields. 顺便说一下,在子类中重新声明一个字段通常是一个非常糟糕的主意,因为它会隐藏超类字段,但仍然会有两个字段。

我认为你需要从B构造函数中调用类A的构造函数,如下所示: super(a);

If B extends A, the first operation of the constructor of B is, to call the constructor of A. 如果B扩展A,则B的构造函数的第一个操作是,调用A的构造函数。

If there is no constructor explicitly defined in A, the standard constructor is called. 如果A中没有显式定义构造函数,则调用标准构造函数。 If you don't call the ctor explicitly, it is called implicitly. 如果不明确调用ctor,则会隐式调用它。

But if there is an explicit ctor, which takes arguments, it isn't called automatically - how should it - with which arguments? 但是如果有一个明确的ctor,它接受参数,它就不会被自动调用 - 它应该如何 - 使用哪些参数? You have to call it yourself. 你必须自己打电话。

But your class hierarchie is somewhat misterious. 但你的班级hierarchie有点神秘。 If you want to access mine in the subclass, make it protected or leave it in default state - don't make it private. 如果要在子类中访问我的,请将其保护或保持默认状态 - 不要将其设为私有。

If you make it private, a new introduced attribute with the same name hides the parent attribute, but this is errorprone and irritating. 如果将其设为私有,则具有相同名称的新引入属性会隐藏父属性,但这是错误的并且令人恼火。

In most cases, access, maybe via a method (getter/setter), would be better, or opening the visibility. 在大多数情况下,可能通过方法(getter / setter)访问会更好,或者打开可见性。

Or a different name for the thing in the subclass. 或者子类中的东西的不同名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM