以下信息来自visual visual c#2012一书。我试图从这些信息实现类和接口。我试着做但却失败了。有没有办法在界面中声明速溶,牛奶,糖和描述,并分别在类咖啡和茶类中声明beantype或leaftype。 << Interface>> CupOfCoffee - IHotDrink(接口) IHotDrink + BeanType:string -------------- +瞬间:bool CupOfTea - IHotDrink(接口) + Milk:bool + LeafType:string + Sugar:byte +描述:String 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); } } }推荐答案
更多推荐
我如何实现类和接口?
发布评论