C ා detailed explanation of simple interface and inheritance example -- quick start

Keywords: C Programming

We talked about inheritance in the last article, but in fact, they are similar.

An interface is an abstraction of a method. If different classes have the same method, you should consider using an interface.

In C, interfaces can inherit from each other and from each other. A class can inherit one class and multiple interfaces at the same time, but an interface cannot inherit a class.

The inheritance representation method and class inheritance between interfaces are the same, and the inheritance rules are the same, that is, the child interface obtains the content of the parent interface. If there are multiple interfaces, the interfaces are separated by ",".

Old rules, serving:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

   * If you are a working student, you have a dual identity. One identity is student, must complete the study task, one identity is employee, must complete the work task. Furthermore, you are a computer student. In addition to basic courses,
   * You must also learn C programming. How to build a model now?
   * 1.We first define a student interface, stipulate that students must learn, and then establish an employee interface, stipulate that employees must complete work tasks.
   * 2.Students majoring in computer science, in addition to completing general learning tasks, are still learning "C". You can define another interface, inherit the student interface, and specify your own learning tasks.
namespace InterfaceApplication
    //Define student interface
    public interface IStudent
        void study_base();
    //Define computer student interface
    public interface IIStudent : IStudent
        void study_computer();
    //Define employee interface
    public interface IEmployee
        void work();
    public class Infostudent : IEmployee, IIStudent
        //Implement student interface
        public void study_base()
            Console.WriteLine("Students: mathematics, Chinese and English must be learned well");
        //Implementation of computer student interface
        public void study_computer()
            Console.WriteLine("Computer students: mathematics, Chinese and English must be learned well, but also C#");
        //Implement employee interface
        public void work()
            Console.WriteLine("Employee: work must be completed");

    //Operation procedure
    class Program
        static void Main(string[] args)
            Infostudent infostudent = new Infostudent();

Let's run a wave to see the effect!

Obviously, students and computer students have inherited the student interface.

Is it very simple? Flexible use of interface can bring a lot of convenience to work!

Posted by CoolAsCarlito on Mon, 02 Dec 2019 19:19:15 -0800