public class Square{
private double height;
private double weight;
private double surfaceArea;
public Square(double height,double weigth){
this.height=height;
this.weight=weight;
}
public void computeSurfaceArea(){
surfaceArea=getHeight() * getWeight();
}
public double getHeight(){
return height;
}
public double getWeight(){
return weight;
}
public double getSurfaceArea(){
return surfaceArea;
}
}
public class DemoSquare{
public static void main(String args[]){
Square square=new Square(3,4);
square.computeSurfaceArea();
System.out.println(square.getSurfaceArea());
}
}
----------------解决方案--------------------------------------------------------
public Square(double height,double weigth){
this.height=height;
this.weight=weight;
}
你打错字了,按你的程序应该是weight
否则weight就是0了,那结果当然是0;
----------------解决方案--------------------------------------------------------
呵呵,楼上的真是高明啊,这么细节的东西都看出来了,佩服。我找了半天啊;
----------------解决方案--------------------------------------------------------
强!
----------------解决方案--------------------------------------------------------
这样也不对 还应该去掉第一个public
因为一个程序中只能有一个主类 程序如下:
class Square{
private double height;
private double weight;
private double surfaceArea;
public Square(double height,double weight){
this.height=height;
this.weight=weight;
}
public void computeSurfaceArea(){
surfaceArea=getHeight() * getWeight();
}
public double getHeight(){
return height;
}
public double getWeight(){
return weight;
}
public double getSurfaceArea(){
return surfaceArea;
}
}
public class DemoSquare{
public static void main(String args[]){
Square square=new Square(3,4);
square.computeSurfaceArea();
System.out.println(square.getSurfaceArea());
}
}
----------------解决方案--------------------------------------------------------
这两个类不在一个文件不就可以都是pubilc 了
----------------解决方案--------------------------------------------------------