如何使用构造函数扩展类

时间:2017-10-27 00:55:38

标签: java inheritance methods constructor

我很难将calCir类扩展到主类 我有一个提供

的构造函数
class calCir {
    double radius;
    calCir(double r) {
        radius = r;
    }

    double AreaCircle() {
        return Math.PI * (radius * radius);
    }
    double CircumferenceCircle() {
        return 2 * Math.PI * radius;
    }
}

我想使用Main扩展calCir但由于构造函数

而得到错误
class Main{
public static void main(String args[]) {

错误:类calCir中的构造函数calCir无法应用于给定类型; class Main扩展了calCir 我是Java的新手,所以我仍然对如何使用继承感到困惑

如果需要,这是完整的代码 https://repl.it/NA5S/8

4 个答案:

答案 0 :(得分:3)

此错误是由于以下原因:

为类创建构造函数时,不会为该类创建任何默认构造函数。因此,如果你扩展该类,并且如果子类试图调用其超类的no-arg构造函数,则会出现编译时错误。

如上所述:Constructor in class cannot be applied to given types

答案 1 :(得分:1)

您已为类创建了显式构造函数。任何显式定义的构造函数都将消除Java将隐式使用的默认no-args构造函数。

以下是您创建的构造函数:

CalCir(double r) {
    radius = r;}

为了按要求使用继承,您可以执行以下任何操作。

  1. 从父类中删除显式构造函数。

  2. 将没有参数的第二个构造插入父类:

    CalCir()
    {
        // Set the radius to default of zero
        this(0);
    }
    
  3. 覆盖子类中的默认构造函数:

    public class MainSubClass extends CalCir
    {
        public MainSubClass()
        {
           // Set the radius to default of zero
           super(0);
        }
    
        public static void main(final String args[])
       {
        // Insert program here
       }
    }
    

答案 2 :(得分:0)

首先,在这种情况下Main扩展CalCir毫无意义。

其次,回到你提出的具体问题。

当您有一个类(例如Child)从另一个(例如Parent)扩展时,在Child的ctor中,它总是需要调用其父级的构造函数。如果您没有明确地调用any,编译器将自动假设您正在调用parent的no-arg构造函数。

e.g。

class Child extends Parent {
    Child() {
        // do something
    }
}

相当于

class Child extends Parent {
    Child() {
        super();
        // do something
    }
}

如果在Parent中,声明了带参数的构造函数,但声明了no-arg ctor:

class Parent {
     Parent(int foo) {...}
}

Child调用Parent的无法参与者是非法的,因为它根本不存在。

因此,您需要明确告诉编译器您要调用哪个ctor:

class Child extends Parent {
    Child() {
        super(123);
        // do something
    }
}

答案 3 :(得分:0)

您需要扩展CalcCir的任何特殊原因?你的CalCir有一个构造函数需要2个args,如果要将它扩展到你的主类,那么你就可以在main中创建一个构造函数:

public Main(double radius) {
    // define your params to parent here or have it passed in constructor...
    super(param1, param2); // matching your super class
}

根据您提供的链接,这似乎更合适:

包含起点的主要课程:

public class Main {

    public static void main(String[] args) {
        Scanner b = new Scanner(System.in);
        while (true) {
            try {
                System.out.println("Determining the area/perimeter of a 2D shape.");
                System.out.println("Choose a shape:\n\nRectangle --> (Type a or rectangle)\nCircle    --> (Type b or circle)");
                String shape = b.nextLine();

                if ((shape.equalsIgnoreCase("Rectangle")) || (shape.equalsIgnoreCase("a"))) {
                    System.out.println("Input Length");
                    double length = b.nextDouble();
                    System.out.println("Input width");
                    double width = b.nextDouble();

                    Shape rectangle = new Rectangle(length, width);

                    System.out.println("Area of rectangle is " + rectangle.getArea());
                    System.out.println("The perimeter is " + rectangle.getPerimeter());
                    if (length == width){
                        System.out.println("This is a special type of reactangle, a square!");
                    }
                    break;
                } else if ((shape.equalsIgnoreCase("circle")) || (shape.equalsIgnoreCase("b"))) {
                    System.out.println("Input Radius");
                    double radius = b.nextDouble();

                    Shape circle = new Circle(radius);

                    System.out.println("Area of circle is " + circle.getArea());
                    System.out.println("The circumference is " + circle.getPerimeter());
                    break;
                } else {
                    System.out.println("Not valid choice\n");
                }
            } catch (Exception e) {
                System.out.println("Not valid choice\n");
            }
        }
    }
}

然后你的圆和矩形类:

public class Circle extends Shape {

    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    @Override
    double getArea() {
        return Math.PI * (radius * radius);
    }

    @Override
    double getPerimeter() {
        return 2 * Math.PI * radius;
    }
}

public class Rectangle extends Shape {

    private double length;
    private double width;

    public Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }

    @Override
    double getArea() {
        return length * width;
    }

    @Override
    double getPerimeter() {
        return 2 * (length + width);
    }
}

其中两者均继承自形状

public abstract class Shape {

    abstract double getArea();

    abstract  double getPerimeter();
}
相关问题