Wang 的个人资料青焰照片日志列表更多 工具 帮助
5月19日

一段代码

在上java eye,看到一个新手的问题,http://bbs.chinajavaworld.com/thread.jspa?messageID=765136򺳐, 挺有意思,摘抄如下:
1.小小代码问题,请高手们指点!
public class ATypeName{
   int x;
   int y;
   AtypeName a = new ATypeName (){
	a.x=3; 
	a.y=7;
   }	
public static void main (String[] args){
	System.out.println ( a.x*a.y );
 
}
}
(注:然后是高手或“高手”们的指点)
2.
 public class ATypeName
{
int x;
int y;

public static void main(String[] args)
{
ATypeName a = new ATypeName ();
a.x=3;
a.y=7;

System.out.println ( a.x*a.y );
}
}
3.
 如果改成这样就可以了,你这种结构的程序我没见过!

public class ATypeName{
int x;
int y;
public static void main (String[] args){
ATypeName a = new ATypeName();
a.x=3;
a.y=7;
System.out.println ( a.x*a.y );
}
}
}
 
4.
public class ATypeName{
   int x;
   int y;
   public ATypeName() {
     this.x = 3;
     this.y = 7;
   }
  static  ATypeName a = new ATypeName ();	
public static void main (String[] args){
	System.out.println ( a.x*a.y );
 
}   
}
 注意当你始话一个函数实例的时候,也就是NEW.....的时候就在在调用构造函数,所以你刚刚的那中写发有点画蛇添足的味道.如果想在MAIN函数外面声明对象最好在前面加个STATIC到底为什么你自己应该清楚,其他的你就看下我给你的原代码吧
 
5.
 class ATypeName {
int x;
int y;

public static void main (String[] args) {
ATypeName a = new ATypeName();
a.x=3;
a.y=7;
System.out.println ( a.x*a.y );
}


}

这应该是标准的程序结构吧……
 
6.
 哥哥你还会发明代码的啊..
看来sun可能会改版时改成你这样..
那.那我和你就是第一个会用的拉..
啊。..哈哈...
 
摘抄结束。
我看到的时候,问题已经解决了,不过,我这个“非高手”觉得真正的,或者和问题最近的解决方法是这样:
public class ATypeName{
   int x;
   int y;
   static AtypeName a = new ATypeName (){
	this.x=3; 
	this.y=7;
   }	
public static void main (String[] args){
	System.out.println ( a.x*a.y );
 
}
}
在最后的留言者看来,我这大概也是发明代码了。呵呵
注:我果然是在发明代码,上面的代码不能compile。我的foundation还是不够扎实,徒增笑耳。