Java程序(类的抽象与封装)
package clearlove;
public class yuan {//定義構造函數
?
?? private double Radius;//成員變量
?? public double circle() {
??? return Radius;
?? }
?? public void circle(double r) {//創建時將半徑初始化為r
??? Radius = r;
?? }
?? public yuan( ){
??? System.out.println("圓!");
?? }
?? public yuan(double? r){
??? this.Radius = r;
?? }
?? public double getArea(double r){//成員方法
??? return Math.PI * r * r;
?? }
?? public double getPerimeter(double r){//成員方法
??? return 2 * Math.PI * r;
?? }
?? public void? show( ){//成員方法
??? System.out.println("圓的面積: " + this.getArea(Radius));
??? System.out.println("圓的周長: " + this.getPerimeter(Radius));
?? }
?}
??
package clearlove;
public class yuanzhu extends yuan {
?
???
?private double hight;
????
????? public yuanzhu (double r, double? h )
????? {
???
?????? super(r);//成員變量
?????? this.hight = h;
????? }
????? public double getVolume(){//get方法存取對象 跟set相比 它不能重載 方法名必須不相同
?????? return Math.PI * this.circle() * this.circle() * hight;
??????
????? }?
????? public void showVolume( ){
?????? System.out.println("圓柱體的體積:" + this.getVolume());
????? }
????}
package clearlove;
import java.util.Scanner;//輸入函數的要定義的
public class 主函數 {
?
?public static void main(String[] args){
??double r,h;
??Scanner in=new Scanner(System.in);
??System.out.print("輸入圓的半徑");
??r=in.nextInt();
??yuan c1=new yuan(r);
??c1.show( );
??Scanner i=new Scanner(System.in);
??System.out.print("輸入圓柱的高");
??h=i.nextInt();
??yuanzhu c2=new yuanzhu(r,h);
??c2.showVolume( );
?}
}
??
?
轉載于:https://www.cnblogs.com/hjza/p/8933552.html
總結
以上是生活随笔為你收集整理的Java程序(类的抽象与封装)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: shell特殊符号cut命令 sort_
- 下一篇: JavaWeb学习之Spring框架(一