黑马程序员技术交流社区
标题:
关于匿名内部类
[打印本页]
作者:
心灵的微幸福
时间:
2014-6-23 11:46
标题:
关于匿名内部类
本帖最后由 心灵的微幸福 于 2014-6-26 17:24 编辑
匿名内部类对象的构造方法如何调用父类的非默认构造方法。
作者:
Mr.Hao
时间:
2014-6-23 16:37
若是想实现匿名内部类,那么内部类必须继承一个类或者实现接口,若是接口的话,接口没有构造方法,所以也就没有调用构造方法这事儿了
要是内部类继承的是普通类,我觉得可能是这样写的:
class Inner{
String string = null;
Inner(){}
Inner(String s){
this.string = s;
}
void method(){};
}
class Test{
static Inner function(){
return new Inner("HelloWorld"){
public void method(){
System.out.println(string);
}
};
}
}
作者:
The_king丶
时间:
2014-6-23 21:44
学渣来试试:package GUI; class Dianhua{ Dianhua(){ } Dianhua(String s){ System.out.println(s); } } class Phone extends Dianhua{ Phone(){ new Dianhua("hello!"); } } public class DeleteDemo { public static void main(String args[]){ new Phone(); } }
作者:
The_king丶
时间:
2014-6-23 21:45
学渣来试试:
class Dianhua{
Dianhua(){
}
Dianhua(String s){
System.out.println(s);
}
}
class Phone extends Dianhua{
Phone(){
new Dianhua("hello!");
}
}
public class DeleteDemo {
public static void main(String args[]){
new Phone();
}
}
作者:
The_king丶
时间:
2014-6-23 21:46
The_king丶 发表于 2014-6-23 21:45
学渣来试试:
class Dianhua{
Dianhua(){
有问题请指出,很想知道答案!
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2