我已经制作了以下类和子类。
import java.io.*;
import java.util.*;
public class ShapeTester{
//---------------------------------------------------------------------------
// Read the dimensions of various 3-D shapes from an input file, then
// displays pertinent information about each shape.
//---------------------------------------------------------------------------
public static void main (String[] args){
try{
Scanner scan = new Scanner (new File("shapes.txt"));
double width, length, height, side, radius;
// Read the data from the input file
while (scan.hasNext()){
String shapeType = scan.next();
if (shapeType.equalsIgnoreCase("cylinder")){
radius = scan.nextDouble();
height = scan.nextDouble();
System.out.println (new Cylinder (radius, height));
}
if (shapeType.equalsIgnoreCase("cone")){
radius = scan.nextDouble();
height = scan.nextDouble();
System.out.println (new Cone (radius, height));
}
if (shapeType.equalsIgnoreCase("cuboid")){
length = scan.nextDouble();
width = scan.nextDouble();
height = scan.nextDouble();
System.out.println (new Cuboid (length, width));
}
}
}
catch (Exception except){
System.err.println(except);
}
}
}//end class ShapeTester
public abstract class Shape{
abstract public double computeArea();
abstract public double computePerimeter();
}//end class Shape
import java.text.*;
public class Circle extends Shape{
protected double radius;
protected static DecimalFormat form = new DecimalFormat("0.##");
public Circle (double rad){
radius = rad;
}
public double computeArea(){
return Math.pow(radius,2) * 3.14;
}
public double computePerimeter(){
return radius * 6.28;
}
public String toString(){
return "Circle: radius is " + form.format(radius) +
"\nperimeter is " + form.format(computePerimeter()) +
", area is " + form.format(computeArea());
}
} //end class Circle
public class Cylinder extends Circle{
private double height;
public Cylinder (double rad, double hei){
super(rad);
height = hei;
}
// Returns the calculated value of face height
public double faceHeight(){
return height;
}
// Returns the calculated value of face area
public double faceArea(){
return faceHeight() * radius * 6.28;
}
// Returns the calculated value of the surface area
public double computeArea(){
return faceArea() + super.computeArea() + super.computeArea();
}
// Returns the calculated value of the volume
public double computeVolume(){
return super.computeArea() * height;
}
public String toString(){
return "Cylinder: Height is " + form.format(height) +
"\nperimeter of base is " + form.format(computePerimeter()) +
", area is " + form.format(computeArea()) +
"\nvolume is " + form.format(computeVolume()) + "\n";
}
}//end class Cylinder
到目前为止,该程序运行良好。我可以编译ShapeTester 我收到了每个值的预期结果。(那是在我之前 插入了长方体的if命令)(还有一个if命令 对于那里的锥形,它也有效,我只是没有包括锥形类 因为这已经足够长了。)
import java.text.*;
public class Rectangle extends Shape{
protected double length;
protected double width;
protected static DecimalFormat form = new DecimalFormat("0.##");
public Rectangle (double len, double wid){
length = len;
width = wid;
}
public double computeArea(){
return length * width;
}
public double computePerimeter(){
return (2*length) + (2*width);
}
public String toString(){
return "Rectangle: length is " + form.format(length) +
"\nwidth is " + form.format(width) +
"\nperimeter is " + form.format(computePerimeter()) +
", area is " + form.format(computeArea());
}
}//end class Rectangle
这节课也很好。 以下类是给我一个问题的类。
public class Cuboid extends Rectangle{
private double height;
public Cuboid (double len, double wid, double hei){
super(len);
super(wid);
height = hei;
}
// Returns the calculated value of the surface area
public double computeArea(){
return (2 * height * width) + (2 * super.computeArea()) + (2 * length * height) ;
}
// Returns the calculated value of the volume
public double computeVolume(){
return super.computeArea() * height;
}
public String toString(){
return "Rectangle Cuboid: Height is " + form.format(height) +
"\nlength is " + form.format(length) +
"\nwidth is " + form.format(width) +
"\nperimeter of base is " + form.format(computePerimeter()) +
", area is " + form.format(computeArea()) +
"\nvolume is " + form.format(computeVolume()) + "\n";
}
}//end class Cuboid
当我尝试编译这个最后一个类时,我得到了这些错误,但我遵循了与其他类(Cylinder)相同的格式。有人可以告诉我为什么吗?
Cuboid.java:5: error: constructor Rectangle in class Rectangle cannot be applied to given types;
super(len);
^
required: double,double
found: double
reason: actual and formal argument lists differ in length
Cuboid.java:6: error: call to super must be first statement in constructor
super(wid);
^
2 errors
答案 0 :(得分:2)
You must call super
exactly as the super constructor is defined.您不希望通过编写
Rectangle
构造函数
new Rectangle(lenth);
new Rectangle(height);
...对吧?你打电话给new Rectangle(length, height)
。所以,改变
public Cuboid (double len, double wid, double hei){
super(len);
super(wid);
height = hei;
}
到
public Cuboid (double len, double wid, double hei){
super(len, wid);
height = hei;
}
答案 1 :(得分:1)
Cuboid
的{{1}}超类会收到两个参数,但是当您使用Rectangle
在子类Rectangle
中调用它时,您只提供一个参数。而不是:
super
执行此操作: super(len);
super(wid);
- super(len,wid)
使用匹配的参数列表调用超类构造函数。你不能两次调用构造函数。即使在JVM字节码级别,也可以在任何给定对象上最多调用一次方法链。第二个错误准确地说明了这一点:super(parameter list)