当前位置:编程学习 > JAVA >>

java面试题-----------你觉得这段代码输出的是什么?


public class test05 {

public static void main(String[] args) {
Father f = new Child();
System.out.println(f.x);

}

}

class Father {
int x = 10;

public Father() {
add(20);
}

public void add(int y) {
System.out.println("Father");
x += y;
}
}

class Child extends Father {
int x = 9;

public void add(int y) {
System.out.println("Child");
x += y;
System.out.println(x + ":  :" + y);
}
}

Java --------------------编程问答-------------------- Father
9 --------------------编程问答--------------------
引用 楼主 xzp_ndsc 的回复:

public class test05 {

public static void main(String[] args) {
Father f = new Child();
System.out.println(f.x);

}

}

class Father {
int x = 10;

public Father() {
add(20);
}

public void add(int y) {
System.out.println("Father");
x += y;
}
}

class Child extends Father {
int x = 9;

public void add(int y) {
System.out.println("Child");
x += y;
System.out.println(x + ":  :" + y);
}
}


这题变着法儿的考啊,还不如直接问复杂对象的构造过程!

Child
20:  :20
10
--------------------编程问答-------------------- 求解释啊。。。看不明白 --------------------编程问答--------------------
引用 2 楼 kiritor 的回复:
Quote: 引用 楼主 xzp_ndsc 的回复:


public class test05 {

public static void main(String[] args) {
Father f = new Child();
System.out.println(f.x);

}

}

class Father {
int x = 10;

public Father() {
add(20);
}

public void add(int y) {
System.out.println("Father");
x += y;
}
}

class Child extends Father {
int x = 9;

public void add(int y) {
System.out.println("Child");
x += y;
System.out.println(x + ":  :" + y);
}
}


这题变着法儿的考啊,还不如直接问复杂对象的构造过程!

Child
20:  :20
10

======================================
说得这么简单,不运行的时候,你不一定就能做出来。
我学识太浅,我做错了,我以为结果会为29 : :20
29

执行构造方法时,Child的构造方法之前执行add方法,
而我以前一直认为子类的变量和代码块执行在父类构造方法之前,这一点我错了。
既然add方法时,并没有初始化子父变量,那么x应该是父类的变量,
结果已经应该为30,子父变量为9未初始化,如果初始化了,那么应该为29。
可执行完成后,子父的变量X=9保持不变,父类的变量X=10不变,那么执行add方法的X变量值到哪里去了?
等忙完手头的事情,JVM还是要更多的关注一下。 --------------------编程问答-------------------- 还有,楼主不要纠结这些问题,类的构造顺序要弄清楚,静态变量-》静态代码-》MINA-》成员变量-》代码块-》构造方法。
向那些特别复杂的,得深入了解JVM才能理解。慢慢成长吧。静态的执行父-》子。初始化的成员与构造方法,按上题执行应该是初始化父类完成再初始化子类。 --------------------编程问答-------------------- 其实这个问题我知道,只是觉得这道题比较有趣 --------------------编程问答--------------------
引用 4 楼 lizhengguang 的回复:
Quote: 引用 2 楼 kiritor 的回复:

Quote: 引用 楼主 xzp_ndsc 的回复:


public class test05 {

public static void main(String[] args) {
Father f = new Child();
System.out.println(f.x);

}

}

class Father {
int x = 10;

public Father() {
add(20);
}

public void add(int y) {
System.out.println("Father");
x += y;
}
}

class Child extends Father {
int x = 9;

public void add(int y) {
System.out.println("Child");
x += y;
System.out.println(x + ":  :" + y);
}
}


这题变着法儿的考啊,还不如直接问复杂对象的构造过程!

Child
20:  :20
10

======================================
说得这么简单,不运行的时候,你不一定就能做出来。
我学识太浅,我做错了,我以为结果会为29 : :20
29

执行构造方法时,Child的构造方法之前执行add方法,
而我以前一直认为子类的变量和代码块执行在父类构造方法之前,这一点我错了。
既然add方法时,并没有初始化子父变量,那么x应该是父类的变量,
结果已经应该为30,子父变量为9未初始化,如果初始化了,那么应该为29。
可执行完成后,子父的变量X=9保持不变,父类的变量X=10不变,那么执行add方法的X变量值到哪里去了?
等忙完手头的事情,JVM还是要更多的关注一下。


--------------------编程问答--------------------
引用 7 楼 kiritor 的回复:
Quote: 引用 4 楼 lizhengguang 的回复:

Quote: 引用 2 楼 kiritor 的回复:

