问答详情
源自:10-3 Java 中的抽象类

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

https://img4.mukewang.com/5c3df6d600019f1c08310364.jpg

https://img1.mukewang.com/5c3df6d70001bfc208270491.jpg

https://img2.mukewang.com/5c3df6d70001d39c07890465.jpg

https://img1.mukewang.com/5c3df6d70001dfe508280481.jpg 

https://img2.mukewang.com/5c3df7260001617d08260208.jpg

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

提问者:毒打妻儿田教授 2019-01-15 23:07

个回答

  • 慕少6192750
    2019-05-30 19:56:58

    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();

    }

    }


  • 抽象驴
    2019-01-19 17:29:43

    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);
    }
    }


  • 抽象驴
    2019-01-19 16:53:22

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