首页 > 代码库 > C#中接口声明属性,但是提示“接口”中不能有属性。

C#中接口声明属性,但是提示“接口”中不能有属性。

C#中接口定义属性如下所示:

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5  6 namespace Ch08Ex05 7 { 8     abstract  class  HotDrink 9    {10       float  Milk;11       float Sugar;12      13       public int Drink(int x);14       public int AddMilk(int x);15       public int AddSugar(int x); 16 17    }    18 19      public interface ICup20   {21       int Color;22       int Volume;23       int Refill(int x);24       bool Wash();25 26    }27      class CupOfCoffee:HotDrink,ICup28     {29         int  BeanType;30         31         public int Drink(int x)32         {33            Console.WriteLine("Welcome,Drink a cup of Coffee??\n");34           35             return x;36         }37 38       ... ...39 }40 41  class CupOfTea:HotDrink,ICup42     {43         int  LeafType;44         45         public int Drink(int x)46         {47            Console.WriteLine("Welcome,Drink a cup of Tea??\n");48           49             return x;50         }51 52       ... ...53 }54 55     class  Program56   {57       static void Main(string[] args)58       {59 60          CupOfCoffee Coffee=new  CupOfCoffee();61          Coffee.Drink(2);62          ...63 64       }65 66 67 68   }69 70 71 }

 

上面代码提示错误为:

第21行和第22行,Interfaces cannot contain fields.

 

C#中接口声明属性,但是提示“接口”中不能有属性。