简体   繁体   English

对多个 inheritance 感到困惑

[英]Confused about multiple inheritance

I just read some where that C# class can't inherit from multiple class, at the same time I also read that each C# class is inherited from a base class "Object Class".我刚刚读到一些 C# class 不能从多个 class 继承的地方,同时我还读到每个 C# class 是从基 class“对象类”继承的。

Now I am confused if I make another class make it to inherit some class, then it is inheriting from the class and the base class, ie two class.现在我很困惑,如果我让另一个 class 让它继承一些 class,那么它是从 class 和基数 class 继承的,即两个 class。

Isn't it breaking the law?这不是违法吗?

Each class inherits from one other class. 每个类都继承自另一个类。 If you make your own class extend one of your other classes, then it will not directly inherit from Object but rather from your superclass. 如果你让自己的类扩展其他类之一,那么它不会直接从Object继承而是从你的超类继承。 The superclass in its turn will inherit from Object . 反过来的超类将继承自Object

Your subclass will have all the methods from Object available trough the superclass. 您的子类将通过超类使用Object所有方法。

This will illustrate it: 这将说明它:

void Main()
{
    Console.WriteLine (new Super().GetType().BaseType);
    Console.WriteLine (new Sub().GetType().BaseType);
}

class Super { }
class Sub : Super { }

Output: 输出:

在此输入图像描述

This is possible in C#: 这在C#中是可能的:

在此输入图像描述

(One base class per derived class, aka "single inheritance ") (每个派生类一个基类,又名“单继承 ”)

This is not possible in C#: 这在C#中是不可能的:

在此输入图像描述

( Multiple inheritance , use interfaces instead in C#) 多重继承 ,在C#中使用接口

I just read some where that C# class can't inherit from multiple class, at the same time I also read that each C# class is inherited from a base class "Object. Now I am confused. 我只是读了一些C#类不能从多个类继承的地方,同时我还读到每个C#类都是从基类“Object”继承的。现在我很困惑。

Every C# class except object inherits directly from exactly one other class. 除了object之外的每个C#类都直接从其他一个类继承。 Object inherits from no class. 对象从没有类继承。

AC# class can inherit indirectly from any (non-negative!) number of classes. AC#类可以间接地从任何(非负数!)类继承。

class Animal : Object {}
class Giraffe : Animal {}

Are our rules met? 我们的规则符合吗? Yes. 是。 Object inherits from no class. 对象从没有类继承。 Animal inherits directly from Object and indirectly from no class. Animal直接从Object继承,间接从没有类继承。 Giraffe inherits directly from Animal and indirectly from Object. 长颈鹿直接从动物身上继承,间接从遗传身上继承。

This eliminates the contradiction. 这消除了矛盾。

Every class, except Object itself, will implictly extend Object if it does not extend another class. 除了Object本身之外,每个类如果不扩展另一个类,将隐含地扩展Object。 Thus all classes implictly always eventually extend from Object. 因此,所有类最终总是从Object扩展。

The only common programming language supporting multiple inheritance is C++, it's explictly not allowed in most languages for good reason. 支持多重继承的唯一通用编程语言是C ++,但在大多数语言中显然不允许这样做。

One class can inherit from one other class directly and implement multiple interfaces. 一个类可以直接从另一个类继承并实现多个接口。
You can chain inheritance, eg class a inherits from class b while class c inherits from class a. 你可以链接继承,例如,类a继承自类b,而类c继承自类a。

interface i {}
class a {}
class b : a {}   // OK
class c : i,b {} // OK
class d : a,b {} // Not OK

Multiple inheritance means that a class' inheritance tree can be branched as desired (ie it can inherit from more than one base classes at the same time). 多重继承意味着可以根据需要对类的继承树进行分支(即,它可以同时从多个基类继承)。 That's (thank god) not possible in C#. 那是(感谢上帝)C#中不可能的。

But as long as the inheritance tree has always only one 'upper leg', it can be as deep as required (ie there is something like an inheritance 'chain', where one class inherits from the other). 但只要继承树总是只有一个“大腿”,它就可以像需要的那样深(即有一个类似继承'链',其中一个类继承另一个)。

It does not break the rule. 它没有违反规则。

Every class in C# will inherit from exactly one class. C#中的每个类都将从一个类继承。 That class either is any class the author of that class wants to extend, or, if none specified, it will extend the Object class. 该类要么是该类的作者想要扩展的任何类,要么如果没有指定,它将扩展Object类。

public class MyClass {} // extends from default Object class

would be exactly the same as 会完全一样的

public class MyClass : object {}

Or, when the author wants to extend a specified class: 或者,当作者想要扩展指定的类时:

public class MyClass : AnyBaseClass {}
  1. What is Is-A relationship in Java? Java 中的 Is-A 关系是什么?

Ans: Is-A relationship represents Inheritance. Ans: Is-A 关系代表 Inheritance。 It is implemented using the “extends” keyword.它是使用“extends”关键字实现的。 It is used for code reusability.它用于代码可重用性。

  1. What is super class and subclass?什么是超级 class 和子类?

Ans: A class from where a subclass inherits features is called superclass. Ans: 子类继承特征的 class 称为超类。 It is also called base class or parent class.它也称为基础 class 或父 class。

A class that inherits all the members (fields, method, and nested classes) from other class is called subclass.从其他 class 继承所有成员(字段、方法和嵌套类)的 class 称为子类。 It is also called a derived class, child class, or extended class.它也称为派生 class、子 class 或扩展 class。

  1. How is Inheritance implemented/achieved in Java? Inheritance 在 Java 中是如何实现/实现的?

Ans: Inheritance can be implemented or achieved by using two keywords: Ans: Inheritance 可以通过使用两个关键字来实现或实现:

extends: extends is a keyword that is used for de extends:extends 是用于 de 的关键字

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

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