C#基础学习——接口(十二)

定义

接口只包含了成员的声明。 成员的定义是派生类的责任。 接口提供了派生类应遵循的标准结构。 接口使得实现接口的类或结构在形式上保持一致。
接口只做抽象的定义、管理;接口是一个抽象类,在接口中定义所有方法声明都是抽象的并用public修饰;统一不同具体类型的一个抽象约束。

语法

public interface 名称{}
/// 1.接口不能定义构造函数
/// 2.在新c#语言中接口可以定义方法实现
/// 3.在新的c#语言中可以定义抽象方法
/// 4.可以定义静态字段,不可以定义实例字段

 public interface JieKou
        {
    
    
            //c#8.0支持
           //  static string str = "sfvn";
            public abstract  void Test();
            c#8.0支持
           // public void Tesst()
           // {
    
    

           // }

            void Test2();
            void Test3();
            
            

        }
        /// <summary>
        /// 派生类——接口方法重写
        /// </summary>
        public class Jik : JieKou
        {
    
    
            public void Test()
            {
    
    
                throw new NotImplementedException();
            }

            public void Test2()
            {
    
    
                throw new NotImplementedException();
            }

            public void Test3()
            {
    
    
                throw new NotImplementedException();
            }
        }

举例

WidthAttritube 与HeightAttritube 都属于同一类型

 /// <summary>
        /// 接口应用举例--统一类型
        /// </summary>
        interface Attritube
        {
    
     
        
        }
        /// <summary>
        /// 统一类型
        /// </summary>
        class WidthAttritube : Attritube
        {
    
     
        
        }
        /// <summary>
        /// 统一类型
        /// </summary>
        class HeightAttritube : Attritube
        {
    
     
        
        }

多接口

1、一个类可以实现多个接口,但是只能继承一个类
2、接口与接口之间可以进行继承

举栗时间到:类继承接口、类继承多接口、接口继承接口、多态

        interface If1
        {
    
    
            void Fangfa1();
            void Fangfa2();
        }
        interface If2
        {
    
    
            void Fangfa3();
            void Fangfa4();
        }
        interface If3
        {
    
    
            void Fangfa5();
            void Fangfa6();
        }
        interface If4 : If3
        {
    
    
            void Fangfa7();
            void Fangfa8();
        }
        ///1 类实现多接口
        public class MultiInterface : If1, If2, If3
        {
    
    
            public void Fangfa1()
            {
    
    
                throw new NotImplementedException();
            }

            public void Fangfa2()
            {
    
    
                throw new NotImplementedException();
            }

            public void Fangfa3()
            {
    
    
                throw new NotImplementedException();
            }

            public void Fangfa4()
            {
    
    
                throw new NotImplementedException();
            }

            public void Fangfa5()
            {
    
    
                throw new NotImplementedException();
            }

            public void Fangfa6()
            {
    
    
                throw new NotImplementedException();
            }
        }
    //抽象类继承接口——方法1:实现接口中提供的抽象方法        
        public abstract class Ab : If1
        {
    
    
            public void Fangfa1()
            {
    
    
                throw new NotImplementedException();
            }

            public void Fangfa2()
            {
    
    
                throw new NotImplementedException();
            }
        }
        //抽象类继承接口--方法2:以抽象方法的形式进行声明
        public abstract class Ab1 : If1
        {
    
    
            public abstract void Fangfa1();
            public abstract void Fangfa2();
        }
         /// <summary>
        ///2 实现继承其他接口
        /// </summary>
        public class Mc : If4
        {
    
    
            public void Fangfa5()
            {
    
    
                throw new NotImplementedException();
            }

            public void Fangfa6()
            {
    
    
                throw new NotImplementedException();
            }

            public void Fangfa7()
            {
    
    
                throw new NotImplementedException();
            }

            public void Fangfa8()
            {
    
    
                throw new NotImplementedException();
            }
        }
         //多态
        public class DT
        {
    
    
            //表示if1
            If1 if1 = new MultiInterface();
            //表示if2
            If2 if2 = new MultiInterface();
        }

举例

举个深浅复制的栗子:Color未继承ICloneable,Myobject继承了ICloneable,实现Clone()方法

        public class Color
        {
    
    
            public int red;
            public int blue;
            public int green;

            public Color(int red, int blue, int green)
            {
    
    
                this.blue = blue;
                this.green = green;
                this.red = red;
            }
        }
        public class Myobject:ICloneable
        {
    
    
            public int id;
            public int size;
            public Color color;
            public Myobject(int id,int size,Color color)
            {
    
    
                this.id = id;
                this.size = size;
                this.color = color;
            }
            public object Clone()
            {
    
    
                //throw new NotImplementedException();
                return new Myobject(this.id, this.size, this.color);
            }
        }

猜你喜欢

转载自blog.csdn.net/weixin_45496521/article/details/127951254