Re: What is the problem of the following code?
SamuelXiao wrote:
[another top posted post]
First off, please stop top-posting. The convention on most newsgroups is
to "post inline, and trim excess". This means, delete the parts of the
quote that aren't relevant to your reply, and add your reply to just
below the part of the quote that it is relevant to.
I still don't understand why:
B(double radius, double length){
circle(radius);
this.length = length;
}
here, cricle(radius) is wrong because the compiler said it cannot find
any symbol(NetBean), when i change the circle(radius) into
super(radius), it works, what is the concept behind?
it should be:
B(double radius, double length) {
super(radius); // Always the word "super", nothing else.
this.length = length;
}
The other problem with your code:
double getArea() {
// Don't forget to use "super.getArea()" here, or you'll end up
// with infinit recursion. Oops!
return super.getArea() * length;
}
Although B.getArea() will actually return the volume, not the area, of a
cylinder. The formula for the surface area of a closed right circular
cylinder is radius*radius*Math.PI * 2 + radius * Math.PI * 2 * length).
This can be factored into (radius + length) * (radius * Math.PI * 2)
Now, all this will make your program "Do what you expect", but that
doesn't mean that it is "correct". It seems like this is a place where
you should use "Composition instead of Inheritance".
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>