zoukankan      html  css  js  c++  java
  • 多重继承

    C#中是不能直接多重继承的,以下代码将出错:
    using System;

    public class m
    {
     
    static void Main()
     
    {
      point p
    =new point();
      Console.WriteLine(p.x.ToString());
      pointgame pg
    =new pointgame();
      Console.WriteLine(pg.nums.ToString());
     }

    }


    class point
    {
     
    public int x=0;
     
    public int y=0;
    }


    class game
    {
     
    public int stage=0;
    }


    class pointgame:point, game
    {
     
    public int nums=0;
    }
    编译器提示:
    p.cs(25,24): error CS1721: 类“pointgame”不能具有多个基类:“point”和“game”
    p.cs(20,7): (与前一个错误相关的符号位置)
    但网络上说可以通过“接口”来实现类似的多重继承(http://dev.csdn.net/article/17/17661.shtm)。我先把以上代码中:
    class point改为interface point
    class game改为interface game
    但编译器提示:p.cs(14,13): error CS0525: 接口不能包含字段,我把对那几个变量的初始化值(=0)给删除掉,编译依旧,看来接口的严格定义我还得去了解一下。参考别人的代码,都只是把方法体现在接口中。于是继续改代码,point接口体现show方法,game接口体现start与over方法,中间还出现:接口中不能用public修饰符,把public去掉,现在编译器提示:pointgame不会实现接口成员point.show(),看来,最后还是要实现内容,不过回头想一下,整段代码都没有体现出show的具体执行什么内容,我就希望程序给出执行出来,真可笑。
    现在要实现以上三个方法,要么在新的类中写相应代码,要么先创建好对应的“父”类实例,然后“继承”。应该说实际的过程是,先有“父”类,然后写接口,新类再从“父”类中“继承”下来。
    于是我先增加一个game的类,加完就有这个问题:p.cs(32,11): error CS0101: 命名空间“<global namespace>”已经包含了“game”的定义
    p.cs(24,7): (与前一个错误相关的符号位置)
    参考别人的代码,才知道别人对类与接口确实是用两个不同的名字。改名后编译通过。
    using System;

    public class m
    {
     
    static void Main()
     
    {
      pointgame pg
    =new pointgame();
      pg.start();
      pg.show();
      pg.over();
     }

    }


    interface point
    {
     
    void show();
    }


    class game
    {
     
    public void start()
     
    { Console.WriteLine("start game"); }
     
    public void over()
     
    { Console.WriteLine("game over."); }
    }


    interface i_game
    {
     
    void start();
     
    void over();
    }


    class pointgame:point, i_game
    {
     
    public int nums=0;
     
    private game g=new game();
     
    public void show()
     
    { Console.WriteLine("nums=" + this.nums); }
     
    public void start()
     
    { g.start(); }
     
    public void over()
     
    { g.over(); }
    }
    我现在新类是都从两个接口来,其实可以一个来自类,另一个是接口。例如,pointgame主要还是继承game,因此我把它继承于game,而point则用接口来实现多重继承。
    using System;

    public class m
    {
     
    static void Main()
     
    {
      pointgame pg
    =new pointgame();
      pg.start();
      pg.show();
      pg.over();
     }

    }


    class point
    {
     
    public int x=0;
     
    public void show()
     
    { Console.WriteLine("x=" + x.ToString()); }
    }


    interface i_point
    {
     
    void show();
    }


    class game
    {
     
    public void start()
     
    { Console.WriteLine("start game"); }
     
    public void over()
     
    { Console.WriteLine("game over."); }
    }


    class pointgame:i_point, game
    {
     
    public int nums=0;
     
    private point p=new point();
     
    public void show()
     
    { p.show(); }
    }

    以上代码编译时,会出现:“error CS1722: 基类“game”必须在任何接口之前”的错误,对调一下就好。
    使用接口,以及在新类中实例化“父”类,针对各个成员函数进行编码,这种方法可以解释我最初的一个疑惑,即父类如果有同名的成员函数时,到底是实现哪一个成员函数。反正看谁接口有定义,新类中具体是实现哪一个父类的功能(测试过,接口有重复。

    最后,我感觉这不像是多重继承,因为接口还要自己写,新的类的成员函数代码也要自己写。但毕竟比完全没有好,至少程序内部的代码不必重新写。

  • 相关阅读:
    js 实现 间隙滚动效果
    js实现无缝滚动
    js 实现弹出层效果
    jQuery实现网页定位导航
    右侧定位的样式
    jQuery实现点击按钮展开和收起
    js实现点击按钮控制展开与收起.
    【BZOJ1202】[HNOI2005]狡猾的商人 带权并查集
    【BZOJ1067】[SCOI2007]降雨量 RMQ+特判
    【BZOJ1367】[Baltic2004]sequence 左偏树
  • 原文地址:https://www.cnblogs.com/yzx99/p/1210508.html
Copyright © 2011-2022 走看看