小弟刚学java不久,有道简单java继承关系小程序,已编代码,望各位大虾帮忙改错,特别是构造方法调用父类的地方(this & super),以及最后应用程序时实参的调用....有点模糊,望好心人们指点迷津~~~
设计一个抽象类Shape,包括一个抽象方法showShapeName(),设计二个类Circle和Rectangle,分别继承Shape类,其中Circle类包含一个静态变量Pi=3.14,一个属性shape,一个属性radius,一个求面积的方法area();Rectangle类包含三个属性shape、length和width,一个求面积的方法area()。再设计二个类Sphere和Cube,分别继承类Circle和Rectangle,其中Sphere类包含一个求球体积的方法volume(),Cube类包含一个属性hight,一个求立方体体积的方法volume()。要求:求出半径为5的圆的面积和体积;长、宽分别为7、8的长方形的面积,长、宽、高分别为7、8、9的立方体的体积。
abstract class Shape
{
public abstract showShapeName();
}
class Circle extends Shape
{
double radius;
circle (double r)
{
radius=r;
}
double area()
{
return Math.PI*radius*radius;
}
void showShapeName()
{
System.out.println("这是一个圆");
}
}
class Rectangle extends Shape
{
double length,width;
Rectangle ( double l,double w)
{
length=l;
width=w;
}
double area()
{
return length*width;
}
void showShapeName()
{
System.out.println("这是一个长方形");
}
}
class Sphere extends Circle
{
Sphere (double r)
{ super(r);}
double volume() }
{
System.out.println("球体的体积为:");
System.out.println("4/3*super.area()*super.radius");
}
}
class cube extends Rectangle
{
double hight
cube (double l,double w,double h)
{ super(l,w);
hight=h; }
double volume()
{
System.out.println("长方体的体积为:");
System.out.println("super.area()*high");
}
}
class testShape
{
public static void main(String args[])
{
Circle a =new Circle(5);
a.area();
Sphere as=new Sphere()
as.volume();
Rectangle b=new Rectangle(7,8);
b.area();
cube bs=new cube( , ,9);
bs.volume();
}
}
设计一个抽象类Shape,包括一个抽象方法showShapeName(),设计二个类Circle和Rectangle,分别继承Shape类,其中Circle类包含一个静态变量Pi=3.14,一个属性shape,一个属性radius,一个求面积的方法area();Rectangle类包含三个属性shape、length和width,一个求面积的方法area()。再设计二个类Sphere和Cube,分别继承类Circle和Rectangle,其中Sphere类包含一个求球体积的方法volume(),Cube类包含一个属性hight,一个求立方体体积的方法volume()。要求:求出半径为5的圆的面积和体积;长、宽分别为7、8的长方形的面积,长、宽、高分别为7、8、9的立方体的体积。
abstract class Shape
{
public abstract showShapeName();
}
class Circle extends Shape
{
double radius;
circle (double r)
{
radius=r;
}
double area()
{
return Math.PI*radius*radius;
}
void showShapeName()
{
System.out.println("这是一个圆");
}
}
class Rectangle extends Shape
{
double length,width;
Rectangle ( double l,double w)
{
length=l;
width=w;
}
double area()
{
return length*width;
}
void showShapeName()
{
System.out.println("这是一个长方形");
}
}
class Sphere extends Circle
{
Sphere (double r)
{ super(r);}
double volume() }
{
System.out.println("球体的体积为:");
System.out.println("4/3*super.area()*super.radius");
}
}
class cube extends Rectangle
{
double hight
cube (double l,double w,double h)
{ super(l,w);
hight=h; }
double volume()
{
System.out.println("长方体的体积为:");
System.out.println("super.area()*high");
}
}
class testShape
{
public static void main(String args[])
{
Circle a =new Circle(5);
a.area();
Sphere as=new Sphere()
as.volume();
Rectangle b=new Rectangle(7,8);
b.area();
cube bs=new cube( , ,9);
bs.volume();
}
}
缺了返回值类型应该是public abstract String showShapeName();
2,cube bs=new cube( , ,9);无参数
public abstract void showShapeName();
}class Circle extends Shape {
double radius; Circle(double r) {
radius = r;
} double area() {
return Math.PI * radius * radius;
} public void showShapeName() {
System.out.println("这是一个圆");
}
}class Rectangle extends Shape {
double length, width; Rectangle(double l, double w) {
length = l;
width = w;
} double area() {
return length * width;
} public void showShapeName() {
System.out.println("这是一个长方形");
}
}class Sphere extends Circle {
Sphere(double r) {
super(r);
} double volume() {
// System.out.println("球体的体积为:");
return (4.0 / 3) * super.area() * super.radius;
}
}class cube extends Rectangle {
double high; cube(double l, double w, double h) {
super(l, w);
high = h;
} double volume() {
// System.out.println("长方体的体积为:");
return super.area() * high;
}
}public class testShape {
public static void main(String args[]) {
Circle a = new Circle(10);
System.out.println("圆的面积=" + a.area());
Sphere as = new Sphere(10);
System.out.println("体积="+as.volume());
Rectangle b = new Rectangle(2, 3);
System.out.println("长方形面积="+b.area());
cube bs = new cube(2, 3, 4);
System.out.println("体积="+bs.volume());
}
}
public abstract void showShapeName();
}class Circle extends Shape {
double radius; Circle(double r) {
radius = r;
} double area() {
return Math.PI * radius * radius;
} public void showShapeName() {
System.out.println("这是一个圆");
}
}class Rectangle extends Shape {
double length, width; Rectangle(double l, double w) {
length = l;
width = w;
} double area() {
return length * width;
} public void showShapeName() {
System.out.println("这是一个长方形");
}
}class Sphere extends Circle {
Sphere(double r) {
super(r);
} void volume() {
{
System.out.println("球体的体积为:");
System.out.println((double) (4 / 3) * super.area() * super.radius);
}
}}class Cube extends Rectangle {
double hight; Cube(double l, double w, double h) {
super(l, w);
hight = h;
} void volume() {
System.out.println("长方体的体积为:");
System.out.println(super.area() * hight);
}
}class testShape {
public static void main(String args[]) {
Circle a = new Circle(5);
a.area();
Sphere as = new Sphere(2.0);
as.volume();
Rectangle b = new Rectangle(7, 8);
b.area();
Cube bs = new Cube(3, 3, 9);
bs.volume();
}
}
结贴给分吧.. 你的程序太乱了. 改的我头疼..
package xxx;abstract class Shape
{
public abstract void showShapeName();
}
class Circle extends Shape
{
double radius;
public Circle(double r)
{
radius=r;
}
double area()
{
return Math.PI*radius*radius;
}
public void showShapeName()
{
System.out.println("这是一个圆");
}
}
class Rectangle extends Shape
{
double length,width;
Rectangle ( double l,double w)
{
length=l;
width=w;
}
double area()
{
return length*width;
}
public void showShapeName()
{
System.out.println("这是一个长方形");
}
}
class Sphere extends Circle
{
public Sphere (double r)
{
super(r);
}
public double volume()
{
System.out.println("球体的体积为:");
System.out.println("4/3*super.area()*super.radius");
return 4/3*super.area()*super.radius;
}
}
class cube extends Rectangle
{
double hight;
cube (double l,double w,double h)
{ super(l,w);
hight=h; }
public double volume()
{
System.out.println("长方体的体积为:");
System.out.println("super.area()*hight");
return super.area()*hight;
}
}
class Shap
{
public static void main(String args[])
{
Circle a =new Circle(5);
a.area();
Sphere as=new Sphere(3.0);
as.volume();
Rectangle b=new Rectangle(7,8);
b.area();
cube bs=new cube(9.0,3.0,4.0);
bs.volume();
}
} 问题确实很多,看看我该的行不行