java中的多重继承

发布于 08-01 18:17 字数 118 浏览 10 评论 0原文

Java 不允许从多个类继承(仍然允许从多个接口继承。),我知道它与经典的钻石问题非常一致。 但我的问题是,为什么在从多个基类继承时没有歧义(因此不会出现菱形问题)时,为什么 java 不允许像 C++ 那样的多重继承?

Java is not allowing inheritance from multiple classes (still it allows inheritance from multiple interfaces.), I know it is very much inline with classic diamond problem. But my questions is why java is not allowing multiple inheritance like C++ when there is no ambiguity (and hence no chances of diamond problem) while inheriting from multiple base class ?

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

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

发布评论

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

评论(9

耀眼的星火2024-08-08 18:17:00

这是 Java 的设计决策。 你永远不会得到它,所以不要太担心。 尽管 MI 可能会帮助您创建 Mixins,但这是 MI 唯一能帮到您的好处。

It was a design decision of Java. You'll never get it, so don't worry too much about it. Although MI might help you make Mixins, that's the only good MI will ever do you.

绳情2024-08-08 18:17:00

我读到大多数程序员没有以正确的方式使用多重继承。 在多重继承的情况下,“直接从类继承只是为了重用代码”并不是最佳实践。

在大多数情况下,许多程序员不知道何时使用简单继承。 如果您想拥有良好的设计,则必须谨慎使用多重继承,并且只有在您知道自己在做什么的情况下才可以使用多重继承。

我不认为 java 中缺乏多重继承(如 c++ 中)会对您的代码/应用程序设计/问题域映射到类中施加限制。

I have read that most programmers don't use multiple inheritance in a proper way. "Just go ahead and inherit from a class just to reuse code" is not the best practice in case of multiple inheritance.

Many programmers do not know when to use simple inheritance in most cases. Multiple inheritance must be used with caution and only if you know what you are doing if you want to have a good design.

I don't think that the lack of multiple inheritance in java (as in c++) will put restrictions in your code / application design / problem domain mapping into classes.

深巷少女2024-08-08 18:17:00

简单。 引用 Tom Sintes

Java 设计团队努力打造 Java:

  • 简单、面向对象且熟悉
  • 稳健且安全
  • 架构中立且可移植
  • 高性能
  • 解释式、线程式和动态

Java语言省略多重继承的原因
大部分源于“简单、面向对象、熟悉”的目标。 作为
作为一种简单的语言,Java 的创建者想要一种最简单的语言
开发人员无需大量培训即可掌握。 为此,他们
致力于使该语言尽可能类似于 C++(熟悉)
不会继承 C++ 不必要的复杂性(简单)。

在设计者看来,多重继承会带来更多问题
和混乱比它解决的。 所以他们切断了多重继承
语言(就像他们减少运算符重载一样)。 设计师们的
丰富的 C++ 经验告诉他们多重继承只是
不值得让人头疼。

Simplicity. To quote Tom Sintes,

The Java design team strove to make Java:

  • Simple, object oriented, and familiar
  • Robust and secure
  • Architecture neutral and portable
  • High performance
  • Interpreted, threaded, and dynamic

The reasons for omitting multiple inheritance from the Java language
mostly stem from the "simple, object oriented, and familiar" goal. As
a simple language, Java's creators wanted a language that most
developers could grasp without extensive training. To that end, they
worked to make the language as similar to C++ as possible (familiar)
without carrying over C++'s unnecessary complexity (simple).

In the designers' opinion, multiple inheritance causes more problems
and confusion than it solves. So they cut multiple inheritance from
the language (just as they cut operator overloading). The designers'
extensive C++ experience taught them that multiple inheritance just
wasn't worth the headache.

痴情2024-08-08 18:17:00

如果java支持多重继承那么它可能会影响java的其他功能
考虑用于调用超类构造函数的 super() 方法。如果程序有多个超类(因为多重继承),那么编译器会混淆应该调用哪个超类构造函数并抛出错误

if java support multiple inheritance then it may effect other features of java
consider super() method which is used to call super class constructor.if program has multiple super class(because of multiple inheritance) then compiler will get confused about which super class constructor should be called and throw an error

不一样的天空2024-08-08 18:17:00

Java 设计者做出了这样的决定。 可以通过使用接口来模拟多重继承。

Java designers decided that. Multiple inheritance can be simulated by the use of interfaces.

风尘浪孓2024-08-08 18:17:00

一个简单的答案是 Java 中的所有类都派生自 java.lang.Object IIRC。 因此,您总是会遇到钻石问题...:-D

One simple answer is that all classes in Java derive from java.lang.Object IIRC. So, you would always have a diamond problem... :-D

伏妖词2024-08-08 18:17:00

确实,Java 并不支持实现的多重继承(仅支持类型,即接口)。 这是一个设计决定。

但是,从 Java 8 开始,它支持使用默认方法的多重继承。 请参阅 http://docs.oracle.com/javase/tutorial/java/ IandI/multipleinheritance.html

多重继承的实现就是能够继承
来自多个类的方法定义。 出现这样的问题
多重继承的类型,例如名称冲突和歧义。
...默认方法引入了一种形式的多重继承
实施。

It is true that Java did not use to support multiple inheritance of implementation (just of type i.e. interface). That was a design decision.

However, since Java 8, it supports multiple inheritance using default methods. See http://docs.oracle.com/javase/tutorial/java/IandI/multipleinheritance.html:

Multiple inheritance of implementation is the ability to inherit
method definitions from multiple classes. Problems arise with this
type of multiple inheritance, such as name conflicts and ambiguity.
... Default methods introduce one form of multiple inheritance of
implementation.

蓦然回首2024-08-08 18:17:00
package org.example;

public class Main {

  public static void main(String[] args) {
    Two_D circle = new Circle(5);
    System.out.println("Area of circle: " + circle.Area());

    Two_D square = new Square(4);
    System.out.println("Area of square: " + square.Area());

    Two_D triangle = new Triangle(3, 4, 5);
    System.out.println("Area of triangle: " + triangle.Area());

    Three_D sphere = new Sphere(3);
    System.out.println("Surface area of sphere: " + sphere.SurfaceArea());
    System.out.println("Volume of sphere: " + sphere.Volume());

    Three_D cube = new Cube(4);
    System.out.println("Surface area of cube: " + cube.SurfaceArea());
    System.out.println("Volume of cube: " + cube.Volume());
  }
}

class Shape {

}

class Two_D extends Shape {

  double Area() {
    return 0;
  }
}

class Three_D extends Shape {

  double SurfaceArea() {
    return 0;
  }

  double Volume() {
    return 0;
  }
}

class Circle extends Two_D {
  double radius;

  Circle(double radius) {
    this.radius = radius;
  }

  @Override
  double Area() {
    return Math.PI * radius * radius;
  }
}

class Square extends Two_D {
  double side;

  Square(double side) {
    this.side = side;
  }

  @Override
  double Area() {
    return side * side;
  }
}

class Triangle extends Two_D {
  double a, b, c;

  Triangle(double a, double b, double c) {
    this.a = a;
    this.b = b;
    this.c = c;
  }

  @Override
  double Area() {

    double s = (a + b + c) / 2;
    return Math.sqrt(s * (s - a) * (s - b) * (s - c));
  }
}

class Sphere extends Three_D {
  double radius;

  Sphere(double radius) {
    this.radius = radius;
  }

  @Override
  double SurfaceArea() {
    return 4 * Math.PI * radius * radius;
  }

  @Override
  double Volume() {
    return (4 / 3) * Math.PI * Math.pow(radius, 3);
  }
}

class Cube extends Three_D {
  double side;

  Cube(double side) {
    this.side = side;
  }

  @Override
  double SurfaceArea() {
    return 6 * side * side;
  }

  @Override
  double Volume() {
    return side * side * side;
  }
}
package org.example;

public class Main {

  public static void main(String[] args) {
    Two_D circle = new Circle(5);
    System.out.println("Area of circle: " + circle.Area());

    Two_D square = new Square(4);
    System.out.println("Area of square: " + square.Area());

    Two_D triangle = new Triangle(3, 4, 5);
    System.out.println("Area of triangle: " + triangle.Area());

    Three_D sphere = new Sphere(3);
    System.out.println("Surface area of sphere: " + sphere.SurfaceArea());
    System.out.println("Volume of sphere: " + sphere.Volume());

    Three_D cube = new Cube(4);
    System.out.println("Surface area of cube: " + cube.SurfaceArea());
    System.out.println("Volume of cube: " + cube.Volume());
  }
}

class Shape {

}

class Two_D extends Shape {

  double Area() {
    return 0;
  }
}

class Three_D extends Shape {

  double SurfaceArea() {
    return 0;
  }

  double Volume() {
    return 0;
  }
}

class Circle extends Two_D {
  double radius;

  Circle(double radius) {
    this.radius = radius;
  }

  @Override
  double Area() {
    return Math.PI * radius * radius;
  }
}

class Square extends Two_D {
  double side;

  Square(double side) {
    this.side = side;
  }

  @Override
  double Area() {
    return side * side;
  }
}

class Triangle extends Two_D {
  double a, b, c;

  Triangle(double a, double b, double c) {
    this.a = a;
    this.b = b;
    this.c = c;
  }

  @Override
  double Area() {

    double s = (a + b + c) / 2;
    return Math.sqrt(s * (s - a) * (s - b) * (s - c));
  }
}

class Sphere extends Three_D {
  double radius;

  Sphere(double radius) {
    this.radius = radius;
  }

  @Override
  double SurfaceArea() {
    return 4 * Math.PI * radius * radius;
  }

  @Override
  double Volume() {
    return (4 / 3) * Math.PI * Math.pow(radius, 3);
  }
}

class Cube extends Three_D {
  double side;

  Cube(double side) {
    this.side = side;
  }

  @Override
  double SurfaceArea() {
    return 6 * side * side;
  }

  @Override
  double Volume() {
    return side * side * side;
  }
}
画离情绘悲伤2024-08-08 18:17:00

当多个父类定义自己的某些实现,并且这两个父类的子类必须处理使用哪个实现的歧义时,就会出现钻石问题。 那么,如果 Java 中的所有类都派生自 Object,那又怎么样,那就是单个父类。 “单父类,多个派生类”与“多父类,单个派生类”不同

The diamond problem arises when multiple parent classes define their own implementations of something and the child class of these two has to deal with the ambiguity of which implementation to use. So what if all classes in Java derive from Object, that's a single parent class. "Single parent, multiple derived classes" is not the same as "Multiple parents, single derived class"

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