c# interface and abstract class learning summary

The following example defines an abstract class and an interface. The abstract class defines the attributes and methods of a mobile phone, and the interface defines a method for playing games.

Abstract class code:

 //抽象类是一个父类+约束 父类是为了实现代码重用 约束是为了多态变化 单继承 is a 
    public abstract class BasePhone
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Branch { get; set; }

        public abstract void System();//约束下子类有这个方法 但又各不相同

        public void Call()
        {
            Console.WriteLine($"Use {this.GetType().Name} Call");
        }

        public void SendMsg()
        {
            Console.WriteLine($"Use {this.GetType().Name} SendMsg");
        }
    }

Interface code:

  //接口就是一个约束 一个类可以实现多个接口 告诉别人一定有什么功能 can do
    public interface IExted
    {
        void PlayGame();
    }

 

Define the Xiaomi mobile phone class, inherit the abstract class BasePhone, and implement the interface: 

  public class Mi : BasePhone, IExted
    {
        public void PlayGame()
        {
            Console.WriteLine($"Use {this.GetType().Name} PlayGame");
        }

        public override void System()
        {
            Console.WriteLine($"{this.GetType().Name} System is Android ");
        }
    }

Define the Apple mobile phone class, inherit the abstract class BasePhone, and implement the interface: 

 public class iPhone : BasePhone, IExted
    {
        public void PlayGame()
        {
            Console.WriteLine($"Use {this.GetType().Name} PlayGame");
        }

        public override void System()
        {
            Console.WriteLine($"{this.GetType().Name} System is IOS ");
        }
    }

 

Instantiate the object and call the method:

  class Program
    {
        static void Main(string[] args)
        {
            iPhone iphone = new iPhone();
            iphone.System();
            iphone.Call();
            iphone.SendMsg();
            iphone.PlayGame();
            iphone.Id=1;

            Mi mi = new Mi();
            mi.System();
            mi.Call();

            Console.ReadLine();
        }
    }

Results of the:

 

Both Apple mobile phones and Xiaomi mobile phones have common attributes and methods, such as calling (Call) and sending text messages (SendMsg), but their operating systems are different, so the operating system method (System) is defined as an abstract method, in the parent class (BasePhone) is not implemented, the subclass rewrites this method of the operating system.

At the same time, both Apple phones and Xiaomi phones can play games, so the method of playing games (PlayGame) is defined in the interface, and the class that implements the interface must implement each method in the interface. In this way, both Apple mobile phones and Xiaomi mobile phones have a way to play games.

The reason why the method of playing the game is defined in the interface, not in the abstract class. This is the consideration. In addition to mobile phones that can play games, tablets and TVs can also play games, but tablets and TVs cannot make calls or send text messages.

 

to sum up:

An abstract class can be understood as a parent class + constraint, the parent class is for code reuse, and the constraint is for polymorphic changes. Abstract classes can only be single inherited. To express the concept of abstract class in English, it can be expressed as'is a', which means what it is. For example, the BasePhone in the above example means a mobile phone.

Interfaces are pure constraints, telling others what functions are available. To express the concept of interface in English, it can be expressed as'can do', which means what can be done.

In actual development, first consider the use of interfaces, and if there is code that needs to be reused, consider using abstract classes.

Guess you like

Origin blog.csdn.net/liangmengbk/article/details/112735451