. NET Learning 3rd Quarter C # Object-oriented polymorphism

Source: Internet
Author: User

. Net Video Learning 2nd-quarter C # Object-oriented

Object-oriented polymorphism

Concept: Having a (parent) object exhibit multiple (subclass) types of attributes; Three methods: Virtual method/abstract class/interface

Method One: Virtual method

Mark the parent class method as a virtual method, using the keyword virtual before returning a value type, this method can be overridden by the class (using the keyword override before the subclass method returns a value type)

The virtual method is not used:

public class person{
private string _name;
public string Name
{
Get{return _name;}
Set{_name = value;}
}
Public person (string name)
{
This. name = name;
}
public void SayHello ()
{
Console.WriteLine ("I am a Person");
}}

public class chinese:person{
Public Chinese (string name): Base (name)
{
}
public void SayHello ()
{
Console.WriteLine ("I am Chinese");
}}
public class japanese:person{
Japanese (string name): Base (name)
{
}
public void SayHello ()
{
Japanese");
}}

Create a person[] array, put the objects of Chinese and Japanese (Richter conversion), and then iterate through the array members call their SayHello () method, will all get I am a person, because although the objects are put in Chinese and japanses , but the array itself is a person array, so the members are interpreted as objects of type person, so that only the SayHello () method of the parent class person can be called. To get a method call for a subclass, you need to force the type conversion.

person{...
public virtual void SayHello () {}}

Chinese:person
{
....
SayHello (){}
}

Japanese:person
{
....
SayHello () {}
}

With the virtual method described above, no casts are required in the array, and Chinese/japanese objects call SayHello () in their respective subclasses.

Essence: The call is still a method of the parent class, but the method has been overridden by the quilt class, as to which subclass of the overridden version is selected, depending on which subclass object the parent object is loaded in (if the subclass object is not loaded, it is the parent class object itself, the parent class method is called)

Method Two: Abstract class

When the method of the parent class is not sure how to implement it, you can choose to write the parent class as an abstract class, while the parent class method is written as an abstract method, using the keyword abstract, noting that the abstract method does not allow the method body (that is, there is no pair of curly braces)

 Public Abstract class animal{public abstract void Bark ();}

public class Dog:animal
{
public override void Bark ()
{
Console.WriteLine ("XXXX");
}
}

public class Cat:animal
{
void Bark ()
{
Console.WriteLine ("YYYY");
}
}

An abstract class cannot create an object. However, the object invocation method of the subclass calls the abstract method of the parent class at all times (except that the content is overridden by the method of its own subclass)

Method has practical meaning with virtual method, no practical meaning with abstract method

. NET Learning 3rd Quarter C # Object-oriented polymorphism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.