■接口:使用关键字interface,只是定义一系列的行为类型,不提供任何实现
interface IMovable{
float speed{get;}
float maxSpeed{get;set;}
void Run();
void Walk();
void Fly();
}
■接口的实现,通过冒号来实现
一个类可以实现多个接口。如有两个接口IMovable,ISpeakable,定义一个Person类来实现这两个接口
class Person:IMovable,ISpeakable
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
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