简体   繁体   中英

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.

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 ! 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. So both Cat and Dog are animals.

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):

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 . The other way around would work fine though ( Two two = new One(); )

In order to get a Two from a One you will have to create a new 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.

Yes, you have to implement a method that copies all members of one to two . 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 .

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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