[英]Java Inheritance constructor trouble
我正在尝试用Java实现一个具有父类RoundShapes和两个孩子Sphere和Cone的程序。 当我尝试在子类中创建构造函数时,它给我一个错误,指出该构造函数无法应用于该给定类型。 然后,我研究并找到了关于继承的教程,他们使用了一个超级构造函数。 我试图使用它,但是现在它给我一个错误,就是在超级构造函数之前不能使用半径?! 我不在构造函数的范围内,所以我不确定这表示什么。
import java.util.*;
public class Miller_A03Q4 {
public static void main(String[] args) {
Sphere sphere1 = new Sphere();
RoundShape cone1 = new RoundShape();
RoundShape.setRadius(4);
}
public static class RoundShape{
double area;
double volume;
double radius;
String shape;
//
public RoundShape(double r){
radius = r;
}
// set radius method
public void setRadius(double r){
radius = r;
}
// get area method
public double getArea(){
return area;
}
// get volume
public double getVolume(){
return volume;
}
}
// sphere is-a from roundshape
static class Sphere extends RoundShape{
public Sphere(double r){
radius = r;
}
//set area
public void setArea(){
area = 4 * Math.PI * radius * radius;
}
// get volume
public void setVolume(){
volume = (4/3) * Math.PI * radius * radius * radius;
}
}
//cone class is-a from roundshape
class Cone extends RoundShape{
double height;
//set area
public void setArea(){
area = Math.PI * radius * (radius + Math.sqrt( height * height + radius * radius));
}
// cone volume
public void setVolume(){
volume = Math.PI * radius * radius * (height/3);
}
}
}
所有构造函数都将通过调用super()
来调用super()
类的构造函数。 如果您忽略对super()
的调用,它将隐式插入该调用。
在您的情况下,您没有在您的超类中指定带有0个参数的构造函数,因此Java无法推断super()
的参数,因此它会引发编译器错误并要求您指定那些参数应该是。
要修复您的代码,您有2个选择
RoundShape
中创建一个RoundShape
0个参数的构造函数。 super()
在其中指定参数。 选项2看起来像这样。 请注意,对super()
的调用必须先于其他任何语句。
public Sphere(double r) {
super(r);
// other statements here
}
多亏了这个论坛上的建议和一些其他研究,我才得以更正我的程序。 我发布的原始代码中也有一些运行时错误,所以我想提供更新的代码,以防像我这样的新手偶然发现。
一些专业是:1.我不需要包括setArea()和setVolume()方法,而是将公式放在getArea()和getVolume()中。 2.我不需要在构造函数中包含体积和面积。 3.我还包括一个toString()方法,以便可以访问我的数据。
我确信这远非最好和最有效的代码,但似乎可行。 我在下面提供了新的和更新的代码以供参考。 感谢所有容忍我愚蠢的问题的人!
import java.util.*;
public class Miller_A03Q4 {
public static void main(String[] args) {
Sphere sphere1 = new Sphere(4);
Cone cone1 = new Cone(3,7);
System.out.println(cone1.toString());
System.out.println(sphere1.toString());
cone1.setHeight(10);
sphere1.setRadius(3);
System.out.println(cone1.toString());
System.out.println(sphere1.toString());
}
// parent Roundhape
public static class RoundShape{
double area;
double volume;
double radius;
String shape;
double height;
//
//constructor
public RoundShape(double r){
radius = r;
}
// set radius method
public void setRadius(double r){
radius = r;
}
// get area method
public double getArea(){
return area;
}
// get volume
public double getVolume(){
return volume;
}
//toString
public String toString(){
return "Shape: " + shape +" Radius: " + radius + " Height: "
+ height+ " Volume: " + this.getVolume() + " Area: "+ this.getArea();
}
}
// sphere is-a from roundshape
public static class Sphere extends RoundShape{
//constructor
public Sphere(double r){
super(r);
radius = r;
shape = "sphere";
}
//get area
public double getArea(){
area = 4 * Math.PI * radius * radius;
return area;
}
// get volume
public double getVolume(){
volume = 4 * Math.PI * (radius * radius * radius)/3;
return volume;
}
}
//cone class is-a from roundshape
public static class Cone extends RoundShape{
// cone with radius and height
public Cone(double r, double h){
super(r);
radius = r;
height = h;
shape = "cone";
}
//set height
public void setHeight(double h){
height = h;
}
// get area
public double getArea(){
area = Math.PI * radius * (radius + Math.sqrt( height * height + radius * radius));
return area;
}
// get volume
public double getVolume(){
volume = Math.PI * radius * radius * (height/3);
return volume;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.