簡體   English   中英

如何使用構造函數擴展類

[英]How to extend a class with a 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

該錯誤是由於以下原因引起的:

為類創建構造函數時,將不會為該類創建任何默認構造函數。 因此,如果擴展該類,並且子類嘗試調用其超類的no-arg構造函數,則將發生編譯時錯誤。

如此處所述: 類中的構造方法不能應用於給定類型

您已經為類創建了一個顯式構造函數。 任何顯式定義的構造函數都將消除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 } } 

首先,在這種情況下讓Main擴展CalCir是沒有意義的。

其次,返回您所詢問的特定問題。

當一個類(例如Child )從另一個類(例如Parent )擴展時,在Child的ctor中,它總是需要調用其父類的構造函數。 如果您未明確調用任何方法,則編譯器會自動假定您正在調用parent的no-arg構造函數。

例如

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

相當於

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

如果在Parent ,聲明了帶有參數的構造函數,但是沒有聲明任何參數:

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

Child調用Parent的no參數是非法的,因為它根本不存在。

因此,您需要明確告訴編譯器您要調用哪個ctor:

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

您是否需要擴展CalcCir的任何特殊原因? 您的CalCir有一個需要2個參數的構造函數,如果要將其擴展到您的主類,則需要在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");
            }
        }
    }
}

然后,您的Circle和Rectangle類:

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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM