请不要使用工具运行,看看Test类main方法执行的结果是?package test;public class Parent {
public static String staticname = "parentStaticname";
public String name = "Parent";
public String getName(){
return name;
}
}public class Child extends Parent {
public static String staticname = "childStaticname";
public String name = "child";
public String getName(){
return name;
}
public String getchildName(){
return name;
}
}
public class Test {
public static void main(String[] args) {
Parent parent;
Child child;
child = new Child();
parent = (Child)child;

System.out.println(child.getClass());
System.out.println(child.name);
System.out.println(child.getName());

System.out.println(parent.getClass());
System.out.println(parent.name);
System.out.println(parent.getName());

System.out.println(child.staticname);
System.out.println(parent.staticname);


}

解决方案 »

  1.   

    亮点在这里
    System.out.println(parent.name);
      

  2.   


    Java动态绑定不包括域,静态方法
      

  3.   

    检查的是java的一个基础:
    动态字段和静态字段均不能被覆盖!                   
    静态方法也不能被覆盖
      

  4.   

    class test.Child
    child
    child
    class test.Child
    Parent
    child
    childStaticname
    parentStaticname
      

  5.   

    为什么  System.out.println(parent.getName()); 输出是 child啊 !请教下;
      

  6.   

    class test.Child
    child
    child
    class test.Child
    Parent
    child
    childStaticname
    parentStaticname
      

  7.   

    java的动态绑定不包括静态的和字段
      

  8.   

    parent.getName()中的getname方法是child里面的getname()方法,返回的child里面的字段child。
      

  9.   

    检查的是java的一个基础: 
    动态字段和静态字段均不能被覆盖!                    
    静态方法也不能被覆盖 
      

  10.   

    parent.getName()中的getname方法是child里面的getname()方法,返回的child里面的字段child。
    parent = (Child)child; 
    System.out.println(parent.name); 
    System.out.println(parent.getName());
    这个java的上朔造型.
    System.out.println(child.staticname); 
    System.out.println(parent.staticname); 
    输出为
     *childStaticname
     *parentStaticname
    因为子类没有被覆盖. 静态方法也不能被覆盖!
    Child extends Parent
      

  11.   

    parent = (Child)child;  
    System.out.println(parent.name);  
    System.out.println(parent.getName()); 
    上朔造型. 
    子类调用父类的属性,属性是父类的属性.
    子类调用父类的方法时,方法是子类的方法.
    所以为
    /**
     *class test.Chid
     *child
     *chind
     *class test.Parent
     *Parent
     *chind
     *childStaticname
     *parentStaticname
     **/
      

  12.   

    考察的是java的多态机制。
    我理解的 java的多态是指将继承类的对象赋值给基类的引用,然后通过基类引用调用方法和域。这样就可以只操作基类但可以改变导出类。
    运行程序时,方法会动态邦定到方法调用的那个继承类,而不是基类。但是域不是动态邦定的。它在编译期就执行了,找到定义它的地方执行。
    一句话:在继承体系中 ,调用方法看对象所在的类型,调用变量看定义变量的类。
      

  13.   

    我有点不明白,如果:parent = (Child) child 上朔造型
    那么:parent = (Parent) child 向上转型?
    请赐教。
      

  14.   

    下面的代码:
    package com.sytdc.cxl; class Parent {
    public static String staticname = "parentStaticname";
    public String name = "Parent"; public String getName() {
    return name;
    }
    }class Child extends Parent {
    public static String staticname = "childStaticname";
    public String name = "child"; public String getName() {
    return name;
    } public String getchildName() {
    return name;
    }
    }public class Test20 {
    public static void main(String[] args) {
    Parent parent;
    Child child;
    child = new Child();
    parent = (Child)child; System.out.println(child.getClass());
    System.out.println(child.name);
    System.out.println(child.getName()); System.out.println(parent.getClass());
    System.out.println(parent.name);
    System.out.println(parent.getName()); System.out.println(child.staticname);
    System.out.println(parent.staticname); }
    }
    运行结果
    class com.sytdc.cxl.Child
    child
    child
    class com.sytdc.cxl.Child
    Parent
    child
    childStaticname
    parentStaticname
    前面三个输出,
    第一个是带包名的类名。
    第二个是Child类中的name成员变量
    第三个也是Child类中的name成员变量,重写父类中的方法。 
    要想使用父类中的name成员,需要改写如下:
    public String getName() {
    //return name;
    return super.name;
    }
    此时的输出结果:
    class com.sytdc.cxl.Child
    child
    Parent
    4,5,6行的输出:
    第四行输出child类,因为进行了类型转换。
    第五行输出parent类的成员name
    第六行,因为进行了类型转换,所以调用子类的getName方法。
    因为static 成员变量不能被子类覆盖。
    第七行输出childStaticname
    第八行输出parentStaticname
      

  15.   

    上朔造型. 
    子类调用父类的属性,属性是父类的属性.
    子类调用父类的方法时,方法是子类的方法.
    ??!!Parent parent =(Child)child 用父类转子类要强制转换,Parent parent = (Parent)child 把子类转父类 这个是默认的,会报错吧。
      

  16.   

    刚学java 还是能看懂的 呵呵!!!
      

  17.   

    刚学java 还是能看懂了 呵呵!!!
      

  18.   

    parent = (Child)child   这句什么意思啊转换不应该是 parent = (Parent)child吗?
      

  19.   

    parent = (Child)child; 这个是亮点~
      

  20.   

    parent = (Child)child 
    红色部分是多次一举吧!!
    搞不懂
      

  21.   

    这就是JAVA多态的一种表现啊.成员方法(非静态)是动态绑定的(运行时绑定).成员变量指向的是当前引用的.如 Child.name;Parent.name;都是自己的变量,静态变量也如此
      

  22.   

    看了类定义的头我下面代码看都不用看就知道,此代码编译不通过一个.java文件下可以定义多个class,但是只有一个class可以用 public修饰这个代码编译错误
      

  23.   

    parent = (Child)child (Child) 是多次一举   试验过了  加(Child) 和不加(Child) 或者parent=(Parent)child结果都是一样的还有房主 一个.java文件中只能有一个public class 你写错了 
      

  24.   

    class test.Child 
    child 
    child 
    class test.Child 
    child
    child 
    childStaticname 
    childStaticname
      

  25.   

    不好意思改一下
     class test.Child 
    child 
    child 
    class test.Child 
    Parent 
    child 
    childStaticname 
    parentStaticnameparent = (Child)child; 
    虽然子类继承父类,但返回的是子类对象。
      

  26.   

    可以吗?
    我记得
    A a;
    B b;
    a=b;的话,编译时会提示不兼容的数据类型啊。parent = (Child)child  一个是Parent类型的,一个是Child类型的,可以这样吗?
      

  27.   

    这里的Child和Parent有继承关系,所以可以的
    就想List list = new ArrayList();一样的道理吧
    Parent parent;
    parent = (Child)child;
    应该就等价于:
    Parent parent = new Child();
      

  28.   

    Parent parent; 
    Child child; 
    child = new Child(); 
    parent = (Child)child; 先不说答案这个应该有问题,大家没有看出来吗parent = (Child)child,child是Child的对象,为什么还要转换,这不是多此一举了吗。
      

  29.   


    List是接口,不要误导人,接口的实现跟类的继承一个道理?
      

  30.   

    google with keyword static binding and dynamic binding.
      

  31.   

    clas  child
    child
    childclass child
    parent
    childchildstaticname
    parentstaticname