JAVA子类是球父类是圆_java的父类和子类,这是矛盾...

ca56232b3bbedf9a539d07f37fffb99a.gif

3144d8b7615c79d9f638db40d5689d26.gif

a218af6549b45ee526caf607ebff1358.gif

0f8df0e29816ae721419de940fb833d1.gif

java的父类和子类,这是矛盾...

public class P134 {//父类

/*圆的半径*/

private double radius;

/**

* 第一个构造方法

* 构造一个单位圆

*/

public P134(){

this(1.0);

}

/**

* 第二个构造方法

* 圆的半径

*/

public P134(double radius){

setRadius(radius);

}

/**

* 半径更改器

* 圆的半径

*/

public void setRadius(double radius){

/*在方法中加入参数检查,确保不会传入负值*/

if(radius<0){

System.out.println("圆的半径不能为负值");

this.radius=1.0;

}else{

this.radius=radius;

}

}

/**

* 半径获取器

* 返回值的半径

*/

public double getRadius(){

return radius;

}

/**

* 计算圆的面积

* 返回圆的面积

*/

public double computerArea(){

return Math.PI*radius*radius;

}

public static void main(String[] args) {

// TODO Auto-generated method stub

//创建一个Cylinder对象,显示它的属性

Cylinder myCylinder=new Cylinder(10.0, 5.0);

System.out.println("The height is "+myCylinder.getHeight());

System.out.println("The radius is "+myCylinder.getRadius());

System.out.println("The volume of the cylinder is "

+());

System.out.println("The area of the circle is "+());

}

}

public class Cylinder extends P134{//子类

private double height;

/**

* 默认构造方法

* 构造一个底面圆为单位圆,高度等于1.0圆柱体

*/

public Cylinder(){

super();

height=1.0;

}

/**

* 第二个构造方法

* 底面圆的半径,圆柱体的高度

*/

public Cylinder(double radius,double height){

super(radius);

setHeight(height);

}

/**

* 高度更改器

* 圆柱体的高度

*/

public void setHeight(double height){

/*方法加入了参数检查,确保不会传入负值*/

if(height<0){

System.out.println("圆柱体的高度不能为负值.");

this.height=1.0;

}else{

this.height=height;

}

}

/**

* 高度获取器

* 圆柱体的高度

*/

public double getHeight(){

return height;

}

/**

* 计算圆柱体体积

*/

public double computerVolume(){

return computerArea()*height;

}

}

怎么上面子类和父类是在同个java文档却用两个class隔开

public class P134 {

/*圆的半径*/

private double radius;

/**

* 第一个构造方法

* 构造一个单位圆

*/

public P134(){

this(1.0);

}

/**

* 第二个构造方法

* 圆的半径

*/

public P134(double radius){

setRadius(radius);

}

/**

* 半径更改器

* 圆的半径

*/

public void setRadius(double radius){

/*在方法中加入参数检查,确保不会传入负值*/

if(radius<0){

System.out.println("圆的半径不能为负值");

this.radius=1.0;

}else{

this.radius=radius;

}

}

/**

* 半径获取器

* 返回值的半径

*/

public double getRadius(){

return radius;

}

/**

* 计算圆的面积

* 返回圆的面积

*/public class Cylinder extends P134{

private double height;

/**

* 默认构造方法

* 构造一个底面圆为单位圆,高度等于1.0圆柱体

*/

public Cylinder(){

super();

height=1.0;

}

/**

* 第二个构造方法

* 底面圆的半径,圆柱体的高度

*/

public Cylinder(double radius,double height){

super(radius);

setHeight(height);

}

/**

* 高度更改器

* 圆柱体的高度

*/

public void setHeight(double height){

/*方法加入了参数检查,确保不会传入负值*/

if(height<0){

System.out.println("圆柱体的高度不能为负值.");

this.height=1.0;

}else{

this.height=height;

}

}

/**

* 高度获取器

* 圆柱体的高度

*/

public double getHeight(){

return height;

}

/**

* 计算圆柱体体积

*/

public double computerVolume(){

return computerArea()*height;

}

}

public double computerArea(){

return Math.PI*radius*radius;

}

public static void main(String[] args) {

// TODO Auto-generated method stub

//创建一个Cylinder对象,显示它的属性

Cylinder myCylinder=new Cylinder(10.0, 5.0);

System.out.println("The height is "+myCylinder.getHeight());

System.out.println("The radius is "+myCylinder.getRadius());

System.out.println("The volume of the cylinder is "

+());

System.out.println("The area of the circle is "+());

}

}

这样怎么不行?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值