编写一个java应用程序,定义一个接口,包括一个方法areas(),在rectangle勒,squ
发布网友
发布时间:2022-05-23 01:26
我来回答
共2个回答
热心网友
时间:2023-10-09 16:39
public interface Shape{
void areas();
}
public class Rectangle implement Shape{
public void areas(){
System.out.println("the area of rectangle");
}
public class Square implement Shape{
public void areas(){
System.out.println("the area of square");
}
public class Round implement Shape{
public void areas(){
System.out.println("the area of round");
}
public class Test{
public static void main(String[] args){
Shape r =new Rectangle();
Shape s =new Square();
Shape round =new Round();
r.areas();
s.areas();
round.areas();
}
}
}
完全手打的,只是给你一个思路
热心网友
时间:2023-10-09 16:39
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();
}
}