关于interface的基本测试

 



 

一个类一个接口

测试1、正常应用

测试2、接口中不能包含字段

测试3、接口中只能declare接口成员, 不能define 接口成员

测试4、无交集的测试

测试5implement 接口,但不 define 接口成员

测试6implement 接口,define 接口成员,但是在类中define 接口成员时,不设定访问权限

测试7implement 接口,define 接口成员,在类中define接口成员时,设定访问权限为public

两个类一个接口

测试8、两个类implement 同一接口

三个类一个接口

测试9、三个类implement同一接口,并在第三个类中 inherit 另外两个类

测试10、三个类implement同一接口,并在第三个类中inherit另外两个类,与测试9的效果一样,不同点在于,用到了接口数组。

一个类和两个接口

测试11、创建一个类和两个接口,并让此类implement这两个接口

测试12、创建一个类和两个接口,并让此类implement这两个接口,同时,两个接口指定的接口成员名称相同,在类中 define 此方法

测试13--14、创建一个类和两个接口,并让此类以不同的方式implement这两个接口,

测试15--17、创建一个类和两个接口,并让其中第一个接口 inherit 第二个接口,然后,让此类implement 第一个接口

1、创建一个类和一个空接口,如下:

namespace InterfaceT

{

    public class ClassA

    {

        public string RtStringClassA ( )

        {

            return "ClassA";

        }

    }

}

namespace InterfaceT

{

    interface InterfaceA

    {

    }

} 

通过控制台程序实例化此测试类,如下:

using System;

using System.Collections.Generic;

using System.Text; 

using InterfaceT; 

namespace InterfaceTest

{

    class Program

    {

        static void Main ( string[] args )

        {

            ClassA classA = new ClassA ( );

            Console.WriteLine ( classA.RtStringClassA ( ) );

            Console.ReadLine ( );

        }

    }

}

输出: ClassA

 

 

2、创建一个类和一个接口,如下:

namespace InterfaceT

{

    public class ClassA

    {

        public string RtStringClassA ( )

        {

            return "ClassA";

        }

    }

}

namespace InterfaceT

{

    interface InterfaceA

    {

        string str;

    }

}

 

编译时输出异常:

InterfaceA.cs 接口不能包含字段

 

3、创建一个类和一个接口,如下:

namespace InterfaceT

{

    public class ClassA

    {

        public string RtStringClassA ( )

        {

            return "ClassA";

        }

    }

}

namespace InterfaceT

{

    interface InterfaceA

    {

        string RtStringInterfaceA ( )

        {

            return "InterfaceA";

        }

    }

}

 

编译时输出异常:

“InterfaceT.InterfaceA.RtStringInterfaceA()”: 接口成员不能有定义

 

 

4、创建一个类和一个接口,如下:

namespace InterfaceT

{

    public class ClassA

    {

        public string RtStringClassA ( )

        {

            return "ClassA";

        }

    }

}

namespace InterfaceT

{

    interface InterfaceA

    {

        string RtStringInterfaceA ( );

    }

}

通过控制台程序实例化此测试类,如下:

using System;

using System.Collections.Generic;

using System.Text;

 

using InterfaceT;

 

namespace InterfaceTest

{

    class Program

    {

        static void Main ( string[] args )

        {

            ClassA classA = new ClassA ( );

            Console.WriteLine ( classA.RtStringClassA ( ) );

            Console.ReadLine ( );

        }

    }

}

输出: ClassA

 

 

5、创建一个类和一个接口,并让此类实现接口如下:

namespace InterfaceT

{

    interface InterfaceA

    {

        string RtStringInterfaceA ( );

    }

}

namespace InterfaceT

{

    public class ClassB: InterfaceA

    {

        public string RtStringClassB ( )

        {

            return "ClassB";

        }

    }

}

 

编译时输出异常:

“InterfaceT.ClassB”不会实现接口成员“InterfaceT.InterfaceA.RtStringInterfaceA()”

 

 

