CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Java >  J2SE / 基础类

请教一下,这个程序的运行结果是多少?有没有不用上机就可以给出答案来的吗???

楼主java33(三岁就很酷)2005-08-01 21:10:19 在 Java / J2SE / 基础类 提问

public   class   Count   {  
    public   static   void   prt(String   s){  
          System.out.println(s);  
      }  
          Value   v=new   Value(10);  
        static   Value   v1,v2;  
       
   
          static{  
              prt("v1.c="+v1.c+"     v2.c="+v2.c);  
              v1=new   Value(27);  
              prt("v1.c="+v1.c+"     v2.c="+v2.c);  
              v2=new   Value(15);  
              prt("v1.c="+v1.c+"     v2.c="+v2.c);  
          }  
   
      public   static   void   main(String[]   args){  
          Count   ct=new   Count();  
         
          prt("ct.c="+ct.v.c);  
          prt("v1.c="+v1.c+"     v2.c="+v2.c);  
          v1.inc();  
          prt("v1.c="+v1.c+"     v2.c="+v2.c);  
          prt("ct.c="+ct.v.c);  
      }  
  }  
  class   Value{  
      static   int   c=1;  
      Value(){  
          c=15;  
      }  
      Value(int   i){  
          c=i;  
      }  
      static   void   inc(){  
          c++;  
      }  
  } 问题点数:20、回复次数:5Top

1 楼java33(三岁就很酷)回复于 2005-08-01 21:15:09 得分 0

是这样的,  
   
  /*  
    *   创建日期   2005-8-1  
    *  
    *   TODO   要更改此生成的文件的模板,请转至  
    *   窗口   -   首选项   -   Java   -   代码样式   -   代码模板  
    */  
   
  /**  
    *   @author   Administrator  
    *    
    *   TODO   要更改此生成的类型注释的模板,请转至   窗口   -   首选项   -   Java   -   代码样式   -   代码模板  
    */  
  public   class   Count   {  
  public   static   void   prt(String   s)   {  
  System.out.println(s);  
  }  
   
  Value   v   =   new   Value(10);  
   
  static   Value   v1,   v2;  
   
  static   {  
  prt("v1.c="   +   v1.c   +   "     v2.c="   +   v2.c);  
  v1   =   new   Value(27);  
  prt("v1.c="   +   v1.c   +   "     v2.c="   +   v2.c);  
  v2   =   new   Value(15);  
  prt("v1.c="   +   v1.c   +   "     v2.c="   +   v2.c);  
  }  
   
  public   static   void   main(String[]   args)   {  
  Count   ct   =   new   Count();  
   
  prt("ct.c="   +   ct.v.c);  
  prt("v1.c="   +   v1.c   +   "     v2.c="   +   v2.c);  
  v1.inc();  
  prt("v1.c="   +   v1.c   +   "     v2.c="   +   v2.c);  
  prt("ct.c="   +   ct.v.c);  
  }  
  }  
   
  class   Value   {  
  static   int   c   =   0;  
   
  Value()   {  
  c   =   15;  
  }  
   
  Value(int   i)   {  
  c   =   i;  
  }  
   
  static   void   inc()   {  
  c++;  
  }  
  }Top

2 楼kill8108(日月之光)回复于 2005-08-01 21:42:52 得分 0

引用类的静态--->本类静态静态---->本类其它  
   
  我也不是很懂呢!!大家谈谈啦!!Top

3 楼wzrain(晨雨)回复于 2005-08-01 21:50:29 得分 0

靠,你是在考人,还是在问人啊?Top

4 楼interhanchi(on the Java Road)回复于 2005-08-01 22:09:55 得分 0

v1.c=1     v2.c=1  
  v1.c=27     v2.c=27  
  v1.c=15     v2.c=15  
  ct.c=10  
  v1.c=10     v2.c=10  
  v1.c=11     v2.c=11  
  ct.c=11  
   
  看了这个你就明白了   !  
  The   order   of   initialization   is   statics   first,   if   they   haven’t   already   been   initialized   by   a   previous   object   creation,   and   then   the   non-static   objects.   You   can   see   the   evidence   of   this   in   the   output.    
   
   
  It’s   helpful   to   summarize   the   process   of   creating   an   object.   Consider   a   class   called   Dog:    
   
   
   
  The   first   time   an   object   of   type   Dog   is   created   (the   constructor   is   actually   a   static   method),   or   the   first   time   a   static   method   or   static   field   of   class   Dog   is   accessed,   the   Java   interpreter   must   locate   Dog.class,   which   it   does   by   searching   through   the   classpath.    
   
  As   Dog.class   is   loaded   (creating   a   Class   object,   which   you’ll   learn   about   later),   all   of   its   static   initializers   are   run.   Thus,   static   initialization   takes   place   only   once,   as   the   Class   object   is   loaded   for   the   first   time.    
   
  When   you   create   a   new   Dog(   ),   the   construction   process   for   a   Dog   object   first   allocates   enough   storage   for   a   Dog   object   on   the   heap.  
   
  This   storage   is   wiped   to   zero,   automatically   setting   all   the   primitives   in   that   Dog   object   to   their   default   values   (zero   for   numbers   and   the   equivalent   for   boolean   and   char)   and   the   references   to   null.    
   
  Any   initializations   that   occur   at   the   point   of   field   definition   are   executed.   Feedback    
  Constructors   are   executed.   As   you   shall   see   in   Chapter   6,   this   might   actually   involve   a   fair   amount   of   activity,   especially   when   inheritance   is   involved.   Feedback  
  Top

5 楼deeplysea(爱是你眼里的一首情歌)回复于 2005-08-23 20:57:52 得分 0

v1.c=1     v2.c=1  
  v1.c=27     v2.c=27  
  v1.c=15     v2.c=15  
  ct.c=10  
  v1.c=10     v2.c=10  
  v1.c=11     v2.c=11  
  ct.c=11  
  Top

相关问题

  • 运行JAVA程序
  • 如何得到程序的运行目录?谁先给答案,分旧全部是谁的。
  • 一个小问题,怎么在java中运行另一个应用程序?收到答案立即结贴
  • 单独运行程序
  • 程序重复运行
  • installshield运行外部程序
  • 运行memfile程序问题
  • 程序运行错误
  • 如何运行WINDOWS程序?
  • 运行外部程序!

关键词

  • c++
  • 模板
  • 代码
  • prt
  • ct
  • dog
  • 静态
  • static
  • initialization
  • count

得分解答快速导航

  • 帖主:java33

相关链接

  • CSDN Java频道
  • Java类图书
  • Java类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo