黑马程序员--接口

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


实现接口的类或结构要与接口的定义严格一致,类和结构可以像类继承基类或结构一样从接口继承,而且可以继承多个接口。当类或结构继承接口时,它继承成员定义但不继承实现。若要实现接口成员,类中的对应成员必须是公共的、非静态的,并且与接口成员具有相同的名称和签名

接口可由方法、属性、事件、索引器或这4种成员类型的任何组合构成。接口不能包含字段。接口成员一定是公共的


接口的声明采用下列格式: 

 修饰符 interface 接口名称 :继承的接口列表  {   接口内容;  }   

其中,除interface 和接口名称,其他的都是可选项

下面是一个C#接口定义的例子

代码如下:

  public interface InterFa

索引器

属性

事件

方法

 } 

接口继承  一个接口可以从一个或多个基接口继承

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

namespace ConsoleApplication9
{
    class Program
    {
        static void Main(string[] args)
        {
            Person n = new Person();
            n.Name = "小丰";
            n.开始();
            n.交谈();
            n.SeHell();
            Console.Write("年龄:"+n.age);
            Console.ReadKey();
        }

    }
    interface 运动 
    {
        string Name { get; set; }
        int this[int indexer] { get; }
    }
    interface 行为 
    {
        void 交谈();
        void SeHell();
    }

    interface 健身 : 运动 
    {
        void 开始();
    }
    class Person : 健身,行为
    {
        public int age = 15;
        public string Name { get; set; }
        public int this[int indexer] 
        {
            get
            {
                return age;
            }
        }
        public void 开始() 
        {
            Console.WriteLine("我叫{0}今年{1}岁,我在跑步机上跑步",this.Name,this.age);
        }
        public void 交谈() 
        {
            Console.WriteLine("我叫{0}你叫什么名字",this.Name);
        }
        public void SeHell() 
        {
            Console.WriteLine("先生你好啊!");
        }
    }
}



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

详细请查看:<a href="http://edu.csdn.net" target="blank"> http://edu.csdn.net </a>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值