6、创建一个类和一个接口,并让此类实现接口如下:

namespace InterfaceT

{

    interface InterfaceA

    {

        string RtStringInterfaceA ( );

    }

}

namespace InterfaceT

{

    public class ClassB: InterfaceA

    {

        public string RtStringClassB ( )

        {

            return "ClassB";

        }

 

        string RtStringInterfaceA ( )

        {

            return "InterfaceA";

        }

    }

}

 

编译时输出异常:

“InterfaceT.ClassB”不会实现接口成员“InterfaceT.InterfaceA.RtStringInterfaceA()”。

“InterfaceT.ClassB.RtStringInterfaceA()”或者是静态、非公共的,或者有错误的返回类型。

 

 

7、创建一个类和一个接口,并让此类实现接口如下:

namespace InterfaceT

{

    interface InterfaceA

    {

        string RtStringInterfaceA ( );

    }

}

namespace InterfaceT

{

    public class ClassB: InterfaceA

    {

        public string RtStringClassB ( )

        {

            return "ClassB";

        }

 

        public string RtStringInterfaceA ( )

        {

            return "InterfaceA";

        }

    }

}

通过控制台程序实例化此测试类,如下:

using System;

using System.Collections.Generic;

using System.Text;

 

using InterfaceT;

 

namespace InterfaceTest

{

    class Program

    {

        static void Main ( string[] args )

        {

            //ClassA classA = new ClassA ( );

            //Console.WriteLine ( classA.RtStringClassA ( ) );

            ClassB classB = new ClassB ( );

            Console.WriteLine ( classB.RtStringClassB ( ) );

            Console.WriteLine ( classB.RtStringInterfaceA ( ) );

            Console.ReadLine ( );

        }

    }

}

 

输出: ClassB

      InterfaceA

 

 

8、创建两个类和一个接口,并让此两个类实现此接口如下:

namespace InterfaceT

{

    interface InterfaceA

    {

        string RtStringInterfaceA ( );

    }

}

namespace InterfaceT

{

    public class ClassB: InterfaceA

    {

        public string RtStringClassB ( )

        {

            return "ClassB";

        }

 

        public string RtStringInterfaceA ( )

        {

            return "InterfaceA Deriving From ClassB";

        }

    }

}

namespace InterfaceT

{

    public class ClassC: InterfaceA

    {

        public string RtStringClassC ( )

        {

            return "ClassC";

        }

 

        public string RtStringInterfaceA ( )

        {

            return "InterfaceA Deriving From ClassC";

        }

    }

}

通过控制台程序实例化此测试类,如下:

using System;

using System.Collections.Generic;

using System.Text;

 

using InterfaceT;

 

namespace InterfaceTest

{

    class Program

    {

        static void Main ( string[] args )

        {

            ClassB classB = new ClassB ( );

            Console.WriteLine ( classB.RtStringClassB ( ) );

            Console.WriteLine ( classB.RtStringInterfaceA ( ) );

            Console.WriteLine ( "****************************" );

            ClassC classC = new ClassC ( );

            Console.WriteLine ( classC.RtStringClassC ( ) );

            Console.WriteLine ( classC.RtStringInterfaceA ( ) );

            Console.ReadLine ( );

        }

    }

}

 

输出: ClassB

      InterfaceA Deriving From ClassB

      ****************************

      ClassC

      InterfaceA Deriving From ClassC

 

 

9、创建三个类和一个接口,并让此三个类实现此接口如下:

namespace InterfaceT

{

    interface InterfaceA

    {

        string RtStringInterfaceA ( );

    }

}

namespace InterfaceT

{

    public class ClassB: InterfaceA

    {

        public string RtStringClassB ( )

        {

            return "ClassB";

        }

 

        public string RtStringInterfaceA ( )

        {

            return "InterfaceA Deriving From ClassB";

        }

    }

}

namespace InterfaceT

{

    public class ClassC: InterfaceA

    {

