当前时区为 UTC + 8 小时



发表新帖 回复这个主题  [ 6 篇帖子 ] 
作者 内容
1 楼 
 文章标题 : [问题]请教JAVA语法问题
帖子发表于 : 2007-04-11 19:27 
头像

注册: 2006-10-12 1:21
帖子: 512
送出感谢: 0 次
接收感谢: 0 次
匿名内部类(有继承的)如何使用构造函数.比如:
class Outer
{
void testadd()
{
Outer.add(new innter(String a)
{
//这个类是一个继承了innter的子类,如何改写他的构造函数呢?
}
)
}
}


页首
 用户资料  
 
2 楼 
 文章标题 : Re: [问题]请教JAVA语法问题
帖子发表于 : 2007-04-12 21:51 

注册: 2007-04-03 14:45
帖子: 12
送出感谢: 0 次
接收感谢: 0 次
liujingjing5 写道:
匿名内部类(有继承的)如何使用构造函数.比如:
class Outer
{
void testadd()
{
Outer.add(new innter(String a)
{
//这个类是一个继承了innter的子类,如何改写他的构造函数呢?
}
)
}
}


首先,你说的这个情况不叫做Inner class,而叫做Anonymous class。改写它的构造函数是做不到的,也就是说显式的声明一个Anonymous class的构造函数是不允许的。但是如果super类(就是你例子里的inner)的构造函数有参数的话,直接写
Outer.add(new inner("string_parameter");
就等于是调用了super("string_parameter")。

参考:
The Java Language Specification, Third Edition

15.9.5.1 Anonymous Constructors
An anonymous class cannot have an explicitly declared constructor. Instead, the compiler must automatically provide an anonymous constructor for the anonymous class. The form of the anonymous constructor of an anonymous class C with direct superclass S is as follows:

If S is not an inner class, or if S is a local class that occurs in a static context, then the anonymous constructor has one formal parameter for each actual argument to the class instance creation expression in which C is declared. The actual arguments to the class instance creation expression are used to determine a constructor cs of S, using the same rules as for method invocations (§15.12). The type of each formal parameter of the anonymous constructor must be identical to the corresponding formal parameter of cs.
The body of the constructor consists of an explicit constructor invocation (§8.8.7.1) of the form super(...), where the actual arguments are the formal parameters of the constructor, in the order they were declared.

Otherwise, the first formal parameter of the constructor of C represents the value of the immediately enclosing instance of i with respect to S. The type of this parameter is the class type that immediately encloses the declaration of S. The constructor has an additional formal parameter for each actual argument to the class instance creation expression that declared the anonymous class. The nth formal parameter e corresponds to the n-1st actual argument. The actual arguments to the class instance creation expression are used to determine a constructor cs of S, using the same rules as for method invocations (§15.12). The type of each formal parameter of the anonymous constructor must be identical to the corresponding formal parameter of cs. The body of the constructor consists of an explicit constructor invocation (§8.8.7.1) of the form o.super(...), where o is the first formal parameter of the constructor, and the actual arguments are the subsequent formal parameters of the constructor, in the order they were declared.
In all cases, the throws clause of an anonymous constructor must list all the checked exceptions thrown by the explicit superclass constructor invocation statement contained within the anonymous constructor, and all checked exceptions thrown by any instance initializers or instance variable initializers of the anonymous class.
Note that it is possible for the signature of the anonymous constructor to refer to an inaccessible type (for example, if such a type occurred in the signature of the superclass constructor cs). This does not, in itself, cause any errors at either compile time or run time.


页首
 用户资料  
 
3 楼 
 文章标题 :
帖子发表于 : 2007-04-12 23:16 
头像

注册: 2006-10-12 1:21
帖子: 512
送出感谢: 0 次
接收感谢: 0 次
谢谢楼上教我,我就是想把下面这个有继承的内部类改写成匿名的内部类.不可以改是吧?
class Outer
{
void testadd()
{
int a = 1,b=2;
Inner innerobj = new Inner(a,b);
Outer.add(innerobj);
}
class Inner extends Another
{
int a,b;
public Inner (int a,int b)
{
super(a);
this.b=b;
}
}
}


页首
 用户资料  
 
4 楼 
 文章标题 :
帖子发表于 : 2007-04-14 15:52 

注册: 2007-04-03 14:45
帖子: 12
送出感谢: 0 次
接收感谢: 0 次
liujingjing5 写道:
谢谢楼上教我,我就是想把下面这个有继承的内部类改写成匿名的内部类.不可以改是吧?
class Outer
{
void testadd()
{
int a = 1,b=2;
Inner innerobj = new Inner(a,b);
Outer.add(innerobj);
}
class Inner extends Another
{
int a,b;
public Inner (int a,int b)
{
super(a);
this.b=b;
}
}
}


对的,打算显式的给一个构造函数给匿名类是做不到的。


页首
 用户资料  
 
5 楼 
 文章标题 :
帖子发表于 : 2007-04-15 6:30 

注册: 2007-04-15 4:12
帖子: 1
送出感谢: 0 次
接收感谢: 0 次
老大你再怎么用Anonymous Class,它本身还是一个完整的语句啊

打个比方:

frame.addMouseMotionListener ( new MouseMotionAdapter () {

// some interesting code here:)

} ); <<-- 最后还是要加上半个括号和分号作为结尾的


页首
 用户资料  
 
6 楼 
 文章标题 :
帖子发表于 : 2007-04-21 9:39 

注册: 2006-09-07 16:02
帖子: 131
送出感谢: 0 次
接收感谢: 0 次
好好的,干嘛一定要那样子去搞呢?


页首
 用户资料  
 
显示帖子 :  排序  
发表新帖 回复这个主题  [ 6 篇帖子 ] 

当前时区为 UTC + 8 小时


在线用户

正在浏览此版面的用户:没有注册用户 和 3 位游客


不能 在这个版面发表主题
不能 在这个版面回复主题
不能 在这个版面编辑帖子
不能 在这个版面删除帖子
不能 在这个版面提交附件

前往 :  
本站点为公益性站点,用于推广开源自由软件,由 DiaHosting VPSBudgetVM VPS 提供服务。
我们认为:软件应可免费取得,软件工具在各种语言环境下皆可使用,且不会有任何功能上的差异;
人们应有定制和修改软件的自由,且方式不受限制,只要他们自认为合适。

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
简体中文语系由 王笑宇 翻译