- <FONT color=red>import java.lang.StrictMath;
- </FONT>interface Printx {
- public void printMyWay();
- }
- class Squre implements Printx{
- double height,wide;
- double area,length;
- Squre(double height,double wide){
- this.height=height;
- this.wide=wide;
- }
- Squre(double height){
- this.height=height;
- }
- public void printMyWay(){
- area=height*wide;
- length=2*(height+wide);
- };
- }
- class Squreson extends Squre {
- double diagonal;
- Squreson(double height)
- {
- super(height);
- <FONT color=red>//因为父类的空参数构造函数已经自动消失,这边一定要手动指定一个父类的构造函数引用;
- //this.height=height;
- </FONT>}
- public void printMyWay(){
- area=height*height;
- length=4*height;
- diagonal=StrictMath.sqrt(2*height*height);
- <FONT color=red>//这边我也不大明白,我查了一下API,想要使用static修饰符所修饰的sqrt(),必须要导入java.lang.StrictMath包或者java.lang.Math包;
- //最后还要提一下,一个java文件只能有一个公共类,而且这个公共类的类名要后java文件名一致;</FONT>
- }
- }
- class Print {
- public static void main(String[] args) {
- Squre squre;
- Squreson squreson;
- squre=new Squre(4,5);
- squre.printMyWay();
- squreson=new Squreson(4);
- squreson.printMyWay();
- }
- }
复制代码 上面的红色字体为我所改动的部分,自己看一下吧!
还有提个小建议,你以后写代码的时候尽量把该标识的地方标识一下,这样提高代码的阅读性
|