作者aa955123 (古灵精怪枪)
看板java
标题[问题] class的一个小问题
时间Thu Mar 5 19:12:31 2009
刚刚在测试
打了这串程式码
public class testcode
{
class test
{
double hight;
double width;
void area()
{
System.out.println("面积是:");
double ans = (hight*width);
System.out.println(ans);
}
}
public static void main( String args [])
{
test a=new test();
a.hight = 10;
a.width = 10;
a.area();
}
}
出现以下的错误
non-static variable this cannot be referenced from a static context
test a=new test();
^
我仔细的跟书上写的对照了好几次 就是不知道到底哪里出错@@"
请大家帮帮忙
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 114.42.213.144
1F:推 carlcarl: class test() ? 03/05 19:20
2F:→ aa955123:可是 书上在宣告class的时候後面并没有加()耶 03/05 19:24
3F:→ aa955123:我知道问题出在哪了!! 谢谢各位帮忙 XD 03/05 19:30
4F:推 howard666:add "static" on class 03/05 20:31
5F:推 chaostorm:应该是内隐(inner class) 03/05 20:36
6F:→ chaostorm:改成testcode.test a = new testcode().new test(); 03/05 20:38
7F:推 chaostorm:应该就OK了 ^^ 03/05 20:42
8F:推 kucom:为什麽要加static? 有什麽意义呢? 03/05 20:43
9F:推 chaostorm:public static void main 03/05 20:52
11F:推 carlcarl:喔 看错了XD 03/06 00:56