2013-10-24 31 views
1

EDIT2如何使用circle.java来计算圆柱的getVolume。 [2班]

对不起没关系我只是说

public double cylinderSurfaceArea() { 
     return 2 * base.circleArea() + base.circleCirumference() * 2 * height; 
    } 
} 

由于没有错误代码。这将是正确的?

编辑:


感谢所有那些谁回答。自那之后,我改变了我之前的Cylinder类阅读。现在,我希望把它更进了一步,并添加

public double cylinderSurfaceArea() { 
     return 2 * Math.PI * radius * radius + 2 * Math.PI * radius * h; 
    } 

但是现在说半径(甚至r)返回一个错误“无法找到符号 - 可变半径)不应该的符号被发现/宣告从Circle类?


我所试图做的是使用一个单独的Circle.java类计算圆柱体的体积。

因此,举例来说,我至今对我circle.java以下

public class Circle { 
    public double radius; 

    public Circle(double r) { 
    radius = r; 
    } 

    public double circleArea() { 
     return Math.PI * radius * radius; 
    } 

    public double circleCirumference() { 
     return Math.PI * 2 * radius; 
    } 
} 

现在,这里是问题的开始。当使Cylinder类我开始:

public class Cylinder extends Circle { 

如果是这样,总体来说,我有:

public class Cylinder extends Circle { 
    public Circle base; 
    public double height; 

    public Cylinder(double r, double h) { 
    height = h; 
    base = new Circle(r); 
     } 

    public double getVolume() { 
    return base.circleArea * height; 
    } 
} 

不过,我把后得到一个错误:

public Cylinder(double r, double h) { 

指出:

构造函数类Circle中的不能应用于给定的类型; required:double;发现:noarguments;原因:实际的和正式的参数列表的长度不同”

有人可以把我在正确的方向我在做什么错

+0

为什么你们都使用合成与继承?这里就够了(如果有疑问,请选择合成)。 –

+0

@dystroy“我不知道如何初始化父类作品”是我的猜测。 – millimoose

回答

0

问题1:??在

问题您程序没有默认的构造函数存在于你的Circle中当创建Cylinder对象时,它寻找Circle中的默认构造函数

如果你修改你的Circle如下所示它将工作

class Circle {  
     public Circle(){ 

     } 
} 

问题2

有“碱。circleArea”方法只存在于圈,你又忘了 “()”

base.circleArea需要改变,以base.circleArea().

public class Cylinder extends Circle { 

    public double getVolume() { 
    return base.circleArea() * height; 
    } 
} 

问题3

你的汽缸应该像下面。你已经因此不需要在气缸内创建可变的圆形基座。

class Cylinder extends Circle { 

     public double height; 

     public Cylinder(double r, double h) { 
      super(r); 
     height = h; 
     } 

     public double getVolume() { 
     return circleArea * height; 
     } 
} 
1

Y你无意中调用超级构造函数,但没有这样的构造函数。

但是你有一个设计问题:你试图同时使用组合和继承。一个就足够了。

使用继承:

public class Cylinder extends Circle { 
    public double height; 

    public Cylinder(double r, double h) { 
    super(r); 
    height = h; 
    } 

    public double getVolume() { 
    return circleArea() * height; 
    } 
} 

使用成分(almost always better):

public class Cylinder { 
    public Circle base; 
    public double height; 

    public Cylinder(double r, double h) { 
    height = h; 
    base = new Circle(r); 
    } 

    public double getVolume() { 
    return base.circleArea * height; 
    } 
} 
2

那是因为你的构造函数的第一个电话是隐含super()

报价从Java Tutorials

如果构造函数没有显式调用超类构造函数,那么Java编译器会自动将调用插入到超类的无参构造函数中。如果超类没有没有参数的构造函数,那么你会得到一个编译时错误。

你需要做一个参数的构造函数在Circle类或更改Cylinder构造是这样的:

public Cylinder(double r, double h) { 
    super(r); 
    height = h; 
} 
1

使用继承时,你不需要在Java中明确base场。初始化基类(或“超”),您需要使用super语句在子类的构造函数:

class Circle { 
    public Circle(double radius) { 
     // … 
    } 
} 

class Cylinder extends Circle { 
    public Cylinder(double radius, double height) { 
     super(radius); // calls the parent class constructor 
     // … 
    } 
} 

或者,你可以使用组成代替继承 - 可能是在这一个更好的设计案例:

class Circle { 
    public Circle(double radius) { /* … */ } 
} 

class Cylinder { // no `extends` here 
    public Cylinder(Circle base, double height) { 
     // … 
    } 

    public Cylinder(double radius, double height) { 
     this(new Circle(radius)); // calls the above constructor 
     // … 
    } 
} 

(我省略琐碎的任务和领域为简洁上面的代码示例中)