        public string RtStringClassC ( )

        {

            return "ClassC";

        }

 

        public string RtStringInterfaceA ( )

        {

            return "InterfaceA Deriving From ClassC";

        }

    }

}

namespace InterfaceT

{

    public class ClassD:InterfaceA

    {

        public string RtStringClassD ( )

        {

            string rtStr = "ClassD";

            InterfaceA interfaceA = new ClassB ( );

            rtStr = rtStr + ""n " + interfaceA.RtStringInterfaceA ( );

            interfaceA = new ClassC ( );

            rtStr = rtStr + ""n " + interfaceA.RtStringInterfaceA ( );

            interfaceA = new ClassD ( );

            rtStr = rtStr + ""n " + interfaceA.RtStringInterfaceA ( );

 

            return rtStr;

        }

 

        public string RtStringInterfaceA ( )

        {

            return "InterfaceA Deriving From ClassD";

        }

    }

}

通过控制台程序实例化此测试类,如下:

using System;

using System.Collections.Generic;

using System.Text;

 

using InterfaceT;

 

namespace InterfaceTest

{

    class Program

    {

        static void Main ( string[] args )

        {

            ClassD classD = new ClassD ( );

            Console.WriteLine ( classD.RtStringClassD ( ) );

            Console.WriteLine ( classD.RtStringInterfaceA ( ) );

            Console.ReadLine ( );

           

        }

    }

}

 

输出: ClassD

      InterfaceA Deriving From ClassB

      InterfaceA Deriving From ClassC

      InterfaceA Deriving From ClassD

      InterfaceA Deriving From ClassD

 

 

10、创建三个类和一个接口,并让此三个类实现此接口如下:

namespace InterfaceT

{

    interface InterfaceA

    {

        string RtStringInterfaceA ( );

    }

}

namespace InterfaceT

{

    public class ClassB: InterfaceA

    {

        public string RtStringClassB ( )

        {

            return "ClassB";

        }

 

        public string RtStringInterfaceA ( )

        {

            return "InterfaceA Deriving From ClassB";

        }

    }

}

namespace InterfaceT

{

    public class ClassC: InterfaceA

    {

        public string RtStringClassC ( )

        {

            return "ClassC";

        }

 

        public string RtStringInterfaceA ( )

        {

            return "InterfaceA Deriving From ClassC";

        }

    }

}

namespace InterfaceT

{

    public class ClassD:InterfaceA

    {

        public string RtStringClassD ( )

        {

            string rtStr = "ClassD";          

 

            //InterfaceA interfaceA = new ClassB ( );

            //rtStr = rtStr + ""n " + interfaceA.RtStringInterfaceA ( );

            //interfaceA = new ClassC ( );

            //rtStr = rtStr + ""n " + interfaceA.RtStringInterfaceA ( );

            //interfaceA = new ClassD ( );

            //rtStr = rtStr + ""n " + interfaceA.RtStringInterfaceA ( );

 

            //return rtStr;

            InterfaceA[] interfaceA ={ new ClassB ( ), new ClassC ( ), new ClassD ( ) };

 

            foreach ( InterfaceA interfacea in interfaceA )

            {

                rtStr = rtStr + ""n " + interfacea.RtStringInterfaceA ( );

            }

            return rtStr;

        }

 

        public string RtStringInterfaceA ( )

        {

            return "InterfaceA Deriving From ClassD";

        }

    }

}

通过控制台程序实例化此测试类,如下:

using System;

using System.Collections.Generic;

using System.Text;

 

using InterfaceT;

 

namespace InterfaceTest

{

    class Program

    {

        static void Main ( string[] args )

        {

            ClassD classD = new ClassD ( );

            Console.WriteLine ( classD.RtStringClassD ( ) );

            Console.WriteLine ( classD.RtStringInterfaceA ( ) );

            Console.ReadLine ( );

           

        }

    }

}

 

