接口
using System;
namespace Practice
{
public interface Food
{
//接口不能包含字段
//可在接口中定义属性且属性也不能实现
float Price { get; }
//在接口中定义方法
//1.不能添加访问修饰符,默认都是public
//2.在接口中的方法不能实现
void Eat();
}
public interface B
{
}
public class A
{
}
//Apple继承于A类且实现了Food接口和B接口(父类放在最前,接口放后且C#中只能继承一个父类,)
//一旦某个类实现了接口,就必须实现接口中定义的全部成员
public class Apple : A, Food,B
{
//实现接口中的Price属性
public float Price
{
get { return 1.4f; }
}
//实现接口中的Eat方法
public void Eat()
{
Console.WriteLine("HP+10");
}
}
class MainClass
{
public static void Main(string[] args)
{
Apple a=new Apple();
a.Eat();
Console.WriteLine(a.Price);
//不能直接实例化接口
Food b=new Apple();
b.Eat();
Console.WriteLine(b.Price);
}
}
}