C#implements polymorphic interfaces.

Posted by cobaswosa on Sat, 11 May 2019 20:29:36 +0200

1. What is an interface?

Interfaces are specification protocols that allow you to write generic code if you agree to follow a specification.

Defines a set of methods with various functions (just an ability, no concrete implementation, just like an abstract method,'Just say nothing').

Understand: What kind of memory should be made: how much voltage, how many wires to plug, this is an international specification, is the interface.Memory, on the other hand, is specifically produced to implement interfaces.No matter what brand of computer you buy, you can use memory. It's polymorphic.

Interfaces can include methods, properties, indexers, events.They are all methods.

All members of the interface cannot write any access modifiers. The default is public.

Members of interfaces, subclasses must be implemented.

2. The function of the interface.

Interfaces can implement multiple inheritance.

Interfaces solve polymorphic problems between different types, such as that fish and boats are not of the same type, but can "swim" in the water in different ways, so interfaces are available.(The last step in the following example is to logically exclude the car from the person class, but still implement polymorphism)

3. Examples.

I want to register people from each country, but I'll write a lot about how to overload, pass in different types of parameters, and implement them.And if you want to add people from a country, you have to change the code, which makes it less scalable.

class Program
    {
        static void Main(string[] args)
        {
            Chinese chinese = new Chinese();
            DengJi(chinese);
            American american = new American();
            DengJi(american);
        }
        public static void DengJi(Chinese chinese)
        {
            chinese.Show();
        }
        public static void DengJi(American american)
        {
            american.Show();
        }
    }
    class Chinese
    {
        public void Show()
        {
            Console.WriteLine("Chinese, 18");
        }
    }
    class American
    {
        public  void Show()
        {
            Console.WriteLine("Americans, 19");
        }
    }

Optimize 1: Polymorphism is achieved through inheritance, and the parent class is used when parameters are used to see the objects stored in the parent class.---------- Extract the parent Person using virtual or abstract methods.Since Person does not need instantiation, I use abstract classes.

class Program
    {
        static void Main(string[] args)
        {
            Chinese chinese = new Chinese();
            DengJi(chinese);
            American american = new American();
            DengJi(american);
        }
        public static void DengJi(Person person)
        {
            person.Show();
        }     
    }
    public abstract class Person
    {
        public abstract void Show();
    }
    class Chinese:Person
    {
        public override void Show()
        {
            Console.WriteLine("Chinese, 18");
        }
    }
    class American:Person
    {
        public override void Show()
        {
            Console.WriteLine("Americans, 19");
        }
    }

Optimize two: If I want to register a car, it inherits Person class, which is not logical, but I want to achieve polymorphism, everything can be registered.So I chose to use interfaces.

 class Program
    {
        static void Main(string[] args)
        {
            Chinese chinese = new Chinese();
            DengJi(chinese);
            Car car = new Car();
            car.Show();
        }
        public static void DengJi(IShowInfo person)
        {
            person.Show();
        }     
    }
    public interface IShowInfo
    {
        void Show();
    }
    public abstract class Person:IShowInfo
    {
        public abstract void Show();
    }
    class Chinese:Person
    {
        public override void Show()
        {
            Console.WriteLine("Chinese, 18");
        }
    }
    class American:Person
    {
        public override void Show()
        {
            Console.WriteLine("Americans, 19");
        }
    }
    class Car :IShowInfo
    {
        public void Show()
        {
            Console.WriteLine("Honda, 2.0t");
        }
    }

In this way, anything that inherits an interface can be registered.

Topics: C# less