利用接口技术周长

题目:

1、通过程序设计几何图形(Shape)、矩形(Rectangle)、圆形(Circle)、正方形(Square)几种类型,能够利用接口和多态性计算几何图形的面积和周长并显示。

 
周长面积代码
interface zhouchang{
public void zhouchang();
}
interface mianji{
public void mianji();


}
abstract class zong implements zhouchang,mianji {
int chang,kuan,banjing,zhouchang,mianji;
public zong(int chang,int kuan,int banjing) {
this.chang  = chang;
this.kuan = kuan;
this.banjing = banjing;
}
}
class Rectangle extends zong{
public Rectangle(int chang, int kuan, int banjing) {
super(chang, kuan, banjing);
}
public void zhouchang(){
zhouchang = (chang+kuan)*2;
System.out.println("矩形的周长是:"+zhouchang);
}public void mianji(){
mianji = chang*kuan;
System.out.println("矩形的面积是:"+mianji);
}
}




class Circle extends zong{
public Circle(int chang, int kuan, int banjing) {
super(chang, kuan, banjing);
}
public void zhouchang(){
zhouchang = (int) (2*3.14*banjing);
System.out.println("圆形的周长是:"+zhouchang);
}
public void mianji(){
mianji = (int) (3.14*(banjing*banjing));
System.out.println("圆形的面积是:"+mianji);
}
}




class Square extends zong{
public Square(int chang, int kuan, int banjing) {
super(chang, kuan, banjing);
}
public void zhouchang(){
zhouchang = (chang+kuan)*2;
System.out.println("正方形的周长是:"+zhouchang);
}
public void mianji(){
mianji = chang*kuan;
System.out.println("正方形的面积是:"+mianji);
}
}
public class zcmj {
public static void main(String[] args) {
Rectangle re = new Rectangle(10,12, 0);
re.zhouchang();
re.mianji();
Circle ci = new Circle(0, 0, 10);
ci.zhouchang();
ci.mianji();
Square sq = new Square(12, 12, 0);
sq.zhouchang();
sq.mianji();


}

运行结果
利用接口技术周长_第1张图片

你可能感兴趣的:(利用接口技术周长)