首页 | 源码下载 | 网站模板 | 网页特效 | 广告代码 | 网页素材 | 字体下载 | 书库 | 站长工具
会员投稿 投稿指南 RSS订阅
当前位置:主页>网络编程>java教程>资讯:JAVA初始化的陷阱

JAVA初始化的陷阱

www.jz123.cn  2010-01-05   来源:   中国建站    责任编辑(袁袁)    我要投递新闻

为了说明这个问题先给大家出个问题吧:

  请看代码:


     view plaincopy to clipboardprint?
  public abstract class A {
  public A() {
  initMethod();
  }
  public abstract void initMethod();
  }
  public abstract class A {
  public A() {
  initMethod();
  }
  public abstract void initMethod();
  }
  view plaincopy to clipboardprint?
  public class B extends A {
  static String staticStr = "static1";
  private String testStr = "Test1";
  /*
  * (non-Javadoc)
  *
  * @see A#initMethod()
  */
  public void initMethod() {
  // TODO Auto-generated method stub
  testStr = "Test2";
  staticStr = "static2";
  }
  /**
  * @return the testStr
  */
  public String getTestStr() {
  return testStr;
  }
  /**
  * @param testStr
  * the testStr to set
  */
  public void setTestStr(String testStr) {
  this.testStr = testStr;
  }
  /**
  * create data: 2009-12-29

  *
  * @author XiangJie
  * @param args
  *
  */
  public static void main(String[] args) {
  // TODO Auto-generated method stub
  B b = new B();
  System.out.println("the variable testStr is:" + b.testStr);
  System.out.println("the variable staticStr is:" + B.staticStr);
  }
  }
  public class B extends A {
  static String staticStr = "static1";
  private String testStr = "Test1";
  /*
  * (non-Javadoc)
  *
  * @see A#initMethod()
  */
  public void initMethod() {
  // TODO Auto-generated method stub
  testStr = "Test2";
  staticStr = "static2";
  }
  /**
  * @return the testStr
  */
  public String getTestStr() {
  return testStr;
  }
  /**
  * @param testStr
  * the testStr to set
  */
  public void setTestStr(String testStr) {
  this.testStr = testStr;
  }
  /**
  * create data: 2009-12-29

  *
  * @author XiangJie
  * @param args
  *
  */
  public static void main(String[] args) {
  // TODO Auto-generated method stub
  B b = new B();
  System.out.println("the variable testStr is:" + b.testStr);
  System.out.println("the variable staticStr is:" + B.staticStr);
  }
  }

  大家给出的输出结果是什么呢?

上一篇:调整Java虚拟机 支持其运行的Java应用程序 下一篇:Java通过jacob对excel,word进行打印等操作

评论总数:0 [ 查看全部 ] 网友评论


关于我们隐私版权广告服务友情链接联系我们网站地图