设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 2719|回复: 0
打印 上一主题 下一主题

接口的例子

[复制链接]
跳转到指定楼层
1#
发表于 2014-3-26 13:12:04 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
今天汪老师给我出了一道题,要求以下:
新建一个C#的控制台程序工程,实现以下几个小功能
1、创建动物接口,内含  吃();喝();叫();跑(); 四个接口
2、创建猫和兔类,继承动物接口,实现每个吃喝叫跑功能
3、用WriteLine来表示一下就行了
我们假设兔子是不会叫的(好像只有遇到危险的时候会叫),也就是说兔子不应该有 叫()方法,这时候怎么处理?
我第一次写的代码
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;

  5. namespace Animal
  6. {
  7.     class Animal2
  8.     {
  9.         static void Main()
  10.         {
  11.             AnimalCanShout Cat = new AnimalCanShout();
  12.             AnimalCannotShout Rabbit = new AnimalCannotShout();
  13.             Cat.eat();
  14.             Cat.drink();
  15.             Cat.shout();
  16.             Cat.run();

  17.             Rabbit.eat();
  18.             Rabbit.drink();
  19.             Rabbit.run();
  20.             Console.ReadKey();
  21.         }
  22.     }
  23.     interface IAnimalCannotShout
  24.     {
  25.         void eat();
  26.         void drink();
  27.         void run();
  28.     }
  29.     interface IAnimalCanShout : IAnimalCannotShout
  30.     {
  31.         void shout();
  32.     }
  33.     public class AnimalCannotShout
  34.     {
  35.         public void eat()
  36.         {
  37.             System.Console.WriteLine("eating");
  38.         }
  39.         public void drink()
  40.         {
  41.             System.Console.WriteLine("drinking");
  42.         }
  43.         public void run()
  44.         {
  45.             System.Console.WriteLine("running");
  46.         }
  47.     }
  48.     public class AnimalCanShout : AnimalCannotShout
  49.     {
  50.         public void shout()
  51.         {
  52.             System.Console.WriteLine("shouting");
  53.         }
  54.     }
  55. }
复制代码
经过汪老师提示后,改了代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;

  5. namespace Animal
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Cat cat = new Cat();
  12.             Rabbit rabbit = new Rabbit();
  13.             cat.eat();
  14.             cat.drink();
  15.             cat.shout();
  16.             cat.run();

  17.             rabbit.eat();
  18.             rabbit.drink();
  19.             rabbit.run();

  20.             System.Console.ReadKey();
  21.         }
  22.     }
  23.     public interface Ieat
  24.     {
  25.         void eat();
  26.     }
  27.     public interface Irun
  28.     {
  29.         void run();
  30.     }
  31.     public interface Ishout
  32.     {
  33.         void shout();
  34.     }
  35.     public interface Idrink
  36.     {
  37.         void drink();
  38.     }
  39.     public class Cat : Ieat, Idrink, Ishout, Irun
  40.     {
  41.         public void eat()
  42.         {
  43.             System.Console.WriteLine("eating");
  44.         }

  45.         public void drink()
  46.         {
  47.             System.Console.WriteLine("drinking");
  48.         }
  49.         public void shout()
  50.         {
  51.             System.Console.WriteLine("shouting");
  52.         }

  53.         public void run()
  54.         {
  55.             System.Console.WriteLine("running");
  56.         }
  57.     }
  58.     public class Rabbit : Ieat, Idrink, Irun
  59.     {
  60.         public void eat()
  61.         {
  62.             System.Console.WriteLine("eating");
  63.         }

  64.         public void drink()
  65.         {
  66.             System.Console.WriteLine("drinking");
  67.         }
  68.         public void run()
  69.         {
  70.             System.Console.WriteLine("running");
  71.         }
  72.     }
  73. }
复制代码
第一次的代码,感觉是散的,猫的类与兔子的类没有什么交集。第二次的代码,把动物分类二类了,一类会叫的,一类不会叫的,动物都有吃喝跑功能。
写第一次代码时,不会用接口的继承,与类的继承,所以感觉写得很散。


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-5 02:48 , Processed in 0.090159 second(s), 24 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表