c # basic grammar



1. What is an interface

The interface is to specify a set of function members without implementing their reference types.
I personally feel that he is more of a norm.


1.1 What is the use of the interface? Look at the example below


Class A

 class AClass
    {
    
    
        public string Name;
        public int Age;
    }

Type B

 class BClass
    {
    
    
        public string First;
        public string Last;
        public double PersonAge;
    }

program class

 class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            AClass aClass = new AClass()
            {
    
    
                Name = "张三",
                Age = 6,
            };
            PrintInfo(aClass);
        }

        static void PrintInfo(AClass item)
        {
    
    
            Console.WriteLine($"名字是{item.Name},年龄是{item.Age}");
        }
    }

The wording of the problem is that if you want to Bclass, rather than Aclassto achieve entry of personal information, this approach is no solution, since Aclassand Bclassare two classes. The things you point out are different.



1.2 Use interface


1.2.1 Declare the interface

 interface IInfo
    {
    
    
        string GetName();
        string GetAge();
    }

1.2.2 Implementing the interface

  class AInfo : IInfo
    {
    
    
        public string Name {
    
     get; set; }
        public int Aget {
    
     get; set; }
        public string GetAge()
        {
    
    
            return Name;
        }

        public string GetName()
        {
    
    
            return Aget.ToString();
        }
    }
class BInfo : IInfo
    {
    
    
        public string First {
    
     get; set; }
        public string Last {
    
     get; set; }
        public double PersonAge {
    
     get; set; }
        public string GetAge()
        {
    
    
            return PersonAge.ToString();
        }

        public string GetName()
        {
    
    
            return First + Last;
        }
    }

1.2.3 Call in the main function

 class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            AInfo aInfo = new AInfo() {
    
     Name = "John", Aget = 35 };
            BInfo bInfo = new BInfo() {
    
     First = "dehua", Last = "liu", PersonAge = 33.6 };
            PrintInfo(aInfo);
            PrintInfo(bInfo);
        }
        static void PrintInfo(IInfo item)
        {
    
    
            Console.WriteLine($"名字{item.GetName()},年龄是{item.GetAge()}");
        }
    }

Note: In the places quoted above, we pass in the interface, not the class that implements an interface.





2. Interface declaration


2.1 Precautions for declaring the interface

  • Interface can only contain non-static member functions (methods, properties, events, indexers)
  • These function members cannot contain any implementation code, and a semicolon must be used after the body
  • According to management, the statement interface, you must use uppercase Ibeginning
  • Interface declarations can have any modifiers. For example public protected internal private. But the members of the interface are implicit publicand cannot take any modifiers, includingpublic
public interface IInfo
    {
    
    
        string GetName();
        string GetAge();
    }

2.2 Precautions for implementing the interface

  • If the class implements the interface, then it must implement all the members of the interface
  • If the interface inherits from the base class and implements the interface, then it must be placed before all the ports. Separate with commas.


2.3 Interface and as operator

The interface is a reference type, which can implement coercive type conversion (that is, use his son to impersonate his father). as follows:

  static void Main(string[] args)
        {
    
    
            AInfo aInfo = new AInfo() {
    
     Name = "John", Aget = 35 };
            IInfo info = (IInfo)aInfo;
            PrintInfo(info);
        }

At that time in this process, if the class does not implement the interface, then the expression should throw an exception, this time, you can use asoperators to avoid this problem, (you should try to avoid abnormal, because they would seriously reduce the speed of the code , And put the program in an inconsistent state)

static void Main(string[] args)
        {
    
    
            AInfo aInfo = new AInfo() {
    
     Name = "John", Aget = 35 };
            IInfo info = aInfo as IInfo;
            if (info != null)
            {
    
    
                PrintInfo(info);
            }
         }


2.4 Implement an interface with duplicate members

If a class implements multiple interfaces, and some of the interfaces have members with the same signature and return type, then the class can implement a single member to satisfy all interfaces that contain duplicate members.

2.5 Derived members as implementation

If the derived class can implement the members of the base class, then even if the class is empty, the base class can still meet the needs of implementing interface methods

interface IIfc1 {
    
     void PrintOut(string s);}
interface IIfc1 {
    
     void PrintOut(string s); }

class AClass
    {
    
    
        public void PrintOut(string s)
        {
    
    
            Console.WriteLine("s");
        }
    }
    
class BClass : AClass, IIfc1
    {
    
      
    // 这里面是空的,也没事
    }



2.6 Explicit interface member implementation

If a single class, members can implement multiple interfaces need, if we want to achieve every few mouthfuls of separation, it should be used to display member function implementation .

statement

interface IIfc1
    {
    
    
        void PrintOut(string s);
    }
    
interface IIfc2
    {
    
    
        void PrintOut(string s);
    }
    class MyClass : IIfc1, IIfc2
    {
    
    
        void IIfc1.PrintOut(string s)
        {
    
    
            Console.WriteLine($"IIf1 输出 {s}");
        }

        void IIfc2.PrintOut(string s)
        {
    
    
            Console.WriteLine($"IIf2 输出 {s}");
        }
    }

transfer:

static void Main(string[] args)
        {
    
    
            MyClass mc = new MyClass();
            IIfc1 ifc1 = mc as IIfc1;
            ifc1.PrintOut("你好");
        }

Guess you like

Origin blog.csdn.net/zhaozhao236/article/details/110853621