如何真正命名这种"inheritance"?

How to actually name this kind of "inheritance"?

有人告诉我,当您创建继承时,您会继承一个对象 定义 作为父对象。

在 Kotlin 中我可以运行这个代码:

fun main(args : Array<String>) {
    open class aux (val input : Int) {
        fun print() {
            System.out.println(this.input)
        }
    }

    class baz : aux(5)
    class bar : aux(6)

    val x = baz()
    x.print()       // 5

    val y = bar()
    y.print()       // 6
}

在这种情况下,我不能真正判断 baz(或 bar)是从 aux 继承的,因为继承有不同的实现 class,一个在构造函数中占用 5 个,另一个在构造函数中占用 6 个。这使得他们可能以完全不同的方式工作。

我不知道如何命名这段代码的作用,因为对我来说,继承一个实例与我作为程序员所看到的相去甚远。

我想不出任何情况下这段代码会有助于制作更好的软件,当然它很不错,但也很难调试

我不明白你的意思。这不是 Kotlin-specific,你可以在 Java 中做同样的事情:

class Main {
  public static void main(String[] args) {
    Aux bar = new Bar();
    Aux baz = new Baz();
    System.out.println(bar.getValue());
    System.out.println(baz.getValue());
  }
}

class Aux {
    private int value;
    public Aux(int i) {
        value = i;
    }

    public int getValue() {
      return value;
    }
}

class Baz extends Aux {
    public Baz() {
        super(4);
    }
}

class Bar extends Aux {
    public Bar() {
        super(5);
    }
}

打印:

5
4

举一个更具体的例子,你可以有一个基数 class Vehicle,接受一个 int 作为构造函数参数(例如,轮子的数量),你可以有子 class 不需要该值(例如,始终有 4 个轮子的 Car extends Vehicle)。如果这对您的域来说是合理的,您可以将其视为 "giving a meaningful value"。当然,如果您愿意,您可以从调用者那里获取该值:

class Baz extends Aux { 
    public Baz(int value) {
        super(value);
    }
}

总体思路是您需要提供从子构造函数构建父对象所需的所有数据。之后,您需要完成子对象的构建,但如果该对象没有 need/accept 任何其他数据,则您不需要其他参数。