天天看点

java定义一个接口shape_编写一个java应用程序,定义一个接口,包含一个方法areas(),在rectangle勒,squ...

import static java.lang.Math.*;

import java.util.*;

interface Shape{

void areas();

}

class rectangle implements Shape{

float h,w;

public rectangle(float h,float w){

this.h=h;

this.w=w;

}

public void areas(){

System.out.println("rectangle 面积为:"+h*w);

}

}

class square implements Shape{

float h;

public square(float h){

this.h=h;

}

public void areas(){

System.out.println("square 面积为:"+h*h);

}

}

class round implements Shape{

float r;

public round(float r){

this.r=r;

}

public void areas(){

System.out.println("round 面积为:"+PI*r*r);

}

}

class test{

public static void main(String[]args){

rectangle r=new rectangle(5,3);

square s=new square(5);

round ro=new round(3);

r.areas();

s.areas();

ro.areas();

}

}

取消

评论