黑马程序员-------面向对象基础(二)

---------------------- <a href="http://edu.csdn.net"target="blank">ASP.Net+Android+IOS开发</a>、<a href="http://edu.csdn.net"target="blank">.Net培训</a>、期待与您交流! ----------------------


 

接口:

一、接口的概念

接口就是一个只包含抽象成员的一种特殊的类,值定义成员的接口规格,而成员的实现是在继承了接口的类中,由派生类根据自身需求,来实现接口的成员内容。

二、运用接口的好处

其实C#语法中,任何语法都有其作用,例如,前面的类的继承,为了大型程序省去了不少麻烦,提高了其可维护性,多态性的功能真的是很强大,然而任何事物都有两面性,有利就有弊,在类的继承中,随着继承的关系结构愈来愈复杂,基类的修改对派生类有不同程度的影响,从而降低了程序的扩充弹性,在这种情况下,使用接口可以解决由继承所带来的问题。

三、接口的特点

1.可以在类中同时继承多个接口,接口只定义规格,不提供具体的实现,所有接口都是在多继承的类中实现。

2.我们既可以在外部通过实例化派生类,引用实现成员方法,也可以将派生类的实例转换为接口类型,这时需要用到as关键字。

3.接口就如同类,也可以通过继承方式来扩充。这时,需要注意的是,实现接口的类,一定也要实现其继承关系中的所有成员。

4.指定实现接口---如果同时继承两个或多个接口,两个或多个接口中都有同一个方法,那么,为了区别开来,要带上接口名.方法名,同时,要把类转换成专属的接口类型,然后通过接口类型来调用该方法。同时不用加访问修饰符。

5.在接口中可以定义属性,如同方法成员。

例如:用类中实现了接口的属性来传值。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace _01

{

    interface Interface1

    {

        string str { getset; }

        void method();

    }

      class myclass : Interface1 

    {

     private string s;

     public  string str

     {

         get {return s ;}

         set { s = value; }

     }

     public myclass(string str)

     {

         this.s = str;

     }

      public  void method()

        {          

        {      

    }

}

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace _01

{

    class Program 

    {

        static void Main(string[] args)

        {

            string str1;

            myclass my1 = new myclass("hello !C#");

            str1 = my1.str;

            Console.WriteLine(str1);

            myclass my = new myclass("Hello ! C#world!");

            str1 = my.str;

            Console.WriteLine(str1 );                    

            Console.ReadKey();            

        }

    }    

}

一个类同时继承了两个接口,每个接口里都有method()方法,需要把类转成接口来调用类里面的方法:

在同接口名.类型的时候,不用加访问修饰符

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace _02

{

    interface Interface1

    {

        void method();

    }

    interface Interface2

    {

        void method();

    }

    class forexample:Interface1 ,Interface2 

    {

        public void method()

        {

            Console.WriteLine("forexample自己的method()方法");

        }

        void Interface1.method()

        {

            Console.WriteLine("第一个接口的method()方法");

        }

        void Interface2.method()

        {

            Console.WriteLine("第二个接口的method()方法");

        } 

    }

}

 using System;

using System.Collections.Generic;

using System.Linq;

using System.Text; 

namespace _02

{

    class Program

    {

        /// <summary>

        /// 一个简单的接口实现的例子

        /// </summary>

        /// <param name="args"></param>

        static void Main(string[] args)

        {

            forexample forexample1 = new forexample();

            forexample1.method();            

            Interface1 interface1 = forexample1 as Interface1;

            if (forexample1 is Interface1)

            {

                interface1.method();

            }

            Interface2 interface2 = forexample1 as Interface2;

            if (interface2 is Interface2)

            {

                interface2.method();

            }           

            Console.ReadKey();

        }

    }

}

 ---------------------- <a href="http://edu.csdn.net"target="blank">ASP.Net+Android+IOS开发</a>、<a href="http://edu.csdn.net"target="blank">.Net培训</a>、期待与您交流! ----------------------

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值