Calling a base class constructor from derived class in Java

前端 未结 3 1424
迷失自我
迷失自我 2020-12-18 18:43

I have a class as follows:

public class Polygon  extends Shape{

    private int noSides;
    private int lenghts[];

    public Polygon(int id,Point center,         


        
相关标签:
3条回答
  • 2020-12-18 18:47
    class Foo {
        Foo(String str) { }
    }
    
    class Bar extends Foo {
        Bar(String str) {
            // Here I am explicitly calling the superclass 
            // constructor - since constructors are not inherited
            // you must chain them like this.
            super(str);
        }
    }
    
    0 讨论(0)
  • 2020-12-18 18:50

    Your constructor should be

    public Regularpolygon extends Polygon{
    
    public Regularpolygon (int id,Point center,int noSides,int lengths[]){
    super(id, center,noSides,lengths[]);
    
    // YOUR CODE HERE
    
    }
    
    }
    
    0 讨论(0)
  • 2020-12-18 18:53
    public class Polygon  extends Shape {    
        private int noSides;
        private int lenghts[];
    
        public Polygon(int id,Point center,int noSides,int lengths[]) {
            super(id, center);
            this.noSides = noSides;
            this.lenghts = lengths;
        }
    }
    
    public class RegularPolygon extends Polygon {
        private static int[] getFilledArray(int noSides, int length) {
            int[] a = new int[noSides];
            java.util.Arrays.fill(a, length);
            return a;
        }
    
        public RegularPolygon(int id, Point center, int noSides, int length) {
            super(id, center, noSides, getFilledArray(noSides, length));
        }
    }
    
    0 讨论(0)
提交回复
热议问题