继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

java入门第二季 抽象类测试

geekcer
关注TA
已关注
手记 2
粉丝 2
获赞 6
package com.imooc.abstractTest;

public abstract class Shape {
    public abstract void girth();
    public  abstract void area();
}
package com.imooc.abstractTest;
import java.util.Scanner;
public class Rectangle extends Shape {
    Scanner input =new Scanner(System.in);
    int width =input.nextInt();
    int height= input.nextInt();
    @Override
    public void girth() {
        int girth = (width+height)*2;
        System.out.println("矩形周长为:"+girth);
    }
    @Override
    public void area() {
        double area= width*height;
        System.out.println("矩形面积为:" +area);
    }
}
package com.imooc.abstractTest;
import java.util.Scanner;
public class Circle extends Shape {
    Scanner input =new Scanner(System.in);
    int r =input.nextInt();
    @Override
    public void girth() {
        double girth = 2*Math.PI*r;
        System.out.println("圆形周长为:"+girth);
    }
    @Override
    public void area() {
        double area= Math.PI*r*r;
        System.out.println("圆形面积为:" +area);
    }
}
package com.imooc.abstractTest;
public class Test {
    public static void main(String[] args) {
        System.out.println("请输入长和宽");
        Shape rec=new Rectangle();
        rec.girth();
        rec.area();
        System.out.println();
        System.out.println("请输入半径");
        Shape cir =new Circle();
        cir.girth();
        cir.area();
    }
}
打开App,阅读手记
3人推荐
发表评论
随时随地看视频慕课网APP