package?yu.zhang;
public?abstract?class?Shape?{
public?abstract?double?perimeter();
public?abstract?double?area();
}
package?yu.zhang;
public?class?Rectangle?extends?Shape{
public?double?i?=?3;????//定義矩形的長(zhǎng)
public?double?j?=?2; //定義矩形的寬
public?double?perimeter(){
return(i+i+j+j);
}
public?double?area(){
return(i*j);
}
}
package?yu.zhang;
public?class?Circle?extends?Shape{
private?double?r?=?4;???//定義圓的半徑
public?double?perimeter(){
return(3.14*r*2);
}
public?double?area(){
return(3.14*r*r);
}
}
package?yu.zhang;
public?class?main?{
public?static?void?main(String[]?args)?{
//?TODO?Auto-generated?method?stub
Shape?rec?=?new?Rectangle();
Shape?cir?=?new?Circle();
double?recPerimeter?=?rec.perimeter();????//求矩形周長(zhǎng)
double?recArea?=?rec.area();?????//求矩形面積
System.out.println("矩形的周長(zhǎng)是:"?+?recPerimeter);
System.out.println("矩形的面積是:"?+?recArea);
double?cirPerimeter?=?cir.perimeter();????//求圓形的周長(zhǎng)
double?cirArea?=?cir.area();????//求圓形的面積
System.out.println("圓形的周長(zhǎng)是:"?+?cirPerimeter);
System.out.println("圓形的面積是:"?+?cirArea);
}
}
2016-05-07
2016-05-07
package?yu.zhang;
?
public?class?main?{
?
????public?static?void?main(String[]?args)?{
????????//?TODO?Auto-generated?method?stub
????????Shape?rec?=?new?Rectangle();
? ? ? ?
??
????????System.out.println("矩形的周長(zhǎng)是:"?+ rec.perimeter());
????????System.out.println("矩形的面積是:"?+?rec.area());
?????????
? ? ? ??Shape?cir?=?new?Circle();
????????System.out.println("圓形的周長(zhǎng)是:"?+?cir.Perimeter());
????????System.out.println("圓形的面積是:"?+?cir.area());
????}
?
}
2016-05-03
抽象類(lèi)和接口的方法應(yīng)該是public abstract void而不是public abstract double
橙色部分為Java關(guān)鍵字,藍(lán)色應(yīng)該為常量