package?Chapter5;
import?java.util.Scanner;
abstract?class?Shape1{
public?abstract?void?Sum();
public?abstract?void?Area();
}?
class?Rectang?extends?Shape1{ ?
?????double?width; ??
?????double?length; ??
?public?Rectang(){ ?
?? ??}
?public?Rectang(double?width,double?length){
? ??this.width=width;
? ??this.length=length; ?
? ???} ???
?public??void?Sum(){ ????
???????double?sum=2*(width+length); ????
???????System.out.println("長方形周長為:"+sum);
???????????}
?public??void?Area(){
???????double?sum=width*length;
???????System.out.println("長方形面積為:"+sum);
???????}?
???}?
class?Circle?extends?Shape1{ ?
??????double?radius; ?
??????public?Circle(){ ??
?????? ??} ?
??????public?Circle(double?radius){
?????? ??this.radius=radius; ??
?????? ??} ???
??????public??void?Sum(){ ???
??????? double?sum=Math.PI*2*radius; ????
????????System.out.println("圓的周長為:"+sum); ??
??????????}
??????public??void?Area(){
????? double?sum=Math.PI*radius*radius;
????????System.out.println("圓的面積為:"+sum);
????????}
????????}
????public?class?Shape_1?{
?????????public?static?void?main(String[]?args)?{
?????????//?TODO?Auto-generated?method?stub
?????????boolean?dd=true;???????
?????????Scanner?in=new?Scanner(System.in);??????
??????????while(dd){???????
???????????System.out.println("請選擇你要計算的圖形:1--長方形,2--圓:");????
???????????int?s=in.nextInt();??????
???????????if(s==1){???? ???
???????????System.out.println("請輸入長方形的寬:");????
???????????int?aa=in.nextInt();???? ???
???????????System.out.println("請輸入長方形的長:");???? ??
???????????int?ss=in.nextInt();???? ??
???????????Shape1?sh;???? ??
???????????sh=new?Rectang(aa,ss);???? ?
?????????????sh.Sum();???? ???
?????????????sh.Area();???? ???
?????????????dd=false;???????
?????????????}
???????????else?if(s==2){???? ??
???????????System.out.println("請輸入圓的半徑:");????
???????????int?aa=in.nextInt();???? ???
???????????Shape1?sh;???? ???
???????????sh=new?Circle(aa);???? ???
???????????sh.Sum();???? ???
???????????sh.Area();???? ???
???????????dd=false;???????
???????????}else{???? ??
???????????System.out.println("你輸入的有誤請重新輸入");????? ??
???????????dd=true;???????
???????????}???????
??????????}
?????????}
?????}
2019-07-18
差不多很簡潔了
2019-07-29
public?class?Circle?extends?Shape?{ ?private?double?radius; ?public?Circle(double?r){ ??this.radius?=?r; ?}? ? ?public?double?Area()?{ ??//?TODO?Auto-generated?method?stub ??return?Math.PI*radius*radius; ?}?public?double?Perimeter()?{??//?TODO?Auto-generated?method?stub??return?Math.PI*2*radius;?} ?} public?class?Test?{ ?public?static?void?main(String[]?args)?{ ??Scanner?input?=?new?Scanner(System.in); ??int?a?=?input.nextInt(); ?? ??if(a==1||a==2){ ???System.out.println("1***長方形;2***圓形"); ???if(a==1){ ????System.out.println("請輸入長方形的長:"); ????int?c?=?input.nextInt(); ????System.out.println("請輸入長方形的寬:"); ????int?w?=?input.nextInt(); ????Rectangle?rectangle?=?new?Rectangle(c,w); ????System.out.println("長方形1形面積:"+rectangle.Area()+"矩形周長:"+rectangle.Perimeter()); ???}else?if(a==2){ ????System.out.println("請輸入圓形的半徑:"); ????int??r?=?input.nextInt(); ????Shape?circle?=?new?Circle(r); ????System.out.println("圓形面積:"+circle.Area()+"圓形周長:"+circle.Perimeter()); ???} ??? ??}else{ ???System.out.println("您的輸入有誤"); ??? ??} ?} }2019-07-21
可以直接用return來寫 ,比如return(pi*r*r);pi可以在抽象類中定義成final,之后就不用了多次寫了,測試中直接創建子類對象調用方法或者創建父類變量調用,調用可以寫在輸出語句中比如syso{ "? "+變量名.方法名/對象.方法}