課后練習(xí)
package?com.lee.poly;
/*Shape類*/
public?abstract?class?Shape?{
public?abstract?double?size();
public?abstract?double?boundry();
}Rectangle類:
package?com.lee.poly;
public?class?Rectangle?extends?Shape{
public?double?len;
public?double?hei;
public?Rectangle(double?len,?double?hei)?{
super();
this.len?=?len;
this.hei?=?hei;
}
public?void?setLen(double?len)?{
this.len?=?len;
}
public?void?setHei(double?hei)?{
this.hei?=?hei;
}
@Override
public?double?size()?{
double?size?=?len?*?hei;
return?size;
}
@Override
public?double?boundry()?{
double?bou?=?(len?+?hei?)?*?2;
return?bou;
}
}Circle類:
package?com.lee.poly;
public?class?Circle?extends?Shape{
public?double?r;
public?static?double?pi?=?3.14;
public?Circle(double?r)?{
super();
this.r?=?r;
}
public?void?setR(double?r)?{
this.r?=?r;
}
@Override
public?double?size()?{
double?size?=?pi?*?r?*?r;
return?size;
}
@Override
public?double?boundry()?{
double?bou?=?2?*?pi?*?r;
return?bou;
}
}測(cè)試類:
package?com.lee.poly;
import?java.util.Scanner;
public?class?Test?{
public?static?void?main(String[]?args)?{
Scanner?scan?=?new?Scanner(System.in);
System.out.println("請(qǐng)輸入長(zhǎng)方形的長(zhǎng):");
double?len?=?scan.nextDouble();
System.out.println("請(qǐng)輸入長(zhǎng)方形的寬:");
double?hei?=?scan.nextDouble();
Rectangle?rect?=?new?Rectangle(len,?hei);
System.out.println("長(zhǎng)方形的面積是:"?+?rect.size());
System.out.println("長(zhǎng)方形的周長(zhǎng)是:"?+?rect.boundry());
System.out.println("請(qǐng)輸入圓形的半徑:");
double?r?=?scan.nextDouble();
Circle?cir?=?new?Circle(r);
System.out.println("圓形的面積是:"?+?cir.size());
System.out.println("圓形的周長(zhǎng)是:"?+?cir.boundry());
scan.close();
}
}結(jié)果:
請(qǐng)輸入長(zhǎng)方形的長(zhǎng): 12 請(qǐng)輸入長(zhǎng)方形的寬: 6 長(zhǎng)方形的面積是:72.0 長(zhǎng)方形的周長(zhǎng)是:36.0 請(qǐng)輸入圓形的半徑: 6 圓形的面積是:113.03999999999999 圓形的周長(zhǎng)是:37.68
2015-12-21
成員變量要用private,
2015-12-21
是要寫程序嗎