为了账号安全,请及时绑定邮箱和手机立即绑定

通过abstract抽象类来计算周长和面积的源代码 如下 已成功运行 有不同想法可以交流

https://img1.sycdn.imooc.com//5c3df6d600019f1c08310364.jpg

https://img1.sycdn.imooc.com//5c3df6d70001bfc208270491.jpg

https://img1.sycdn.imooc.com//5c3df6d70001d39c07890465.jpg

https://img1.sycdn.imooc.com//5c3df6d70001dfe508280481.jpg 

https://img1.sycdn.imooc.com//5c3df7260001617d08260208.jpg

已成功运行 有不同想法可以交流

正在回答

3 回答

1.

package com.imooc;


public abstract class Shape {

    public abstract void ares();

    public abstract void girth();

}

2.

package com.imooc;

    public class Rec extends Shape {

        private int l;

        private int w;

    public Rec(int l,int w) {

        this.l=l;

        this.w=w;

}


@Override

    public void ares() {

     System.out.println("长方形面积为: "+l*w);

}


@Override

    public void girth() {

    System.out.println("长方形周长为: "+(l+w)*2);

}

}

3.

package com.imooc;


public class Squ extends Shape {

    private int r;

    public Squ() {

}

    public Squ(int r) {

    this.r=r;

}

@Override

    public void ares() {

    System.out.println("圆面积: "+Math.PI*r*r);

}


@Override

public void girth() {

    System.out.println("圆周长: "+Math.PI*2*r);

}

}

4.

package com.imooc;


public class Initial {

public static void main(String[] args) {

    Shape R=new Rec(15,8);

    Shape S=new Squ(7);

    R.ares();

    R.girth();

    S.ares();

    S.girth();

}

}


0 回复 有任何疑惑可以回复我~
1.
package jszcmj;
public abstract class shape {
public double pi=3.14;
public abstract void Reactangle(double a,double b);
public abstract void Circle(double a);
}
2.
package jszcmj;
public class ReacLong extends shape {
@Override
public void Reactangle(double a, double b) {
// TODO Auto-generated method stub
double rt=(a+b)*2;
System.out.println("矩形周长为:"+rt);
}
@Override
public void Circle(double a) {
// TODO Auto-generated method stub
double cc=pi*a*2;
System.out.println("圆形周长为:"+cc);
}
}
3.
package jszcmj;
public class Cirmian extends shape {
@Override
public void Reactangle(double a, double b) {
// TODO Auto-generated method stub
double rtm=a*b;
System.out.println("矩形面积为:"+rtm);
}
@Override
public void Circle(double a) {
// TODO Auto-generated method stub
double ccm=pi*a*a;
System.out.println("圆形面积为;"+ccm);
}
}
4.
package jszcmj;
public class Initail {
public static void main(String[] args) {
// TODO Auto-generated method stub
shape sh1=new ReacLong();
shape sh2=new Cirmian();
sh1.Reactangle(10, 20);
sh2.Reactangle(10, 20);
sh1.Circle(4);
sh2.Circle(4);
}
}


1 回复 有任何疑惑可以回复我~

要用传参方法怎么实现?矩形两个变量,圆一个变量

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

通过abstract抽象类来计算周长和面积的源代码 如下 已成功运行 有不同想法可以交流

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号