输出: ClassD

      InterfaceA Deriving From ClassB

      InterfaceA Deriving From ClassC

      InterfaceA Deriving From ClassD

      InterfaceA Deriving From ClassD

 

 

11、创建一个类和两个接口,并让此类实现这两个接口如下:

namespace InterfaceT

{

    interface InterfaceA

    {

        string RtStringInterfaceA ( );

    }

}

namespace InterfaceT

{

    interface InterfaceB

    {

        string RtStringInterfaceB ( );

    }

}

namespace InterfaceT

{

    public class ClassE: InterfaceA,InterfaceB

    {

        public string RtStringClassE ( )

        {

            string rtStr = "ClassE";

            InterfaceA interfaceA= new ClassE ( );

            rtStr = rtStr + ""n" + interfaceA.RtStringInterfaceA ( );

            InterfaceB interfaceB = new ClassE ( );

            rtStr = rtStr + ""n" + interfaceB.RtStringInterfaceB ( );

            return rtStr;

        }

 

        public string RtStringInterfaceA ( )

        {

            return "InterfaceA Deriving From ClassE";

        }

 

        public string RtStringInterfaceB ( )

        {

            return "InterfaceB Deriving From ClassE";

        }

    }

}

通过控制台程序实例化此测试类,如下:

using System;

using System.Collections.Generic;

using System.Text;

 

using InterfaceT;

 

namespace InterfaceTest

{

    class Program

    {

        static void Main ( string[] args )

        {

            ClassE classE = new ClassE ( );

            Console.WriteLine ( classE.RtStringClassE ( ) );

            Console.WriteLine ( classE.RtStringInterfaceA ( ) );

            Console.WriteLine ( classE.RtStringInterfaceB ( ) );

            Console.ReadLine ( );

        }

    }

}

 

输出: ClassE

      InterfaceA Deriving From ClassE

      InterfaceB Deriving From ClassE

      InterfaceA Deriving From ClassE

      InterfaceB Deriving From ClassE

 

 

12、创建一个类和两个接口,并让此类实现这两个接口,同时,两个接口指定的方法名相同,如下:

namespace InterfaceT

{

    interface InterfaceA

    {

        string RtStringInterfaceA ( );

    }

}

namespace InterfaceT

{

    interface InterfaceC

    {

        string RtStringInterfaceA ( );

    }

}

namespace InterfaceT

{

    public class ClassF : InterfaceA, InterfaceC

    {

        public string RtStringClassF ( )

        {

            string rtStr = "ClassF";

            InterfaceA interfaceA = new ClassF ( );

            rtStr = rtStr + ""n" + interfaceA.RtStringInterfaceA ( );

            InterfaceC interfaceC = new ClassF ( );

            rtStr = rtStr + ""n" + interfaceC.RtStringInterfaceA ( );

            return rtStr;

        }

        public string RtStringInterfaceA ( )

        {

            return "InterfaceA Deriving From ClassF";

        }

    }

}

通过控制台程序实例化此测试类,如下:

using System;

using System.Collections.Generic;

using System.Text;

 

using InterfaceT;

 

namespace InterfaceTest

{

    class Program

    {

        static void Main ( string[] args )

        {

            ClassF classF = new ClassF ( );

            Console.WriteLine ( classF.RtStringClassF ( ) );

            Console.WriteLine ( classF.RtStringInterfaceA ( ) );

            Console.ReadLine ( );

        }

    }

}

 

输出: ClassF

      InterfaceA Deriving From ClassF

      InterfaceA Deriving From ClassF

      InterfaceA Deriving From ClassE

 

 

13、创建一个类和两个接口,并让此类实现这两个接口,如下:

namespace InterfaceT

{

    interface InterfaceA

    {

        string RtStringInterfaceA ( );

    }

}

namespace InterfaceT

{

    interface InterfaceB

    {

        string RtStringInterfaceB ( );

    }

}

namespace InterfaceT

{

    public class ClassG: InterfaceA,InterfaceB

    {

        public string RtStringClassG ( )