Quote: 引用 楼主 xzp_ndsc 的回复:


public class test05 {

public static void main(String[] args) {
Father f = new Child();
System.out.println(f.x);

}

}

class Father {
int x = 10;

public Father() {
add(20);
}

public void add(int y) {
System.out.println("Father");
x += y;
}
}

class Child extends Father {
int x = 9;

public void add(int y) {
System.out.println("Child");
x += y;
System.out.println(x + ":  :" + y);
}
}


这题变着法儿的考啊,还不如直接问复杂对象的构造过程!

Child
20:  :20
10

======================================
说得这么简单,不运行的时候,你不一定就能做出来。
我学识太浅,我做错了,我以为结果会为29 : :20
29

执行构造方法时,Child的构造方法之前执行add方法,
而我以前一直认为子类的变量和代码块执行在父类构造方法之前,这一点我错了。
既然add方法时,并没有初始化子父变量,那么x应该是父类的变量,
结果已经应该为30,子父变量为9未初始化,如果初始化了,那么应该为29。
可执行完成后,子父的变量X=9保持不变,父类的变量X=10不变,那么执行add方法的X变量值到哪里去了?
等忙完手头的事情,JVM还是要更多的关注一下。



===================================
这图表示什么意思,如果你很强,不要给出运行结果,给出代码在运行过程中的路径,包括在JVM的状态。要不然,楼主怎么明白? --------------------编程问答-------------------- 这个的确很难啊 --------------------编程问答-------------------- 跟着debug走,发现f.x方法的时候,f的x和c的x会恢复成9和10(前面构造函数的时候都变成10和20了,但是构造函数执行完,这两个值就被清空了……) --------------------编程问答-------------------- 执行顺序:f构造器,f.x = 10,后进c的add方法,由y传值20(此时c.x = 0),后f构造方法结束,进c构造方法c.x=9,到此这个
Father f = new Child();
方法结束并且两个x都被清掉
在下一行输出f.x的时候,f.x=10,c.x=9……你说会输出哪个呢?
完毕…… --------------------编程问答-------------------- 感觉楼主的题目相当有意思,小弟好好的研究了一番,抛砖引玉一把。

// 原则1:属性不多态(计算机认为同名变量不相同)
// 原则2:方法多态(哪个类方法就是用哪个类属性)

public class test05 {

public static void main(String[] args) {
Father f = new Child();
// 原则1:属性不多态,可以想象一下将Father里的属性x改名为fx,那么打印f.fx肯定是10
System.out.println(f.x);

}

}

class Father {
int x = 10;

public Father() {
// 原则2:调用的是子类方法,本例因为Child的x还未赋值,默认是0。调用的结果只是改变了子类对象的变量值,不会改变父类变量值
add(20);
// 后加的语句,这个才会改变父类的变量值。
// x = 100;
}

public void add(int y) {
System.out.println("Father");
x += y;
}
}

class Child extends Father {
int x = 9;

// 原则2:本方法只会改变本类里定义的变量Child类里的x
public void add(int y) {
System.out.println("Child");
x += y;
System.out.println(x + ":  :" + y);
// 后加的语句,这个才会改变父类的变量值。
// super.x = 100;
}
}
--------------------编程问答-------------------- 感谢5楼的兄弟指点,不过测试发现你说的有点不准确:
应该是:代码块、成员变量的优先级相同,由源代码定义的先后决定。如下代码就是代码块先执行。
public class T {
{
a = 9;
}
int a = 100;
public static void main(String[] args) {
T t = new T();
System.out.println(t.a);
}
}
--------------------编程问答--------------------
引用 13 楼 u010255083 的回复:
感谢5楼的兄弟指点,不过测试发现你说的有点不准确:
应该是:代码块、成员变量的优先级相同,由源代码定义的先后决定。如下代码就是代码块先执行。
public class T {
{
a = 9;
}
int a = 100;
public static void main(String[] args) {
T t = new T();
System.out.println(t.a);
}
}

==========================
老大,你这样写都无法编译通过呀。 --------------------编程问答--------------------
引用 14 楼 lizhengguang 的回复:
Quote: 引用 13 楼 u010255083 的回复:

感谢5楼的兄弟指点,不过测试发现你说的有点不准确:
应该是:代码块、成员变量的优先级相同,由源代码定义的先后决定。如下代码就是代码块先执行。
public class T {
{
a = 9;
}
int a = 100;
public static void main(String[] args) {
T t = new T();
System.out.println(t.a);
}
}

==========================
老大,你这样写都无法编译通过呀。


