A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 心灵的微幸福 中级黑马   /  2014-6-23 11:46  /  1025 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 心灵的微幸福 于 2014-6-26 17:24 编辑

匿名内部类对象的构造方法如何调用父类的非默认构造方法。

4 个回复

倒序浏览
若是想实现匿名内部类,那么内部类必须继承一个类或者实现接口,若是接口的话,接口没有构造方法,所以也就没有调用构造方法这事儿了
要是内部类继承的是普通类,我觉得可能是这样写的:
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);
                        }
                };
        }
}
回复 使用道具 举报
学渣来试试: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();   } }
回复 使用道具 举报
学渣来试试:
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(){

有问题请指出,很想知道答案!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马