日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/19 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA子类是球父类是圆_java的父类和子类,这是矛盾... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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 "+());

}

}

這樣怎么不行?

總結

以上是生活随笔為你收集整理的JAVA子类是球父类是圆_java的父类和子类,这是矛盾...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。