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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

java 圆 继承_java 类的继承(转)

發布時間:2024/9/27 编程问答 77 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 圆 继承_java 类的继承(转) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這個星期主要是學習了Java中類的繼承問題.繼承就是由已有的類創建新類,通過子類繼承父類的方法,實現一些功能.

下面就是老師布置作業的其中一個:

定義父類圓,通過繼承,獲得子類球、圓錐、圓柱,子類具有計算體積的功能。

1)新建文件夾Test1;

2)定義父類Circle,Circle具有保護成員變量半徑r,并提供成員方法setR、getArea和帶參數的構造函數;

3)子類球Ball具有獲得體積的成員方法getValume;

4)子類圓錐Cone、圓柱Cylinder具有私有的成員變量高high、設置高的成員方法setHigh()和獲得體積的getValume;

5)3個子類都提供帶參數的構造函數;

6)3個子類在重設半徑后,應獲得正確的體積;

7)編寫測試類進行測試

代碼:

Circle.java

package test3;

public class Circle {

public static void main(String[] args) {

// TODO Auto-generated method stub

}

protected double r;

public void setR(double r) {

this.r = r;

}

public double getArea() {

return 3.14*r*r;

}

//public Circle(double r) {

//super();

//this.r = r;

//}

public Circle() {

super();

// TODO Auto-generated constructor stub

}

}

Ball.java

package test3;

import java.util.Scanner;

public class Ball extends Circle{

public static void main(String[] args) {

// TODO Auto-generated method stub

reader = new Scanner(System.in);

Ball ball =new Ball();

double r;

System.out.println("請輸入圓的半徑:");

r = reader.nextDouble();

ball.setR(r);

System.out.println("體積:" + ball.getValumn());

}

private static Scanner reader;

public double getValumn() {

return getArea()*r*4/3;

}

public Ball() {

super();

// TODO Auto-generated constructor stub

}

}

Cone.java

package test3;

import java.util.Scanner;

public class Cone extends Circle{

//public Cone(double r) {

//super(r);

TODO Auto-generated constructor stub

//}

//private double valumn;

private double high;

private static Scanner reader;

public void setHigh(double high) {

this.high = high;

}

public double getValumn() {

return getArea() *high/3;

}

public Cone() {

super();

// TODO Auto-generated constructor stub

}

public static void main(String[] args) {

// TODO Auto-generated method stub

reader = new Scanner(System.in);

Cone cone = new Cone();

double high,r;

System.out.println("請輸入圓錐的高:");

high = reader.nextDouble();

System.out.println("請輸入圓錐的半徑:");

r = reader.nextDouble();

cone.setHigh(high);

cone.setR(r);

System.out.println("體積:" + cone.getValumn());

}

}

Cylinder.java

package test3;

import java.util.Scanner;

public class Cylinder extends Circle{

public static void main(String[] args) {

// TODO Auto-generated method stub

reader = new Scanner(System.in);

Cylinder cy = new Cylinder();

double r,high;

System.out.println("請輸入的高:");

high = reader.nextDouble();

System.out.println("請輸入半徑:");

r = reader.nextDouble();

cy.setHigh(high);

cy.setR(r);

System.out.println("體積:" + cy.getValumn());

}

private double high;

private static Scanner reader;

public void setHigh(double high) {

this.high = high;

}

public double getValumn() {

return getArea() *high;

}

public Cylinder() {

super();

// TODO Auto-generated constructor stub

}

}

Java中類的繼承,更多的可以查看http://www.cnblogs.com/dolphin0520/p/3803432.html,這里有詳細的介紹,我大致就是看這個學習的.

---------------------

作者:yang_tang

來源:CSDN

原文:https://blog.csdn.net/yang_tang/article/details/78070791?utm_source=copy

版權聲明:本文為博主原創文章,轉載請附上博文鏈接!

總結

以上是生活随笔為你收集整理的java 圆 继承_java 类的继承(转)的全部內容,希望文章能夠幫你解決所遇到的問題。

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