一个关于Class类的小小疑惑

作者在 2011-10-29 10:44:13 发布以下内容

class Rect 

{ double width,height,area;

  public double getArea()

  {  area=height*width;

     return area;

  }

}

public class Boy

{  public static void main(String args[])

  try{  Class cs=Class.forName("Rect");

            Rect rect=(Rect)cs.newInstance();

            rect.width=100;

            rect.height=200;

            System.out.println("rect的面积"+rect.getArea());

          }

       catch(Exception e){} 

   }  

}

此程序运行没有结果 求解释??

默认分类 | 阅读 846 次
文章评论,共2条
李臣
2011-10-29 22:51
1
不会吧,在JCreator下,有结果呀,不知道你是在什么环境下运行的<br />
还有不明白你的main方法为什么要那样写<br />
写成这样:<br />
&nbsp; &nbsp; &nbsp; &nbsp; public static void main(String args[])<br />
 {&nbsp;&nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;Rect rect=new Rect();<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;rect.width=100;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;rect.height=200;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;System.out.println(&quot;rect的面积&quot;+rect.getArea());<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;<br />
不也可以吗?
wangjiangliu(作者)
2011-10-30 09:43
2
尝试下Class这个类的用法嘛,我在eclipse下运行的
游客请输入验证码
文章分类
文章归档