1.
package hhhhkj; public class Shape { double area; double per; String coulor; public Shape(int area, int per, String coulor) { super(); this.area = area; this.per = per; this.coulor = coulor; } public String getCoulor() { return coulor; } public void setCoulor(String coulor) { this.coulor = coulor; } public Shape(){ super(); } public double getArea() { return area; } public void setArea(int area) { this.area = area; } public double getPer() { return per; } public void setPer(int per) { this.per = per; } public void showall(){ Shape r=new Shape(); r.getArea(); r.getCoulor(); r.getPer(); System.out.println("颜色为"+coulor); System.out.println("周长为"+per); System.out.println("面积为"+area); } }
package hhhhkj; public class Rectangle extends Shape{ int width; double heigth; public void getarea(){ area=width*heigth; System.out.println("面积为"+area); } public void getpers(){ per=2*(width+heigth); System.out.println("周长为"+per); } public void showall1(){ System.out.println("颜色为"+coulor); System.out.println("周长为"+per); System.out.println("面积为"+area); } }
package hhhhkj; public class Circle extends Shape{ int ridus=3; double pero; double areao; public void getper() { pero=2*3.14*ridus; System.out.println("周长为"+pero); } public void getarea(){ areao=3.14*ridus*ridus; System.out.println("面积为"+areao); } public void showall(){ System.out.println("周长为"+pero); System.out.println("面积为"+areao); } }
测试类
package hhhhkj; public class main { public static void main(String[] args) { // TODO Auto-generated method stub Shape s=new Shape(); s.per=3.4; s.area=4.5; s.coulor="红色"; s.showall(); Rectangle t=new Rectangle(); t.width=2; t.heigth=3.4; t.getarea(); t.getpers(); t.showall1(); Circle o=new Circle(); o.getarea(); o.ridus=2; o.showall(); } }
2.