代码如下:
import java.io.*; import java.math.*;//导入math interface Graph{ abstract void catercorner(); } class Rectangle implements Graph{ double length, width; Rectangle(){ length=0;width=0; } Rectangle(int i,int j){ length=i;width=j; } public void catercorner(){ double catercorner_long=length*length+width*width; System.out.println(Math.sqrt(catercorner_long)); } } class ClassSquare extends Rectangle{ double r=0; void fuzhi(double num){ r=num; } public void catercorner(){ double catercorner_long=r*r+r*r; System.out.println(Math.sqrt(catercorner_long)); } } public class JieKou { public static void main(String args[]) throws IOException{ double num=0; BufferedReader din=new BufferedReader(new InputStreamReader(System.in)); System.out.print("请输入正方形边长:"); num=Integer.parseInt(din.readLine()); ClassSquare s=new ClassSquare(); s.fuzhi(num); s.catercorner(); } }
结果演示:
编辑