日期:2014-05-20  浏览次数:20678 次

本人菜鸟求高手解释啊?这程序哪里出错了??我很费解啊!!
package javabasic;



interface EqualDiagonal{
public void getDiagonal();

}


class Rectangle implements EqualDiagonal{
  int a;
int b;
public Rectangle(int a,int b){
this.a=a;
this.b=b;
  }
public Rectangle(int a){
this.a=a;
this.b=a;
}
public int getchang(){
  return a;
  }
  public int getkuan(){
  return b ;
  }
public int getzhouchang(){
  return (a+b)*2 ;
  }

public int getmianji(){
  return a*b ; 
  }
public void getDiagonal(){
double s=Math.sqrt((double)(a*a+b*b));
System.out.println(s);
}

}



class Square extends Rectangle implements EqualDiagonal {
public Square(int a){
super(a);
}  
public void getDiagonal(){
double s=Math.sqrt((double)(a*a+b*b));
System.out.println(s);
}
}



public class Text{
public static void main(String[] args){
Rectangle m=new Rectangle(3,4);
Square n= new Square(5);
  m.getDiagonal();
n.getDiagonal();


}
}


------解决方案--------------------
Java code
interface EqualDiagonal { 
public void getDiagonal();

}

class Rectangle implements EqualDiagonal {
int a;
int b;

public Rectangle(int a, int b) {
this.a = a;
this.b = b;
}

public Rectangle(int a) {
this.a = a;
this.b = a;
}

public int getchang() {
return a;
}

public int getkuan() {
return b;
}

public int getzhouchang() {
return (a + b) * 2;
}

public int getmianji() {
return a * b;
}

public void getDiagonal() {
double s = Math.sqrt((double) (a * a + b * b));
System.out.println(s);
}

}

class Square extends Rectangle implements EqualDiagonal {
public Square(int a) {
super(a);
}

public void getDiagonal() {
double s = Math.sqrt((double) (a * a + b * b));
System.out.println(s);
}
}

public class Main {
public static void main(String[] args) {
Rectangle m = new Rectangle(3, 4);
Square n = new Square(5);
m.getDiagonal();
n.getDiagonal();

}
}

//result:
//5.0
//7.0710678118654755

------解决方案--------------------
Java code

interface EqualDiagonal
{
    /**
     * 求对角线长度
     */
    public void getDiagonal();

}

/**
 * 长方形
 */
class Rectangle implements EqualDiagonal
{
    int a;
    int b;

    public Rectangle(int a, int b)
    {
        this.a = a;
        this.b = b;
    }

    public int getchang()
    {
        return a;
    }

    public int getkuan()
    {
        return b;
    }

    public int getzhouchang()
    {
        return (a + b) * 2;
    }

    public int getmianji()
    {
        return a * b;
    }

    public void getDiagonal()
    {
        double s = Math.sqrt((double) (a * a + b * b));
        System.out.println(s);
    }

}

/**
 * 正方形
 */
class Square extends Rectangle
{
    public Square(int a)
    {
        super(a, a);
    }

    public void getDiagonal()
    {
        double s = Math.sqrt((double) (a * a + b * b));
        System.out.println(s);
    }
}


public class Text
{
    public static void main(String[] args)
    {
        Rectangle m = new Rectangle(3, 4);
        Square n = new Square(5);
        m.getDiagonal();
        n.getDiagonal();

    }
}