zoukankan      html  css  js  c++  java
  • 看看这段程序,为什么这样的“继承”会出错?

    abstract class Glyph {
      abstract void draw();
      Glyph() {
        System.out.println("Glyph() before draw()");
        draw();
        System.out.println("Glyph() after draw()");
      }
    }

    class RoundGlyph01 extends Glyph {
      int radius=0;
      String s;
      RoundGlyph01(int r,String s) {
       radius=r;
       this.s=s;
       System.out.println("RoundGlyph01.RoundGlyph(),radius="+radius+" "+s);
      }
      void draw() {
        System.out.println("RoundGlyph01.draw(),radius="+radius+" "+s);
      }
    }

    class RoundGlyph02 extends Glyph {
      int radius=0;
      String s;
      RoundGlyph02(int r,String s) {
       radius=r;
       this.s=s;
       System.out.println("RoundGlyph02.RoundGlyph(),radius="+radius+" "+s);
      }
      void draw() {
        System.out.println("RoundGlyph02.draw(),radius="+radius+" "+s);
      }
    }

    class RoundGlyph03 extends RoundGlyph01 {
      int radius=0;
      String s;
      RoundGlyph03(int r,String s) {
       radius=r;
       this.s=s;
       System.out.println("RoundGlyph03.RoundGlyph(),radius="+radius+" "+s);
      }
      void draw() {
        System.out.println("RoundGlyph03.draw(),radius="+radius+" "+s);
      }
    }

    public class PolyConstructors {
      public static void main(String[] args) {
        System.out.println("Creating object RoundDlyph01...");
        new RoundGlyph01(5,"String");
        System.out.println("**********************************************");
        System.out.println("Creating object RoundDlyph02...");
        new RoundGlyph02(5,"String");
        System.out.println("**********************************************");
        System.out.println("Creating object RoundDlyph03...");
        new RoundGlyph03(5,"String");
        System.out.println("**********************************************");
      }
    }
     

    编译结果:
    PolyConstructors.java:40: cannot resolve symbol
    symbol  : constructor RoundGlyph01 ()
    location: class RoundGlyph01
      RoundGlyph03(int r,String s) {
                                   ^
    1 error

    经过测试,如果我把RoundGlyph03 继承Glyph而非RoundGlyph01,一切正常,那么为什么继承RoundGlyph01就会出现如上错误呢?

  • 相关阅读:
    打印沙漏
    秋季学期学习总结
    bzoj1059[ZJOI2007]矩阵游戏 二分图匹配
    bzoj1055[HAOI2008]玩具取名 区间dp
    bzoj1053[HAOI2007]反素数ant
    bzoj1049[HAOI2006]数字序列
    bzoj1046[HAOI2007]上升序列
    bzoj1044[HAOI2008]木棍分割 单调队列优化dp
    bzoj3930[CQOI2015]选数 容斥原理
    bzoj1069 [SCOI2007]最大土地面积 旋转卡壳
  • 原文地址:https://www.cnblogs.com/johnny/p/18153.html
Copyright © 2011-2022 走看看