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




