zoukankan      html  css  js  c++  java
  • 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 ICup
    20   {
    21       int Color;
    22       int Volume;
    23       int Refill(int x);
    24       bool Wash();
    25 
    26    }
    27      class CupOfCoffee:HotDrink,ICup
    28     {
    29         int  BeanType;
    30         
    31         public int Drink(int x)
    32         {
    33            Console.WriteLine("Welcome,Drink a cup of Coffee??
    ");
    34           
    35             return x;
    36         }
    37 
    38       ... ...
    39 }
    40 
    41  class CupOfTea:HotDrink,ICup
    42     {
    43         int  LeafType;
    44         
    45         public int Drink(int x)
    46         {
    47            Console.WriteLine("Welcome,Drink a cup of Tea??
    ");
    48           
    49             return x;
    50         }
    51 
    52       ... ...
    53 }
    54 
    55     class  Program
    56   {
    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.

  • 相关阅读:
    原生态 php连接mysql
    sql查询慢 查找
    死锁查询和处理
    linq详细案例
    linq深入
    DataTable 与XML 交互
    DataTable运用
    通过反射修改已有数组的大小
    通过反射取得并修改数组信息
    通过反射机制直接操作属性
  • 原文地址:https://www.cnblogs.com/meihao1989/p/4120828.html
Copyright © 2011-2022 走看看