java英雄相互攻击接口实现_Java 接口示例以及实现

接口的实现与类的继承是相似的,不同之处是:实现接口的类不从该接口的定义中继承任何行为,为实现该接口的类的任何对象中都能调用这个接口中定义的方法。接口实际上就是一个特殊的抽象类,同时实现多个接口就意味着有多重继承的功能。

package 接口示例;

public interface Shape2D {

double PI = 3.14;

double grith();

double area();

}

package 接口示例;

import java.text.DecimalFormat;

import java.util.Scanner;

class Cricle implements Shape2D{

double radius;

public Cricle(double r){

radius = r;

}

@Override

public double grith() {

// TODO 自动生成的方法存根

return 2*PI*radius;

}

@Override

public double area() {

// TODO 自动生成的方法存根

return PI*radius*radius;

}

}

class Rectangle implements Shape2D{

double width,heigth;

public Rectangle(double w,double h){

width = w; heigth = h;

}

@Override

public double grith() {

// TODO 自动生成的方法存根

return 2*(width+heigth);

}

@Override

public double area() {

// TODO 自动生成的方法存根

return width*heigth;

}

}

public class InterfaceExample {

public static void main(String[] args) {

// TODO 自动生成的方法存根

DecimalFormat df = new DecimalFormat("0.00");

Scanner scan = new Scanner(System.in);

System.out.print("请输入圆的半径 r = ");

double r;

r = scan.nextInt();

Cricle cricle = new Cricle(r);

System.out.println("此圆的周长为:"+ df.format(cricle.grith()));

System.out.println("此圆的面积为:"+ df.format(cricle.area()));

System.out.println();

System.out.print("请输入矩形的长 h = ");

double h;

h = scan.nextInt();

System.out.print("请输入矩形的长 w = ");

double w;

w = scan.nextInt();

Rectangle rectangle = new Rectangle(w, h);

System.out.println("此矩形的周长为:"+ df.format(rectangle.grith()));

System.out.println("此矩形的面积为:"+ df.format(rectangle.area()));

}

}

fc4cb90d873d4b7a89829e2d6a1d5f81.jpg

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值