手记

java入门第二季 抽象类测试

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();
    }
}
3人推荐
随时随地看视频
慕课网APP