接口

package test;
//创建Cricle类实现接口Shape2D,重写其中的方法

public class Cricle implements Shape2D {
 double radius;
 public Cricle(double radius){
  this.radius=radius;
 }
 

 @Override
 public double grith() {
  // TODO Auto-generated method stub
  return 2*PI*radius;
 }

 @Override
 public double area() {
  // TODO Auto-generated method stub
  return PI*radius*radius;
 }

}

package test;

import java.util.Scanner;

//创建InterfaceExample类实现图形的周长、面积的计算(要求在控制台输入圆形的半径、矩形的长和宽)
public class InterfaceExample {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Scanner input=new Scanner(System.in);
  System.out.print("请输入圆形的半径:");
  double radius=input.nextDouble();
  System.out.print("请输入长方形的长:");
  double length=input.nextDouble();
  System.out.print("请输入长方形的宽:");
  double width=input.nextDouble();
  Cricle cr=new Cricle(radius);
  Rectangle re=new Rectangle(length,width);
  System.out.println("输出圆的周长:"+cr.grith());
  
  System.out.println("输出圆的面积:"+cr.area());
  
  System.out.println("输出长方形的周长:"+re.grith());
  
  System.out.println("输出长方形的面积:"+re.area());
  
 
 }

}

package test;
//创建Rectangle类实现接口Shape2D,重写其中的方法

public class Rectangle implements Shape2D {
 double length;
 double width;
 public Rectangle(double length,double width){
  this.length=length;
    this.width=width;
  
 }
 
 @Override
 public double grith() {
  // TODO Auto-generated method stub
  return 2*(length+width);
 }

 @Override
 public double area() {
  // TODO Auto-generated method stub
  return length*width;
 }
}

package test;
//创建接口Shape2D,其中定义常量PI为3.14并定义周长与面积的计算方法grith()和area()

public interface Shape2D {
 final double PI=3.14;
 double grith();
 double area();
 
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值