Application of Liskov Conversion in C# and Coercive Type Conversion in Java to Polymorphism

In c#:

Notice:
The subclass does not inherit the constructor of the parent class, but will call the parameterless constructor of the parent class by default.
If a subclass inherits a parent class, the subclass can use members inherited from the parent class in addition to its own members. But the parent class can always only use its own members, not the members of the subclass.
Subclasses also cannot use each other's members.
The concept of Liskov transformation:
 1 ), the subclass can be assigned to the parent class
 2 ), if the parent class is loaded with a subclass object, then it can be said that the parent class is forced to be a subclass object.
Namespace Richter Conversion_Interface Exercise
{
    class Program
    {
        static void Main(string[] args)
        {
            // Test 1
             // IFavorateFood iff = new Cat(); // Subclass can assign to superclass
             // iff.Food();
             // IVoice iv = new Cat();
             // iv.Voice()
 

            // Test 2
             // IVoice Pvoice = new Cat();
             // Pvoice.Voice(); // Only call the methods in IVoice
             // Cat cat = Pvoice as Cat; // If the subclass object installed by the parent class , you can cast the parent class to a child class object
             // cat.Food();

            // Test 3 
            IVoice Pvoice = new Cat();
            IFavorateFood pFavorateFood = Pvoice as IFavorateFood;
            pFavorateFood.Food();
            Console.ReadLine();
        }
    }
}
public class Cat : IFavorateFood, IVoice
{
    public void Food()
    {
        Console.WriteLine( " My favorite thing is mice " );
    }

    public void Voice()
    {
        Console.WriteLine("喵喵喵");
    }
}
interface IFavorateFood
{
    void Food();
}
interface IVoice
{
    void Voice();
}

Application of coercion type conversion in polymorphism in Java:

// Animal class 
abstract  class Animal{

    String name;

    public Animal(String name){
        this.name = name;
    }

    public abstract void run();
}

// mouse 
class   Mouse extends Animal{


    public Mouse(String name){
        super(name);
    }
    
    public  void run(){
        System.out.println(name + "Go slowly on four legs!" );
    }

    // The mouse-specific method---punch a hole 
    public  void dig(){
        System.out.println( "The mouse is making a hole.." );
    }
}



//
class Fish extends Animal{

    public Fish(String name){
        super(name);
    }

    public  void run(){
        System.out.println(name + "Wag your tail and swim!" );
    }


    // Blow bubbles 
    public  void bubble(){
        System.out.println(name + "Blow bubbles...!" );
    }

}




class Demo2 
{
    public static void main(String[] args) 
    {
        /*
        Animal a = new Mouse("mouse"); //polymorphism
        // call a method specific to the subclass
        Mouse m = (Mouse)a; //Force type conversion
        m.dig ();
        */

        Mouse m = new Mouse("Mickey Mouse" );
        Fish f = new Fish("草鱼");

        print(f);

    }


    // Requirements: Define a function that can receive any type of animal object, and call animal-specific methods inside the function. 
    public  static  void print(Animal a){ // Animal a = new Mouse("Mickey Mouse"); 
        if (a instanceof Fish){
            Fish f = (Fish)a;
            f.bubble();
        }else if(a instanceof Mouse){
            Mouse m = (Mouse)a;
            m.dig ();
        }
    }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324799177&siteId=291194637