当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
关于Java构造方法
发布时间:2010/7/7 10:37:04 来源:城市学习网 编辑:ziteng
  java中,在创建一个新的对象的时候,会调用其父亲的构造方法,测试程序如下:
  class parent{
  int a=5;
  public parent(){
  System.out.println("a="+a);
  System.out.println("parent()");
  }
  public parent(int b){
  System.out.println("a="+a);
  System.out.println("parent(int a)");
  }
  }
  class child extends parent{
  public child(){
  super(9);
  System.out.println("child()");
  }
  }
  public class Test{
  public static void main(String[] args){
  child c=new child();
  }
  }
  此时的输出结果为:
  a=5
  parent(int a)
  child
  而如果去掉parent的无参构造函数,且将super(9)注释掉,则会产生错误,提示找不到parent()方法。因为,如果不显性的使用super,子类会调用父类的构造方法,而只要显示的写了构造方法,缺省构造方法就不存在了,呵呵!
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved