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
2019-07-21
可以直接用return來寫 ,比如return(pi*r*r);pi可以在抽象類中定義成final,之后就不用了多次寫了,測試中直接創(chuàng)建子類對象調(diào)用方法或者創(chuàng)建父類變量調(diào)用,調(diào)用可以寫在輸出語句中比如syso{ "? "+變量名.方法名/對象.方法}