[问题]请教JAVA语法问题

C、C++和Java语言
回复
头像
liujingjing5
帖子: 512
注册时间: 2006-10-12 1:21
送出感谢: 0
接收感谢: 0

[问题]请教JAVA语法问题

#1

帖子 liujingjing5 » 2007-04-11 19:27

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

Re: [问题]请教JAVA语法问题

#2

帖子 abel » 2007-04-12 21:51

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.
头像
liujingjing5
帖子: 512
注册时间: 2006-10-12 1:21
送出感谢: 0
接收感谢: 0

#3

帖子 liujingjing5 » 2007-04-12 23:16

谢谢楼上教我,我就是想把下面这个有继承的内部类改写成匿名的内部类.不可以改是吧?
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;
}
}
}
abel
帖子: 12
注册时间: 2007-04-03 14:45
送出感谢: 0
接收感谢: 0
联系:

#4

帖子 abel » 2007-04-14 15:52

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;
}
}
}
对的,打算显式的给一个构造函数给匿名类是做不到的。
durandal
帖子: 1
注册时间: 2007-04-15 4:12
送出感谢: 0
接收感谢: 0

#5

帖子 durandal » 2007-04-15 6:30

老大你再怎么用Anonymous Class,它本身还是一个完整的语句啊

打个比方:

frame.addMouseMotionListener ( new MouseMotionAdapter () {

// some interesting code here:)

} ); <<-- 最后还是要加上半个括号和分号作为结尾的
winstars
帖子: 131
注册时间: 2006-09-07 16:02
送出感谢: 0
接收感谢: 0

#6

帖子 winstars » 2007-04-21 9:39

好好的,干嘛一定要那样子去搞呢?
回复

回到 “C/C++/Java”