父類(lèi)Shape
package?com.imooc;
public?abstract?class?Shape?{
????public?abstract?void?perimeter(int?length);
????public?abstract?void?area(int?length);
}
子類(lèi)
package?com.imooc;
public?class?Rectangle?extends?Shape?{
????
????@Override
????public?void?perimeter(int?length)?{
????????//?TODO?Auto-generated?method?stub
????????System.out.println("矩形周長(zhǎng)為"+length*4);
????}
????@Override
????public?void?area(int?length)?{
????????//?TODO?Auto-generated?method?stub
????????System.out.println("矩形面積為"+length*length);
????}
}
子類(lèi)
package?com.imooc;
public?class?Circle?extends?Shape?{
????
????@Override
????public?void?perimeter(int?length)?{
????????//?TODO?Auto-generated?method?stub
????????System.out.println("圓形周長(zhǎng)為"+2*3.14*length);
????}
????@Override
????public?void?area(int?length)?{
????????//?TODO?Auto-generated?method?stub
????????System.out.println("圓形面積為"+3.14*length*length);
????}
}
package?com.imooc;
public?class?Initial?{
????public?static?void?main(String[]?args)?{
????????Shape?s1?=?new?Rectangle();
????????Shape?s2?=?new?Circle();
????????int?length?=?10;
????????s1.perimeter(length);
????????s1.area(length);
????????s2.perimeter(length);
????????s2.area(length);
????}
}
執(zhí)行結(jié)果:
矩形周長(zhǎng)為40
矩形面積為100
圓形周長(zhǎng)為62.800000000000004
圓形面積為314.0
2017-05-06
那是因?yàn)镴ava輸出的時(shí)候自動(dòng)轉(zhuǎn)換成了double