Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
关于java重写中的方法_Java_Oop - Fatal编程技术网

关于java重写中的方法

关于java重写中的方法,java,oop,Java,Oop,这是超类:Glyph public class Glyph { void draw() { System.out.println("Glyph.draw("); } Glyph() { System.out.println("Glyph() before draw()"); draw(); System.out.println("Glyph() after draw()"); } } class R

这是超类:
Glyph

public class Glyph {
    void draw() {
        System.out.println("Glyph.draw(");
    }
    Glyph() {
        System.out.println("Glyph() before draw()");
        draw();
        System.out.println("Glyph() after draw()");
    }
}
class RoundGlyph extends Glyph {
    int radius = 1;
    RoundGlyph(int r) {
        super();
        radius = r;
        System.out.println("RoundGlyph.RoundGlyph(), radius = " + radius);
    }

    void draw() {
        System.out.println("RoundGlyph.draw(), radius = " + radius);
    }
}
这是子类
RoundGlyph

public class Glyph {
    void draw() {
        System.out.println("Glyph.draw(");
    }
    Glyph() {
        System.out.println("Glyph() before draw()");
        draw();
        System.out.println("Glyph() after draw()");
    }
}
class RoundGlyph extends Glyph {
    int radius = 1;
    RoundGlyph(int r) {
        super();
        radius = r;
        System.out.println("RoundGlyph.RoundGlyph(), radius = " + radius);
    }

    void draw() {
        System.out.println("RoundGlyph.draw(), radius = " + radius);
    }
}
我们使用这个类来测试

public class PolyConstructors {
    public static void main(String[] args) {
        new RoundGlyph(5);
    }
}
输出:

Glyph() before draw()
 RoundGlyph.draw(), radius = 0
 Glyph() after draw()
 RoundGlyph.RoundGlyph(), radius = 5
为什么程序在执行
super()
时在类
RoundGlyph
中执行方法
draw()

和打印

RoundGlyph.draw(), radius = 0

这是一个多态性!当您从父级调用方法时,如果存在重写,则子级中的方法将被调用。如果要调用parent的方法,必须执行以下操作:

class RoundGlyph extends Glyph {
   int radius = 1;
   RoundGlyph(int r) {
    super();
    radius = r;
    System.out.println("RoundGlyph.RoundGlyph(), radius = " + radius);
  }
@Override
void draw() {
   super.draw();//go draw in parent first
   System.out.println("RoundGlyph.draw(), radius = " + radius);
 }
}
public class Glyph {
 void draw() {
  System.out.println("Glyph.draw(");
 }
 Glyph() {
  System.out.println("Glyph() before draw()");
  Glyph.draw();//explicit calling
  System.out.println("Glyph() after draw()");
 }
}
或者您必须在父级中进行显式方法调用,如下所示:

class RoundGlyph extends Glyph {
   int radius = 1;
   RoundGlyph(int r) {
    super();
    radius = r;
    System.out.println("RoundGlyph.RoundGlyph(), radius = " + radius);
  }
@Override
void draw() {
   super.draw();//go draw in parent first
   System.out.println("RoundGlyph.draw(), radius = " + radius);
 }
}
public class Glyph {
 void draw() {
  System.out.println("Glyph.draw(");
 }
 Glyph() {
  System.out.println("Glyph() before draw()");
  Glyph.draw();//explicit calling
  System.out.println("Glyph() after draw()");
 }
}

阅读多态性和重写方法。在任何情况下,正如您的代码所示,这也是您不应该从构造函数调用实例方法的原因。