        {

            string rtStr = "ClassG";

 

            ClassG classG = new ClassG ( );

            InterfaceA interfaceA = classG;

            rtStr = rtStr + ""n" + interfaceA.RtStringInterfaceA ( );

            InterfaceB interfaceB = classG;

            rtStr = rtStr + ""n" + interfaceB.RtStringInterfaceB ( );

            return rtStr;

        }

 

        public string InterfaceA.RtStringInterfaceA()

        {

            return "InterfaceA Deriving From ClassG";

        }

 

        public string InterfaceB.RtStringInterfaceB ( )

        {

            return "InterfaceB Deriving From ClassG";

        }

    }

}

 

编译时输出异常:

1    修饰符“public”对该项无效

2    修饰符“public”对该项无效

 

 

14、创建一个类和两个接口,并让此类实现这两个接口,如下:

namespace InterfaceT

{

    interface InterfaceA

    {

        string RtStringInterfaceA ( );

    }

}

namespace InterfaceT

{

    interface InterfaceB

    {

        string RtStringInterfaceB ( );

    }

}

namespace InterfaceT

{

    public class ClassG: InterfaceA,InterfaceB

    {

        public string RtStringClassG ( )

        {

            string rtStr = "ClassG";

 

            ClassG classG = new ClassG ( );

            InterfaceA interfaceA = classG;

            rtStr = rtStr + ""n" + interfaceA.RtStringInterfaceA ( );

            InterfaceB interfaceB = classG;

            rtStr = rtStr + ""n" + interfaceB.RtStringInterfaceB ( );

            return rtStr;

        }

 

        string InterfaceA.RtStringInterfaceA()

        {

            return "InterfaceA Deriving From ClassG";

        }

 

        string InterfaceB.RtStringInterfaceB ( )

        {

            return "InterfaceB Deriving From ClassG";

        }

    }

}

通过控制台程序实例化此测试类,如下:

using System;

using System.Collections.Generic;

using System.Text;

 

using InterfaceT;

 

namespace InterfaceTest

{

    class Program

    {

        static void Main ( string[] args )

        {

            ClassG classG = new ClassG ( );

            Console.WriteLine ( classG.RtStringClassG ( ) );

            Console.ReadLine ( );

        }

    }

}

 

输出: ClassG

      InterfaceA Deriving From ClassG

      InterfaceB Deriving From ClassG

 

 

 

15、创建一个类和两个接口,并让此类实现这两个接口,如下:

namespace InterfaceT

{

    interface InterfaceA

    {

        string RtStringInterfaceA ( );

    }

}

namespace InterfaceT

{

    interface InterfaceD : InterfaceA

    {

        string RtStringInterfaceD ( );

    }

}

namespace InterfaceT

{

    public class ClassH:InterfaceD

    {

        public string RtStringClassH ( )

        {

            string rtStr = "ClassH";

 

            ClassH classH = new ClassH ( );

            //InterfaceA interfaceA = classH;

            InterfaceD interfaceD = classH;

            rtStr = rtStr + ""n" + interfaceD.RtStringInterfaceA ( );

            rtStr = rtStr + ""n" + interfaceD.RtStringInterfaceD ( );

 

            return rtStr;

        }

 

        public string RtStringInterfaceD ( )

        {

            return "InterfaceD Deriving From ClassH";

        }

 

        public string RtStringInterfaceA ( )

        {

            return "InterfaceA Deriving From ClassH";

        }

    }

}

通过控制台程序实例化此测试类,如下:

using System;

using System.Collections.Generic;

using System.Text;

 

using InterfaceT;

 

namespace InterfaceTest

{

    class Program

    {

        static void Main ( string[] args )

        {

            ClassH classH = new ClassH ( );

            Console.WriteLine ( classH.RtStringClassH ( ) );

            Console.ReadLine ( );

        }

    }

}

 

输出: ClassH

      InterfaceA Deriving From ClassH

      InterfaceD Deriving From ClassH

 