我这是从eclipse里面拷出来的代码,怎么没法编译通过呢? --------------------编程问答--------------------
引用 13 楼 u010255083 的回复:
感谢5楼的兄弟指点,不过测试发现你说的有点不准确:
应该是:代码块、成员变量的优先级相同,由源代码定义的先后决定。如下代码就是代码块先执行。
public class T {
{
a = 9;
}
int a = 100;
public static void main(String[] args) {
T t = new T();
System.out.println(t.a);
}
}


父类静态代码块->子类静态代码块->父类构造函数->父类非静态代码块->子类构造函数->子类非静态代码块 --------------------编程问答--------------------
引用 15 楼 u010255083 的回复:
Quote: 引用 14 楼 lizhengguang 的回复:

Quote: 引用 13 楼 u010255083 的回复:

感谢5楼的兄弟指点,不过测试发现你说的有点不准确:
应该是:代码块、成员变量的优先级相同,由源代码定义的先后决定。如下代码就是代码块先执行。
public class T {
{
a = 9;
}
int a = 100;
public static void main(String[] args) {
T t = new T();
System.out.println(t.a);
}
}

==========================
老大,你这样写都无法编译通过呀。


我这是从eclipse里面拷出来的代码,怎么没法编译通过呢?
{}这个叫构造代码块 先于构造函数执行 --------------------编程问答-------------------- 这个题目,我表示跪了,不运行的话,还真的不知道得到什么东西…… --------------------编程问答-------------------- Child
20:  :20
10 --------------------编程问答--------------------
引用 16 楼 mr____chen 的回复:
Quote: 引用 13 楼 u010255083 的回复:

感谢5楼的兄弟指点,不过测试发现你说的有点不准确:
应该是:代码块、成员变量的优先级相同,由源代码定义的先后决定。如下代码就是代码块先执行。
public class T {
{
a = 9;
}
int a = 100;
public static void main(String[] args) {
T t = new T();
System.out.println(t.a);
}
}


父类静态代码块->子类静态代码块->父类构造函数->父类非静态代码块->子类构造函数->子类非静态代码块

非静态代码块是先于构造方法执行的,要不然构造方法怎么能起到初始化成员的作用 --------------------编程问答-------------------- 所实话 我没有看懂 --------------------编程问答-------------------- 好吧,我做错了 --------------------编程问答-------------------- 除 --------------------编程问答-------------------- 考的是类(继承情况下)的初始化顺序
代码注释中用()标识的是代码执行顺序,看一下就明白了



public class test05 {
 
    public static void main(String[] args) {
     System.out.println("(1)");
        Father f = new Child();//(1)
        System.out.println("(7)");
        System.out.println(f.x);//(7) Father.x = 10
 
    }
 
}
 
class Father {
    int x = 10;//(2)
    
    {
     System.out.println("(2)");
    }
    
    public Father() {
     System.out.println("(3)");
        add(20);//(3)
    }
 
    //子类有重载,无视之
    public void add(int y) {
        System.out.println("Father");
        x += y;
    }
}
 
class Child extends Father {
    int x = 9;//(5),在5之前,x的值为0(还没有初始化)
 
    {
     System.out.println("(5)");
    }
    
    public Child() {
     super();//(1)
     //(6)
     System.out.println("(6)");
    }

    public void add(int y) {
     System.out.println("(4)");
     //(4)此时Child.x = 0
        System.out.println("Child");
        x += y;//x = 0 + 20 = 20
        System.out.println(x + ":  :" + y);//20,20
    }
}


--------------------编程问答-------------------- 好题目啊,我又错了。。。 --------------------编程问答-------------------- 我发现自己好好脆 --------------------编程问答--------------------
引用 24 楼 zyc13701469860 的回复:
考的是类(继承情况下)的初始化顺序
代码注释中用()标识的是代码执行顺序,看一下就明白了



public class test05 {
 
    public static void main(String[] args) {
     System.out.println("(1)");
        Father f = new Child();//(1)
        System.out.println("(7)");
        System.out.println(f.x);//(7) Father.x = 10
 
    }
 
}
 
class Father {
    int x = 10;//(2)
    
    {
     System.out.println("(2)");
    }
    
    public Father() {
     System.out.println("(3)");
        add(20);//(3)
    }
 
    //子类有重载,无视之
    public void add(int y) {
        System.out.println("Father");
        x += y;
    }
}
 
class Child extends Father {
    int x = 9;//(5),在5之前,x的值为0(还没有初始化)
 
    {
     System.out.println("(5)");
    }
    
