Java Shape类

该代码示例展示了如何在Java中定义接口`Shape`,并创建`Circle`和`Rectangle`两个实现该接口的类。程序通过多态调用显示不同形状的面积和周长,提供了一个简单的命令行输入来选择不同的操作。这体现了面向对象编程中的接口使用和多态特性。
摘要由CSDN通过智能技术生成

在这里插入图片描述

import  java.util.Scanner;
//定义接口Shape
interface Shape{
	double area();
	double perimeter();
}
//定义Circle类
class Circle implements Shape{
	int radius;
	public Circle(){
		this.radius=1;
	}
    public Circle(int r){
		this.radius=r;
	}
    public double area(){
    	return radius*radius*Math.PI;
    }
    public double perimeter(){
    	return 2*Math.PI*radius;
    }
}
//定义Rectangle类
class Rectangle implements Shape{
	int width;
	int length;
	public Rectangle(){
		this.length=1;
		this.width=1;
	}
	public Rectangle(int w,int l){
		this.length=l;
		this.width=w;
	}
	 public double area(){
	    	return width*length;
	    }
	    public double perimeter(){
	    	return 2*(width+length);
	    }
}
class Show{
	public static void showArea(Shape s){
		System.out.println(s.area());
	}
    public static void showPerimeter(Shape s){
    	System.out.println(s.perimeter());
	}
}
public class APP {
   public  static  void  main(String[]  args)  {
                int  n;
                Scanner  scan=new  Scanner(System.in);
                n=scan.nextInt();
                Shape  s;
                switch(n){
                case  1:
                        System.out.println(new  Circle().area());
                        break;
                case  2:
                        System.out.println(new  Circle(2).perimeter());
                        break;
                case  3:
                        System.out.println(new  Rectangle().area());
                        break;
                case  4:
                        System.out.println(new  Rectangle(3,4).perimeter());
                        break;
                case  5:
                        s=new  Circle();
                        System.out.println(s.area());
                        break;
                case  6:
                        s=new  Circle(2);
                        Show.showArea(s);
                        Show.showPerimeter(s);
                        break;
                case  7:
                        s=new  Rectangle();
                        System.out.println(s.area());
                case  8:
                        s=new  Rectangle(2,  6);
                        Show.showArea(s);
                        Show.showPerimeter(s);
                        break;
                case  9:
                        s=new  Rectangle();
                        Show.showArea(s);
                        Show.showPerimeter(s);
                        break;
                case  10:
                        System.out.println("compile  pass!");
                }
                scan.close();
        }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值