接口的声明
接口中不能有字段
接口的声明
建立项目—点击打开新建项—选择接口—接口声明
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _10._1接口的声明
{
public interface IEatable//接口默认为public,加不加public 都一样,类默认为private
{
//属性
string Name//接口不能有访问修饰符,例如public
{
get;//由于接口不能有字段,所以属性经常被写作自动属性
set;
}
//方法
void Write();//方法不能包含方法体
}
}
接口的实现和继承
- 接口中不能定义成员的实现,只允许我们定义成员(给成员起好名字,规划好是否有返回值)
- 不同接口(不包含派生)中允许有同名的成员
- 同一接口中成员名不能重名,即使类型不同
- 如果派生接口中对显示基接口中的成员进行重新定义时,需要使用new来解除警告
父类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _10._2接口的实现和继承
{
abstract class Bird
{
public abstract void Eat();
}
}
会飞功能接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _10._2接口的实现和继承
{
interface IFlyable
{
void Fly();
}
}
老鹰子类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _10._2接口的实现和继承
{
class Eagle:Bird,IFlyable
{
public void Fly()
{
Console.WriteLine("我是老鹰,我会飞");
}
public override void Eat()
{
Console.WriteLine("我是老鹰,我专吃小鸡");
}
}
}
麻雀子类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _10._2接口的实现和继承
{
class Sparrow:Bird,IFlyable
{
//接口的实现过程要在实现接口的类中进行
public void Fly()
{
Console.WriteLine("我是麻雀,我会飞");
}
public override void Eat()
{
Console.WriteLine("我是麻雀,我专吃粮食");
}
}
}
鸵鸟子类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _10._2接口的实现和继承
{
class Ostrich:Bird
{
public override void Eat()
{
Console.WriteLine("我是鸵鸟,我专吃青草");
}
}
}
天鹅子类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _10._2接口的实现和继承
{
class Swan:Bird,IFlyable
{
public override void Eat()
{
Console.WriteLine("我是天鹅,我吃鱼");
}
public void Fly()
{
Console.WriteLine("我是天鹅,我会飞");
}
}
}
气球子类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _10._2接口的实现和继承
{
class Balloon:IFlyable
{
public void Fly()
{
Console.WriteLine("我是气球,我会飞");
}
}
}
主程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _10._2接口的实现和继承
{
class Program
{
static void Main(string[] args)
{
IFlyable[] flys = { new Sparrow(), new Eagle(),new Swan(),new Balloon() };
foreach(IFlyable outFlys in flys)
{
outFlys.Fly();
}
Bird[] birds = { new Sparrow(), new Eagle(), new Swan() };
foreach(Bird outbird in birds)
{
outbird.Eat();
}
Console.ReadKey();
}
}
}
结果:
我是麻雀,我会飞
我是老鹰,我会飞
我是天鹅,我会飞
我是气球,我会飞
我是麻雀,我专吃粮食
我是老鹰,我专吃小鸡
我是天鹅,我吃鱼
显示实现接口
父类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _10._4显示实现接口
{
abstract class Bird
{
public abstract void Eat();
}
}
接口一
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _10._4显示实现接口
{
interface IFlyable1
{
void Fly();
}
}
接口二
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _10._4显示实现接口
{
interface IFlyable2
{
void Fly();
}
}
子类1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _10._4显示实现接口
{
class Eagle:Bird,IFlyable1
{
public void Fly()
{
Console.WriteLine("我是老鹰,我会飞");
}
public override void Eat()
{
Console.WriteLine("我是老鹰,我专吃小鸡");
}
}
}
子类二
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _10._4显示实现接口
{
class Sparrow:Bird,IFlyable1
{
public void Fly()
{
Console.WriteLine("(隐式实现)我是麻雀,我会飞");
}
void IFlyable1.Fly()
{
Console.WriteLine("(显示实现)我是麻雀,我会飞");
}
public override void Eat()
{
Console.WriteLine("我是麻雀,我专吃粮食");
}
}
}
子类三
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _10._4显示实现接口
{
class Balloon:IFlyable1,IFlyable2
{
//显示实现接口
void IFlyable1. Fly()
{
Console.WriteLine("我是1中的飞");
}
void IFlyable2.Fly()
{
Console.WriteLine("我是2中的飞");
}
}
}
主程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _10._4显示实现接口
{
class Program
{
static void Main(string[] args)
{
//隐式地实现接口
IFlyable1 myEagle1 = new Eagle();
myEagle1.Fly();//通过接口实现
Eagle myEagle2 = new Eagle();
myEagle2.Fly();//通过具体的类实现
IFlyable1 myBalloon1 = new Balloon();
myBalloon1.Fly();
IFlyable2 myBalloon2 = new Balloon();
myBalloon2.Fly();
//显示实现接口的作用就是指定方法到底是哪个接口的
//只能通过接口调用,不能通过具体类来调用
//同时显式和隐式实现接口
IFlyable1 mySparrow1 = new Sparrow();//当显示实现方法存在时,接口调用是显示实现
mySparrow1.Fly();
Sparrow mySparrow2 = new Sparrow();//方法调用是隐式实现
mySparrow2.Fly();
Console.ReadKey();
}
}
}
结果:
我是老鹰,我会飞
我是老鹰,我会飞
我是1中的飞
我是2中的飞
(显示实现)我是麻雀,我会飞
(隐式实现)我是麻雀,我会飞
抽象类与抽象方法
抽象方法必须写在抽象类中,抽象方法没有方法体
父类Pow
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _10._5抽象类与抽象方法的声明
{
public abstract class Pow
{
public abstract void PowMethod(int x,int y );
//abstract方法:没有自己的实现
//virtual方法:有自己的实现
//共同点:都可以通过override来实现对原有方法的重写
}
}
子类PowB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _10._5抽象类与抽象方法的声明
{
class PowB:Pow
{
public override void PowMethod(int x,int y)
{
int pow = 1;
for(int i=1;i<=y;i++)
{
pow *= x;
}
Console.WriteLine("求幂的结果是" + pow);
}
}
}
子类PowC
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _10._5抽象类与抽象方法的声明
{
class PowC:Pow
{
public override void PowMethod(int x,int y)
{
Console.WriteLine("求幂的结果是" + System.Math.Pow(x, y));
}
}
}
主程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _10._5抽象类与抽象方法的声明
{
class Program
{
static void Main(string[] args)
{
PowB myPowB = new PowB();
myPowB.PowMethod(2, 10);
PowC myPowC = new PowC();
myPowC.PowMethod(2, 10);
Console.ReadKey();
}
}
}
结果:
求幂的结果是1024
求幂的结果是1024
密封类与密封方法
如果一个类不希望将来在使用的过程中被继承,被派生,这时候可以采用关键字sealed
虚方法virtual,抽象方法abstract的存在就是为了继承,为了派生,因此不能被放在sealed类中
本章小结及任务实施
接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _10._7本章小结及任务实施
{
interface ISaywords
{
void Say();
}
}
真猫子类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _10._7本章小结及任务实施
{
class RealCat
{
public void Say()
{
Console.WriteLine("我是真猫,我不会说话");
}
}
}
Tom猫子类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _10._7本章小结及任务实施
{
class TomCat:ISaywords
{
public void Say()
{
Console.WriteLine("我是Tom猫,我会学别人说话");
}
}
}
Kitty猫子类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _10._7本章小结及任务实施
{
class KittyCat:ISaywords
{
public void Say()
{
Console.WriteLine("我是Kitty猫,我自己会说话");
}
}
}
主程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _10._7本章小结及任务实施
{
class Program
{
static void Main(string[] args)
{
ISaywords myCat1=new TomCat();
myCat1.Say();
ISaywords myCat2 = new KittyCat();
myCat2.Say();
RealCat myCat3 = new RealCat();
myCat3.Say();
Console.ReadKey();
}
}
}
结果:
我是Tom猫,我会学别人说话
我是Kitty猫,我自己会说话
我是真猫,我不会说话