    public Child() {
     super();//(1)
     //(6)
     System.out.println("(6)");
    }

    public void add(int y) {
     System.out.println("(4)");
     //(4)此时Child.x = 0
        System.out.println("Child");
        x += y;//x = 0 + 20 = 20
        System.out.println(x + ":  :" + y);//20,20
    }
}


那请解释一下下面的代码:

class A{

static{
System.out.print("1");
}

public A(){
System.out.print("2");
}
}

class B extends A{

static{
System.out.print("a");
}

public B(){System.out.print("b");
}

}

public class Hello{
public static void main(String[] ars)
{
A ab = new B(); 
ab = new B(); 
}


}
--------------------编程问答-------------------- 明白了代码的初始化顺序!好题目! --------------------编程问答--------------------
引用 27 楼 ganshenml 的回复:
Quote: 引用 24 楼 zyc13701469860 的回复:

考的是类(继承情况下)的初始化顺序
代码注释中用()标识的是代码执行顺序,看一下就明白了



public class test05 {
 
    public static void main(String[] args) {
     System.out.println("(1)");
        Father f = new Child();//(1)
        System.out.println("(7)");
        System.out.println(f.x);//(7) Father.x = 10
 
    }
 
}
 
class Father {
    int x = 10;//(2)
    
    {
     System.out.println("(2)");
    }
    
    public Father() {
     System.out.println("(3)");
        add(20);//(3)
    }
 
    //子类有重载,无视之
    public void add(int y) {
        System.out.println("Father");
        x += y;
    }
}
 
class Child extends Father {
    int x = 9;//(5),在5之前,x的值为0(还没有初始化)
 
    {
     System.out.println("(5)");
    }
    
    public Child() {
     super();//(1)
     //(6)
     System.out.println("(6)");
    }

    public void add(int y) {
     System.out.println("(4)");
     //(4)此时Child.x = 0
        System.out.println("Child");
        x += y;//x = 0 + 20 = 20
        System.out.println(x + ":  :" + y);//20,20
    }
}


那请解释一下下面的代码:

class A{

static{
System.out.print("1");
}

public A(){
System.out.print("2");
}
}

class B extends A{

static{
System.out.print("a");
}

public B(){System.out.print("b");
}

}

public class Hello{
public static void main(String[] ars)
{
A ab = new B(); 
ab = new B(); 
}


}

这个就简单了:1a2b2b --------------------编程问答--------------------
引用 29 楼 lxgwm2008 的回复:
Quote: 引用 27 楼 ganshenml 的回复:

Quote: 引用 24 楼 zyc13701469860 的回复:

考的是类(继承情况下)的初始化顺序
代码注释中用()标识的是代码执行顺序,看一下就明白了



public class test05 {
 
    public static void main(String[] args) {
     System.out.println("(1)");
        Father f = new Child();//(1)
        System.out.println("(7)");
        System.out.println(f.x);//(7) Father.x = 10
 
    }
 
}
 
class Father {
    int x = 10;//(2)
    
    {
     System.out.println("(2)");
    }
    
    public Father() {
     System.out.println("(3)");
        add(20);//(3)
    }
 
    //子类有重载,无视之
    public void add(int y) {
        System.out.println("Father");
        x += y;
    }
}
 
class Child extends Father {
    int x = 9;//(5),在5之前,x的值为0(还没有初始化)
 
    {
     System.out.println("(5)");
    }
    
    public Child() {
     super();//(1)
     //(6)
     System.out.println("(6)");
    }

    public void add(int y) {
     System.out.println("(4)");
     //(4)此时Child.x = 0
        System.out.println("Child");
        x += y;//x = 0 + 20 = 20
        System.out.println(x + ":  :" + y);//20,20
    }
}


那请解释一下下面的代码:

class A{

static{
System.out.print("1");
}

public A(){
System.out.print("2");
}
}

class B extends A{

static{
System.out.print("a");
}

public B(){System.out.print("b");
}

}

public class Hello{
public static void main(String[] ars)
{
A ab = new B(); 
ab = new B(); 
}


}

这个就简单了:1a2b2b
运行一下看看! --------------------编程问答-------------------- 又到了找工作的季节。。 --------------------编程问答--------------------
引用 30 楼 ganshenml 的回复:
Quote: 引用 29 楼 lxgwm2008 的回复:

Quote: 引用 27 楼 ganshenml 的回复:

Quote: 引用 24 楼 zyc13701469860 的回复:

考的是类(继承情况下)的初始化顺序
代码注释中用()标识的是代码执行顺序,看一下就明白了



