我很难将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 无法应用于给定类型;类 主要扩展 calCir我对 Java 相当陌生,所以我仍然对如何使用继承感到困惑
如果需要,这是完整的代码https://repl.it/NA5S/8
此错误是由于以下原因造成的:
为类创建构造函数时,不会为该类创建任何默认构造函数。 因此,如果扩展该类并且子类尝试调用其超类的 no-arg 构造函数,则会出现编译时错误。
如此处所述:类中的构造函数不能应用于给定类型
您已经为类创建了一个显式构造函数。任何显式定义的构造函数都将消除 Java 将隐式使用的默认 no-args 构造函数。
下面是您创建的构造函数:
CalCir(double r) {
radius = r;}
为了根据请求使用继承,您可以执行以下任一操作。
从父类中删除显式构造函数。
将第二个不带参数的构造插入到父类中:
CalCir() { // Set the radius to default of zero this(0); }
重写子类中的默认构造函数:
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 中,它总是需要调用其父级的构造函数。 如果你没有显式调用任何,编译器将自动假定你正在调用父级的no-arg构造函数。
例如
class Child extends Parent {
Child() {
// do something
}
}
相当于
class Child extends Parent {
Child() {
super();
// do something
}
}
如果在 Parent
中,声明了一个带有参数的构造函数,但没有声明 arg ctor:
class Parent {
Parent(int foo) {...}
}
Child
调用Parent
的no-arg ctor是非法的,因为它根本不存在。
所以你需要明确地告诉编译器你要调用哪个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:nnRectangle --> (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 choicen");
}
} catch (Exception e) {
System.out.println("Not valid choicen");
}
}
}
}
然后你的圆形和矩形类:
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();
}