码迷,mamicode.com
首页 > 其他好文 > 详细

作业-继承8

时间:2016-09-23 23:03:30      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

编写一个Shape类,具有属性:周长和面积;

定义其子类三角形和矩形,分别具有求周长的方法。

定义主类E,在其main方法中创建三角形和矩形类的对象,

并赋给Shape类的对象a、b,使用对象a、b来测试其特性。

package T092208;

public class Shape {
    private int bian;
    private double zhouchang;
    private double mianji;
    public int getBian() {
        return bian;
    }
    public void setBian(int bian) {
        this.bian = bian;
    }
    public double getZhouchang() {
        return zhouchang;
    }
    public void setZhouchang(double zhouchang) {
        this.zhouchang = zhouchang;
    }
    public double getMianji() {
        return mianji;
    }
    public void setMianji(double mianji) {
        this.mianji = mianji;
    }
    public void bian(){
        System.out.println("您创建了一个"+bian+"边形!");
    }
}



package T092208;

public class San extends Shape {
    double[] bianchang=new double[3];
    San(){
        
    }
    San(double a,double b, double c){
        bianchang[0]=a;
        bianchang[1]=b;
        bianchang[2]=c;
    }
    public void zhouchang(){
        if(getBian()==3&&bianchang[0]+bianchang[1]>bianchang[2]&&bianchang[0]-bianchang[1]<bianchang[2]){
            double sum=0;
            for(int i=0;i<bianchang.length;i++){
                sum+=bianchang[i];
                System.out.print(" 边长"+(i+1)+"是"+ bianchang[i]);
            }
            System.out.println(" 三角形的周长是:"+sum);
        }
        else{
            System.out.println("这不是一个三角形");
        }
    }
}



package T092208;

public class juxing extends Shape {
    double[] bianchang=new double[4];
    juxing(){}
    juxing(double a,double b,double c,double d){
        bianchang[0]=a;
        bianchang[1]=b;
        bianchang[2]=c;
        bianchang[3]=d;
    }
    public void zhouchang(){
        if(getBian()==4 && bianchang[0]==bianchang[2]&&bianchang[1]==bianchang[3]){
            double sum=0;
            for(int i=0;i<bianchang.length;i++){
                sum+=bianchang[i];
                System.out.print(" 边长"+(i+1)+"是"+bianchang[i]);
            }
            System.out.println(" 矩形的周长是:"+sum);
        }
        else{
            System.out.println("这不是一个矩形");
        }
    }
}



package T092208;

public class Test {
    public static void main(String[] args){
        Shape s=new Shape();
        s.setBian(5);
        s.bian();
        San sa=new San(15,14,12);
        sa.setBian(3);
        sa.zhouchang();
        juxing ju=new juxing(5,10,5,10);
        ju.setBian(4);
        ju.zhouchang();
    }
}

技术分享

作业-继承8

标签:

原文地址:http://www.cnblogs.com/wangguoning/p/5901684.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!