The method of realizing polymorphism: 1. Virtual method vartual 2. Abstract class, abstract method Abstraction 3. Interface interface.
1. Virtual method
The first step is to add the parent method to virtual, which can be overridden after overriding this. Represents the overridden method, base. Methods inherited from the parent class
public class Person
{
public virtual void saynation ()
{
Console.WriteLine ("Person");
}
}
public class American:person
{
Overriding the parent virtual method by using override
public override void Saynation ()
{
Console.WriteLine ("USA");
}
}
public class Chinese:person
{
public override void Saynation ()
{
Console.WriteLine ("China");
}
}
public class Japanese:person
{
public override void Saynation ()
{
Console.WriteLine ("JAPAN");
}
}
2. Abstract class
1. You can have instance members, or you can abstract members
2. Abstract members cannot have any implementations
3. Abstract members must be in abstract class
4. Objects cannot be instantiated, so the function of an abstract class is to be inherited the primary purpose is to implement polymorphic
5. Inheritance must be rewritten with overvide, unless the subclass is also abstract
Abstract class Myclas
{
public int Age
{
Get
Set
}
public abstract void Sayhi ();
}
Class Myclass1:myclas
{
public override void Sayhi ()
{
Console.WriteLine ("Hao");
}
}
3. Interface
An interface is an abstraction of a series of related functions, forcing must be implemented.
interface and abstract class reuse and extension with inheritance
More concerned that he has the same function---interface
Or care about his extensibility, reuse----abstract class
public interface Irunable
{
void run ();
}
public interface Iswimable
{
void swim ();
}
Public interface irunswimable:irunable, iswimable
{
void Change ();
}
public class Animals
{
}
public class land Animals: Animals, irunable
{
public void Run ()
{
}
}
public class Leopard: irunswimable
{
public void Change ()
{
throw new NotImplementedException ();
}
public void Run ()
{
throw new NotImplementedException ();
}
public void swim ()
{
throw new NotImplementedException ();
}
}
The difference between polymorphism and overloading:
Overload:
A non-virtual function is defined in the base class, and a function with the same name but with a different parameter table is defined in the derived class, which is the overload. When you call these functions on a derived class object, different parameters are called to different functions, which may be called directly to the base class.
Polymorphic:
A virtual function is defined in the base class, and a function with the same name as the parameter table is defined in the derived class, which is polymorphic. Polymorphism is the only case in these 3 cases where dynamic binding technology is used. That is, the object is manipulated by a base-class pointer, and if the object is a base class object, that function in the base class is called, and if the object is actually a derived class object, that function in the sound Ray is called, which function is not determined by the function's parameter table, but by the actual type of the function.
C # polymorphic