简体   繁体   English

C#继承(入门)

[英]C# inheritance (beginner)

I have something which should be easy to answer for most of your I think: 对于您我认为的大多数人,我都有一些应该易于回答的问题:

I have the following classes: 我有以下课程:

class One
{
string first;
}
class Two : One
{
string second;
}

Now I wanted to replace all One values of a Two value. 现在,我想将所有“一”值替换为“二”值。 So I tried the following: 所以我尝试了以下方法:

One one = new One();
Two two = new Two();
two = (Two) one; // <= this seems to not work

So do I really have to implement a Method that copys all members of one to two? 那么,我真的必须实现一个将一个成员的所有成员复制到两个成员的方法吗?

One doesn't inherit from Two , and that is what is not working great. One不会从Two继承而来,那是行不通的。

Class inheritance doesn't mean to hide or to replace one class's property value, but that the derived class is a specialization of the base class it inherits from. 类继承并不意味着隐藏或替换一个类的属性值,而是派生类是对其继承的基类的专门化。

For example: 例如:

public class Cat {
}

public class Dog {
}

What do these two have in common? 这两个有什么共同点?

  1. They have four legs; 他们有四只脚;
  2. They are all animals; 他们都是动物。
  3. They have hairs; 他们有头发。

What do they not have in common? 他们没有什么共同点?

  1. A cat meows; 猫叫声;猫叫声
  2. A dog barkles; 狗叫;

Let's revise our model by setting this in order. 让我们通过设置顺序来修改我们的模型。

public class Cat {
    public bool HasHair { get { return true; } }
    public int Legs { get { return 4; } }
    public string Speaks { get { return "Meows"; } }
}

public class Dog { 
    public bool HasHair { get {return true; } }
    public int Legs { get { return 4; } }
    public string Speaks { get { return "Barkles"; } }
}

Now, to save you time and coding, what could we do? 现在,为了节省您的时间和编码,我们该怎么办? Generalize what both classes have in common? 归纳两个类的共同点? Alright! 好的! But how to make it so!? 但是如何做到这一点呢?

public class Animal {
    public bool HasHair { get { return true; } }
    public int Legs { get { return 4; } }
    public virtual string Speaks { get { return "Does a sound"; } }        
}

// We can now inherit from Animal to write our Cat and Dog classes.
public class Cat : Animal {
    public overrides string Speaks { get { return "Meows"; } }
}

public class Dog : Animal { 
    public overrides string Speaks { get { return "Barkles"; } }
}

And you can do: 您可以执行以下操作:

Dog dog = new Dog();
dog.Legs; // Because Legs is an Animal property, and a Dog inherits from Animal, then Dog has a property called Legs, since it got it from his base class.

Now, we can do: 现在,我们可以做:

Animal pet = (Animal)(new Cat());

That said, you can typecast a Cat to an Animal , because it is an Animal ! 就是说,您可以将Cat铸成Animal ,因为它 Animal Now, you have to consider that your typecasted Cat will "do a sound", instead of "meowling", since by typecasting Cat to Animal , you're saying that you want to work with any Animal , as long as it is one. 现在,您必须考虑到将Cat转换为“声音”,而不是“嘶嘶作响”,因为通过将Cat转换为Animal ,就是说您想与任何Animal ,只要它是Animal So both Cat and Dog are animals. 因此,无论CatDog是动物。

We could push our example even further by saying that not every animal has four legs, some doesn't have any, and others have only two. 我们可以说不是每个动物都有四条腿,有些没有任何一条,而其他只有两条,这可以进一步推动我们的例子。 Then, we would have to generalize and to specialize accordingly. 然后,我们将不得不概括和专门化。

In short: Yes 简而言之:是的

Like what was already said, One doesn't inherit from Two, so there's no "logical" default action to take here. 就像已经说过的那样,“一个”不继承自“两个”,因此这里没有“逻辑”默认操作。 You should look into Conversion Operators . 您应该研究转换运算符 This'll let you still use the two=(Two) one syntax after defining an operator, like this(in the class def): 这将使您在定义运算符后仍使用two =(Two)一种语法,如下所示(在def类中):

public static explicit operator Two(One o)
    {
        var t=new Two();
        t.first=o.first;
        t.second="default";//or whatever kind of default value you want
        return t;
    }

You can't cast a One as a Two because One does not inherit from Two . 您不能将“一One为“ Two因为“ One ”不继承自“ Two The other way around would work fine though ( Two two = new One(); ) 但是,另一种方法可以正常工作( Two two = new One();

In order to get a Two from a One you will have to create a new Two . 为了从One获得Two ,您必须创建一个新的Two You could have a method that copies all members of One to Two or you could have a constructor for Two that takes a One and sets the properties from there. 您可以有一个将“ OneTwo所有成员复制的方法,也可以有一个为“ Two的构造器,该构造器采用“一One并从此处设置属性。

Yes, you have to implement a method that copies all members of one to two . 是的,你必须实现的方法是复制的所有成员onetwo Normally, you would make a constructor: 通常,您将创建一个构造函数:

public Two(One one) {
    this.first = one.first;
}

// Elsewhere ...
Two two = new Two(one);

Or if you want to copy over the values of an existing instance, you might do it with an instance method: 或者,如果你想现有的情况下的值复制,则可能是一个实例方法做到这一点:

public void CopyValuesFrom(One one) {
    this.first = one.first;
}

// Elsewhere ...
Two two = new Two();
two.CopyValuesFrom(one);

It is important to remember that one and two are just pointers in memory to an instance of an object. 重要的是要记住,一个和两个只是内存中指向对象实例的指针。

If we were to write this out in plain English it would read something like this: 如果我们用通俗易懂的英语写出来,它会显示如下内容:

One one = new One();

Create a pointer in memory that refers to an object of type One and label it one , Then create an object of type One and place it at the memory location indicated by one . 在内存中创建一个指向类型为One的对象的指针并将其标记为1 ,然后创建类型为One的对象并将其放置在由one指示的内存位置。

two = (Two) one;

Replace the object referenced by two with the object referenced by one , and treat it as though it were an object of type Two 更换由两个通过一个引用的对象引用的对象,并把它当作好像它是类型的对象, Two

Aside from the obvious typing problem you have, this will only replace the referenced object in memory, not copy the values from one to the other. 除了明显的键入问题之外,这只会替换内存中的引用对象,而不会将值从一个对象复制到另一个对象。

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

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