c#入门经典(第8章)习题

 <img src="https://p-blog.csdn.net/images/p_blog_csdn_net/perisenjut/EntryImages/20091024/TM截图未命名.png"></img>

 

 

为一个函数编写一些代码,接受上述实例的两个杯子对象中的一个,作为一个参数。此函数可以为它传送的任何杯子对象调用Addmilk()、drink()、wash()方法

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    //--------------------------------------------------------------------
    public interface ICup                            // 定义接口
    {
        void Wash();                                 // 空方法
    }

    //--------------------------------------------------------------------
    public abstract class HotDrink              // 抽象类
    {
        public abstract void Drink();            // 声明抽象方法

        public abstract void AddMilk();         // 声明抽象方法
    }

    //--------------------------------------------------------------------
    public class CupOfCoffee : HotDrink, ICup   // 继承于抽象类与接口
    {
        public string dd;                            // 声明字段
        public string aa;
        public string ww;

        public CupOfCoffee( string d, string a, string w )  // 构造函数
        {
            dd = d;                                      // 初始字段
            aa = a;
            ww = w;
        }

        public override void Drink()            // 重写方法
        {
            Console.WriteLine( "想喝什么? {0}", dd );

            Console.WriteLine();
        }

        public override void AddMilk()          // 重写方法
        {
            Console.WriteLine( "想加奶吗? {0}", aa );

            Console.WriteLine();
        }

        public void Wash()                      // 实现接口中的方法
        {
            Console.WriteLine( "要洗手吗? {0}", ww );

            Console.WriteLine();
        }
    }

    //--------------------------------------------------------------------
    public class CupOfTea : HotDrink, ICup      // 继承于抽象类与接口
    {
        public string dd;                       // 声明字段
        public string aa;
        public string ww;

        public CupOfTea( string d, string a, string w ) // 构造函数
        {
            dd = d;                             // 初始字段
            aa = a;
            ww = w;
        }

        public override void Drink()            // 重写方法
        {
            Console.WriteLine( "想喝什么? {0}", dd );

            Console.WriteLine();
        }

        public override void AddMilk()          // 重写方法
        {
            Console.WriteLine( "想加奶吗? {0}", aa );

            Console.WriteLine();
        }

        public void Wash()                      // 实现接口中的方法
        {
            Console.WriteLine( "要洗手吗? {0}", ww );

            Console.Read();
        }
    }

    //--------------------------------------------------------------------
    class Program
    {
        static void Main(string[] args)
        {
            CupOfCoffee aa = new CupOfCoffee( "咖啡", "可以", "谢谢!" );

            aa.Drink();
            aa.AddMilk();
            aa.Wash();

            CupOfTea bb = new CupOfTea( "绿茶", "少来", "狗屁!" );

            bb.Drink();
            bb.AddMilk();
            bb.Wash();
        }
    }

    //--------------------------------------------------------------------
}

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值