• 看看这段程序,为什么这样的“继承”会出错?


    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就会出现如上错误呢?

  • 相关阅读:
    Tutorial: Getting Started with TFS in VS2010 (转载)
    Flex开源组件——FlexReport
    GIS和开源见解(摘录)
    Google Wave了解(资料收集)
    分享ArcGis For Flex API 1.3 Diagram
    基于.NET的开源GIS项目收集整理(转载)
    TFS 2010 for SourceSafe Users (转载)
    一个处理UTF8编码文件BOM头的简单方法
    用ISO文件制作启动U盘
    为Ubuntu安装buildessential软件包
  • 原文地址:https://www.cnblogs.com/johnny/p/18153.html
Copyright © 2020-2023  润新知