point类、circle类和cycliner类之间的继承问题

hkfxp 2008-04-23 04:57:44
以下编写的程序编译时出现错误找不到符号new,getX()和getY()方法不能在cycliner类中调用。不知道如何改错,请前辈们指点!
(1)设计一个表示二维平面上点的类Point,包含有表示坐标位置的成员变量x和y,获取和设置x和y值的方法。
(2)设计一个表示二维平面上圆的类Circle,它继承自类Point,还包含有表示圆半径的成员变量r、获取和设置r值的方法、计算圆面积的方法。
(3)设计一个表示圆柱体的类Cylinder,它继承自类Circle,还包含有表示圆柱体高的成员变量h、获取和设置h值的方法、计算圆柱体体积的方法。
(4)建立若干个Cylinder对象,输出其轴心位置坐标、半径、高及其体积的值

point类
public class point{
float x,y;
public point(float a,float b){
x=a;
y=b;
}
public void setPoint(float x,float y){
this.x=x;
this.y=y;
}
public float getX(float x){
return x;
}
public float getY(float y){
return y;
}
}

circle类
public class circle extends point{
protected float radius;
// final float PI=3.14;
public circle(float a,float b,float r){
super(a,b);
radius=r;
}
public void setCircle(float r){
radius=r;
}
float getRadius(){
return radius;
}
double area(){
return 3.14*radius*radius;
}
}

cylinder类
public class cylinder extends circle{
float heigh;
public cylinder(float a,float b,float r,float h){
super(a,b,r);
heigh=h;
}
void setHeigh(float h){
heigh=h;
}
float getHeigh(){
return heigh;
}
double volume(){
return super.area()*heigh;
}
public static void main(String[] args){
cylinder cylinder1=new cylinder(3.5,6.4,5.2,10);
cylinder cylinder2=new cylinder(3.7,6.5,5.3,11);
cylinder cylinder3=new cylinder(3.8,6.6,5.4,12);
System.out.println(cylinder1.getX()+" "+cylinder1.getY()+" "+cylinder1.radius+" "+cylinder1.heigh+" "+cylinder1.volume());
System.out.println(cylinder2.getX()+" "+cylinder2.getY()+" "+cylinder2.radius+" "+cylinder2.heigh+" "+cylinder2.volume());
System.out.println( cylinder3.getX()+" "+cylinder3.getY()+" "+cylinder3.radius+" "+cylinder3.heigh+" "+cylinder3.volume());
}
}
...全文
1577 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
胡矣 2008-04-23
  • 打赏
  • 举报
回复
按照编译的错误其实就能改过来的 。
耐心+细心。
胡矣 2008-04-23
  • 打赏
  • 举报
回复

class point {
float x, y;

public point(float a, float b) {
x = a;
y = b;
}

public void setPoint(float x, float y) {
this.x = x;
this.y = y;
}

public float getX() {
return x;
}

public float getY() {
return y;
}
}

class circle extends point {
protected float radius;

// final float PI=3.14;
public circle(float a, float b, float r) {
super(a, b);
radius = r;
}

public void setCircle(float r) {
radius = r;
}

float getRadius() {
return radius;
}

double area() {
return 3.14 * radius * radius;
}
}

class cylinder extends circle {
float heigh;

public cylinder(float a, float b, float r, float h) {
super(a, b, r);
heigh = h;
}

void setHeigh(float h) {
heigh = h;
}

float getHeigh() {
return heigh;
}

double volume() {
return super.area() * heigh;
}

public static void main(String[] args) {
cylinder cylinder1 = new cylinder(3.5F, 6.4F, 5.2F, 10F);
cylinder cylinder2 = new cylinder(3.7F, 6.5F, 5.3F, 11F);
cylinder cylinder3 = new cylinder(3.8F, 6.6F, 5.4F, 12F);
System.out.println(cylinder1.getX() + " " + cylinder1.getY() + " "
+ cylinder1.radius + " " + cylinder1.heigh + " "
+ cylinder1.volume());
System.out.println(cylinder2.getX() + " " + cylinder2.getY() + " "
+ cylinder2.radius + " " + cylinder2.heigh + " "
+ cylinder2.volume());
System.out.println(cylinder3.getX() + " " + cylinder3.getY() + " "
+ cylinder3.radius + " " + cylinder3.heigh + " "
+ cylinder3.volume());
}
}
hkfxp 2008-04-23
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 anqini 的回复:]
还有方法getX(),你写的是带参数的,你调用的时候不带参数的!应该改为不待参数!
[/Quote]
真是一针见血,我程序看了好几遍我怎么就没看出来呢 真的很佩服你的细心和java功底
goodmrning 2008-04-23
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 caoyinghui1986 的回复:]
lz要坚持啊.
继续加油学习............
[/Quote]
hmsuccess 2008-04-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 healer_kx 的回复:]
面向对象关系没有对错之分,只有好坏之别。

但是你这个继承关系,没有一个是“对”的。
[/Quote]

Shine_Panda 2008-04-23
  • 打赏
  • 举报
回复
lz要坚持啊.
继续加油学习............
healer_kx 2008-04-23
  • 打赏
  • 举报
回复
通读
http://blog.csdn.net/Slugfest/category/359453.aspx
anqini 2008-04-23
  • 打赏
  • 举报
回复
还有方法getX(),你写的是带参数的,你调用的时候不带参数的!应该改为不待参数!
hkfxp 2008-04-23
  • 打赏
  • 举报
回复
惭愧 学习java已经两个月了,还是没有入门,实在是无奈 也许不适合学编程 可是我喜欢自己琢磨 不懂我就会来这问了 不论怎样 这没课还得继续学下去 错就错吧
anqini 2008-04-23
  • 打赏
  • 举报
回复
cylinder的构造方法改为
public cylinder(double a,double b,double r,int h)
就可以了

因为你new的时候new cylinder(3.5,6.4,5.2,10);
3.5认为是double类型,要么你new的时候这样
new cylinder(3.5f, 6.4f, 5.2f, 10);
就ok了
healer_kx 2008-04-23
  • 打赏
  • 举报
回复
面向对象关系没有对错之分,只有好坏之别。

但是你这个继承关系,没有一个是“对”的。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