C# 接口

■接口:使用关键字interface,只是定义一系列的行为类型,不提供任何实现
interface IMovable{
    float speed{get;}
    float maxSpeed{get;set;}
    void Run();
    void Walk();
    void Fly();
}
■接口的实现,通过冒号来实现

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace useInterface
{
    
interface  IMovable {
        
float  Speed{get;}
        
float  MaxSpeed { get; set; }
        
void  Run();
        
void  Walk();
        
void  Fly();
    }
    
class  Dog : IMovable {
        
public   float  Speed {
            get {
                
return   10.5f ;
            }
        }
        
private   float  _MaxSpeed;
        
public   float  MaxSpeed{
            get { 
return   this ._MaxSpeed; }
            set { 
this ._MaxSpeed  =  value; }
        }
        
public   void  Run() {
            System.Console.WriteLine(
" Dog is running " );
        }
        
public   void  Walk()
        {
            System.Console.WriteLine(
" Dog is walking " );
        }
        
public   void  Fly()
        {
            System.Console.WriteLine(
" Dog can't fly " );
        }

    }
    
class  Person : IMovable
    {
        
public   float  Speed
        {
            get
            {
                
return   20.5f ;
            }
        }
        
private   float  _MaxSpeed;
        
public   float  MaxSpeed
        {
            get { 
return   this ._MaxSpeed; }
            set { 
this ._MaxSpeed  =  value; }
        }
        
public   void  Run()
        {
            System.Console.WriteLine(
" Person is running " );
        }
        
public   void  Walk()
        {
            System.Console.WriteLine(
" Person is walking " );
        }
        
public   void  Fly()
        {
            System.Console.WriteLine(
" Person flies with a plane " );
        }

    }
    
class  Program
    {
        
static   void  SthMove(IMovable mov) {
            string type 
=  mov.GetType().Name;
            System.Console.WriteLine(
" This is a {0}  " ,type);
            System.Console.WriteLine(
" {0}.Speed={1} " ,type,mov.Speed);
            System.Console.WriteLine(
" {0}.MaxSpeed={1} " , type, mov.MaxSpeed);
            mov.Walk();
            mov.Run();
            mov.Fly();
        }
        
static   void  Main(string[] args)
        {
            Person aPerson 
=   new  Person();
            aPerson.MaxSpeed 
=   50 ;
            SthMove(aPerson);
            IMovable mov 
=   new  Dog();
            mov.MaxSpeed 
=   20 ;
            SthMove(mov);
            System.Console.ReadLine();
        }
    }
}

结果:
This is a Person
Person.Speed=20.5
Person.MaxSpeed=50
Person is walking
Person is running
Person flies with a plane
This is a Dog
Dog.Speed=10.5
Dog.MaxSpeed=20
Dog is walking
Dog is running
Dog can't fly



一个类可以实现多个接口。如有两个接口IMovable,ISpeakable,定义一个Person类来实现这两个接口
class Person:IMovable,ISpeakable
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值