16、创建一个类和两个接口,并让此类实现这两个接口,如下:

namespace InterfaceT

{
    interface InterfaceA

    {

        string RtStringInterfaceA ( );

    }

}

namespace InterfaceT

{

    interface InterfaceD : InterfaceA

    { 

        string RtStringInterfaceD ( );

 

    }

}

namespace InterfaceT

{

    public class ClassJ:InterfaceD

    {

        string InterfaceD.RtStringInterfaceD ( )

        {

            return "InterfaceD Deriving From ClassJ";

        } 

        string InterfaceA.RtStringInterfaceA ( )
        {

            return "InterfaceA Deriving From ClassJ";

        } 

        public string RtStringClassJ ( )
        {

            string rtStr = "ClassJ";

            ClassJ classJ = new ClassJ ( );

            //InterfaceA interfaceA = classH; 

            rtStr = rtStr + ""n" + classJ.RtStringInterfaceA ( );

            rtStr = rtStr + ""n" + classJ.RtStringInterfaceD ( ); 

            return rtStr;

        }

    }
}

 

编译时输出异常:

1    “InterfaceT.ClassJ”并不包含“RtStringInterfaceA”的定义

2    “InterfaceT.ClassJ”并不包含“RtStringInterfaceD”的定义

 

 

 

17、创建一个类和两个接口,并让此类实现这两个接口,如下:

namespace InterfaceT

{

    interface InterfaceA

    {

        string RtStringInterfaceA ( );

    }

}

namespace InterfaceT

{

    interface InterfaceD : InterfaceA

    {

 

        string RtStringInterfaceD ( );

 

    }

}

namespace InterfaceT

{

    public class ClassI:InterfaceD

    {

        string InterfaceD.RtStringInterfaceD ( )

        {

            return "InterfaceD Deriving From ClassI";

        }

 

        string InterfaceD.RtStringInterfaceA ( )

        {

            return "InterfaceA Deriving From ClassI";

        }

 

        //string InterfaceA.RtStringInterfaceA ( )

        //{

        //    return "InterfaceA Deriving From ClassI";

        //}

 

        public string RtStringClassI ( )

        {

            string rtStr = "ClassI";

 

            ClassI classI = new ClassI ( );

            //InterfaceA interfaceA = classH;

            InterfaceD interfaceD = classI;

            rtStr = rtStr + ""n" + interfaceD.RtStringInterfaceA ( );

            rtStr = rtStr + ""n" + interfaceD.RtStringInterfaceD ( );

 

            return rtStr;

        }

    }

}

编译时输出异常:

1    显式接口声明中的“InterfaceD.RtStringInterfaceA”不是接口成员

2    “InterfaceT.ClassI”不会实现接口成员“InterfaceT.InterfaceA.RtStringInterfaceA()”

 

namespace InterfaceT

{

    public class ClassI:InterfaceD

    {

        string InterfaceD.RtStringInterfaceD ( )

        {

            return "InterfaceD Deriving From ClassI";

        }

 

        //string InterfaceD.RtStringInterfaceA ( )

        //{

        //    return "InterfaceA Deriving From ClassI";

        //}

 

        string InterfaceA.RtStringInterfaceA ( )

        {

            return "InterfaceA Deriving From ClassI";

        }

 

        public string RtStringClassI ( )

        {

            string rtStr = "ClassI";

 

            ClassI classI = new ClassI ( );

            //InterfaceA interfaceA = classH;

            InterfaceD interfaceD = classI;

            rtStr = rtStr + ""n" + interfaceD.RtStringInterfaceA ( );

            rtStr = rtStr + ""n" + interfaceD.RtStringInterfaceD ( );

 

            return rtStr;

        }

    }

}

 

输出: ClassI

      InterfaceA Deriving From ClassI

      InterfaceD Deriving From ClassI

转载于:https://www.cnblogs.com/handing/archive/2009/08/26/1554384.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值