我如何实现类和接口?

编程入门 行业动态 更新时间:2024-10-24 16:23:28
本文介绍了我如何实现类和接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

以下信息来自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); } } }

推荐答案

更多推荐

我如何实现类和接口?

本文发布于:2023-11-04 05:36:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1557091.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何实现   接口

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!