public class test05 {
 
    public static void main(String[] args) {
     System.out.println("(1)");
        Father f = new Child();//(1)
        System.out.println("(7)");
        System.out.println(f.x);//(7) Father.x = 10
 
    }
 
}
 
class Father {
    int x = 10;//(2)
    
    {
     System.out.println("(2)");
    }
    
    public Father() {
     System.out.println("(3)");
        add(20);//(3)
    }
 
    //子类有重载,无视之
    public void add(int y) {
        System.out.println("Father");
        x += y;
    }
}
 
class Child extends Father {
    int x = 9;//(5),在5之前,x的值为0(还没有初始化)
 
    {
     System.out.println("(5)");
    }
    
    public Child() {
     super();//(1)
     //(6)
     System.out.println("(6)");
    }

    public void add(int y) {
     System.out.println("(4)");
     //(4)此时Child.x = 0
        System.out.println("Child");
        x += y;//x = 0 + 20 = 20
        System.out.println(x + ":  :" + y);//20,20
    }
}


那请解释一下下面的代码:

class A{

static{
System.out.print("1");
}

public A(){
System.out.print("2");
}
}

class B extends A{

static{
System.out.print("a");
}

public B(){System.out.print("b");
}

}

public class Hello{
public static void main(String[] ars)
{
A ab = new B(); 
ab = new B(); 
}


}

这个就简单了:1a2b2b
运行一下看看!

这就是我运行的结果 --------------------编程问答--------------------
引用 32 楼 lxgwm2008 的回复:
Quote: 引用 30 楼 ganshenml 的回复:

Quote: 引用 29 楼 lxgwm2008 的回复:

Quote: 引用 27 楼 ganshenml 的回复:

Quote: 引用 24 楼 zyc13701469860 的回复:

考的是类(继承情况下)的初始化顺序
代码注释中用()标识的是代码执行顺序,看一下就明白了



public class test05 {
 
    public static void main(String[] args) {
     System.out.println("(1)");
        Father f = new Child();//(1)
        System.out.println("(7)");
        System.out.println(f.x);//(7) Father.x = 10
 
    }
 
}
 
class Father {
    int x = 10;//(2)
    
    {
     System.out.println("(2)");
    }
    
    public Father() {
     System.out.println("(3)");
        add(20);//(3)
    }
 
    //子类有重载,无视之
    public void add(int y) {
        System.out.println("Father");
        x += y;
    }
}
 
class Child extends Father {
    int x = 9;//(5),在5之前,x的值为0(还没有初始化)
 
    {
     System.out.println("(5)");
    }
    
    public Child() {
     super();//(1)
     //(6)
     System.out.println("(6)");
    }

    public void add(int y) {
     System.out.println("(4)");
     //(4)此时Child.x = 0
        System.out.println("Child");
        x += y;//x = 0 + 20 = 20
        System.out.println(x + ":  :" + y);//20,20
    }
}


那请解释一下下面的代码:

class A{

static{
System.out.print("1");
}

public A(){
System.out.print("2");
}
}

class B extends A{

static{
System.out.print("a");
}

public B(){System.out.print("b");
}

}

public class Hello{
public static void main(String[] ars)
{
A ab = new B(); 
ab = new B(); 
}


}

这个就简单了:1a2b2b
运行一下看看!

这就是我运行的结果

哦,3可有! --------------------编程问答--------------------
引用 17 楼 fatg1988 的回复:
Quote: 引用 15 楼 u010255083 的回复:

Quote: 引用 14 楼 lizhengguang 的回复:

Quote: 引用 13 楼 u010255083 的回复:

感谢5楼的兄弟指点,不过测试发现你说的有点不准确:
应该是:代码块、成员变量的优先级相同,由源代码定义的先后决定。如下代码就是代码块先执行。
public class T {
{
a = 9;
}
int a = 100;
public static void main(String[] args) {
T t = new T();
System.out.println(t.a);
}
}

==========================
老大,你这样写都无法编译通过呀。


我这是从eclipse里面拷出来的代码,怎么没法编译通过呢?
{}这个叫构造代码块 先于构造函数执行


构造代码块并不是先于构造函数执行,它依托于构造函数,即相当于会把代码块内的代码插入每个构造函数的最前面执行,除非遇到this方法

     public class Client {
          {
                 // 构造代码块
                System. out.println( "执行构造代码块" );
          }
          public Client(){
                System. out.println( "执行无参构造" );
          }
          public Client(String _str){
                System. out.println( "执行有参构造" );
          }
     }
补充:Java ,  Java相关
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,