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

  • 相关阅读:
    jvm调试相关:jmap失效下找到alternatives神器
    作业2:java内存模型图示
    Python脚本:Linux自动化执行Python脚本
    Nodejs:单线程为什么能支持高并发?
    作业1:java虚拟机内存模型图示
    SpringBoot中获取spring.profiles.active的值
    线程中使用注解出现空指针如何解决?
    java7:核心技术与最佳实践读书笔记——对象生命周期
    java7:核心技术与最佳实践读书笔记——类加载
    虚拟机上不了网啦
  • 原文地址:https://www.cnblogs.com/johnny/p/18153.html
Copyright © 2011-2022 走看看