我如何实现类和接口? [英] How do I implement classes and interfaces?

查看:96
本文介绍了我如何实现类和接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下信息来自visual visual c#2012一书。我试图从这些信息实现类和接口。我试着做但却失败了。有没有办法在界面中声明速溶,牛奶,糖和描述,并分别在类咖啡和茶类中声明beantype或leaftype。



<< Interface>> CupOfCoffee - IHotDrink(接口)

IHotDrink + BeanType:string

--------------

+瞬间:bool CupOfTea - IHotDrink(接口)

+ Milk:bool + LeafType:string

+ Sugar:byte

+描述:String < br $> b $ b

The following information is from "beginning visual c#2012" book. I am trying to implement the classes and interface from these information. I tried to do it but failed. Is there a way to declare instant, milk, sugar and description in interface and declare beantype or leaftype in class coffee and class tea respectively.

<<Interface>> CupOfCoffee - IHotDrink(Interface)
IHotDrink + BeanType:string
--------------
+Instant: bool CupOfTea - IHotDrink (Interface)
+Milk : bool + LeafType:string
+Sugar : byte
+Description : String

namespace Interfaces
{
    interface IHotDrink
    class Coffee: IHotDrink
    {
        public string BeanType;
        public string Description;
        public string Sugar;
        public string Milk;
    }
    class Tea: IHotDrink
    {
        public string LeafType;
        public string Description;
        public string Sugar;
        public string Milk;
    }
    class Program
    {
        static void PrintInfo(IHotDrink item)
        {
            Console.WriteLine("BeanType: {0}, Description: {1}, Sugar: {2}, Milk:  {3}",item.BeanType, item.Description, item.Sugar,
                item.Milk);
        }
        static void PrintInfo(Tea item)
        {
            Console.WriteLine("LeafType: {0}, Description: {1}, Sugar: {2}, Milk:  {3}", item.LeafType, item.Description, item.Sugar,
                item.Milk);
        }
        static void Main()
        {
            Coffee c = new Coffee() { BeanType = "Medium Roast", Description = "Large", Sugar ="No", Milk = "Yes"  };
            Tea t = new Tea() { LeafType = "Green", Description = "Small", Sugar = "No", Milk = "Yes" };
            PrintInfo(c);
            PrintInfo(t);
        }
    }
}

推荐答案

这篇关于我如何实现类和接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