A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 米大米 中级黑马   /  2014-4-16 21:28  /  1256 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

给定Java代码如下所示,则编译运行后,输出结果是( )。
class Parent {
  public void count() {
    System.out.println(10%3);
  }
}
public class Child  extends Parent{
  public void count() {
    System.out.println(10/3);
  }
  public static void main(String args[]) {
    Parent p = new Child();
    p.count();
  }
}



public void count() {
    System.out.println(10%3);
  }
}与下面的 public void count() {
    System.out.println(10/3);
  }
是因为构造函数被覆盖了 所以只输出的是下面的结果吗?
结果是3  是不是这个原因啊

8 个回复

倒序浏览
成员变量,静态方法看左边;非静态方法:编译看左边,运行看右边
回复 使用道具 举报
不是 这是 一个多态

     public class ExplorePhoy {

        public static void main(String []args){
                Father f = new Son();
  System.out.println(f.x);
                //f.show();
        }
}
//父类
class Father{
int x = 20;
public void show(){
                //int x = 60 ;
                System.out.println( x );
        }
}
class Son extends Father{
        int x = 5;
public  void show(){
                //int x = 90 ;
                System.out.println( x );
        }
}
非静态成员变量:
       编译时期,参考父类中的成员变量,如果有编译成功,没有编译失败
       运行时期,参考父类中的成员变量
        int x = 20;
  int x = 5;
  Father f = new Son();
  System.out.println(f.x);
   20
  
     静态成员变量:
       编译时期,参考父类中的成员变量,如果有编译成功,没有编译失败
       运行时期,参考父类中的成员变量
     Static         int x = 20;
   Static  int x = 5;
  Father f = new Son();
  System.out.println(f.x);
20
非静态成员方法:
       编译时期,参考父类中的成员方法,如果有编译成功,没有编译失败
       运行时期,运行的是子类重写后的方法,如果子类没重写,运行父类的
public void show(){
                //int x = 60 ;
                System.out.println( x );
        }

f.show();  ----->  5

     静态成员方法:
       编译时期,参考父类中的静态成员方法,如果有编译成功,没有编译失败
       运行时期,运行父类中的静态成员方法
     public static void show(){
                //int x = 60 ;
                System.out.println( x );
        }
f.show();  ----->  20


     简单的说:
       除了非静态的成员方法以外,编译看左边,运行看左边  Fu f = new Zi() ; 等于号的左边
      
        只有非静态的成员方法,编译看左边,运行看右边
       

看明白 你就知道运行原理

评分

参与人数 1技术分 +1 收起 理由
zzkang0206 + 1

查看全部评分

回复 使用道具 举报
楼主是正确的.如果方法不是覆盖了父类.那就是一直在执行父类.则不合理
回复 使用道具 举报
package com.itheima;
// 这是多态啊,  你虽然定义一个 父母对象,可以指针确实孩子,孩子对象在调用时候,首先应该调用本身自count 方法,
public class debug_18_parent {
                public void count(){
                        System.out.println(10%3);
                }

}


package com.itheima;

public class debug_18_Child extends debug_18_parent {
               
                public void count(){
                        System.out.println(10/3+":::");
                }
               
               
                public static void main(String []args)
                {
                        debug_18_parent fun1=new debug_18_Child();
                        fun1.count();
                }
}
回复 使用道具 举报
多态的问题 编译看=左边 运行看=右边 ,所以执行10/3
回复 使用道具 举报
doyxy 中级黑马 2014-4-17 00:21:50
7#
另外,这些是不是构造函数呃
回复 使用道具 举报
1. Child 类 extends Parents类时,并没有继承父类Parents的构造方法,构造方法是不能被继承的,但是在子类Child的构造方法中第一行有一个隐式的Super(),它可以调用父类中的无参构造方法,
2. Parent p = new Child(); p.count();  这个p的类型是父类Parent, 但它实际存储的是子类对象的内存地址,也就是它实际是指向堆中子类对象,
3. p.count();在编译的时候是去看父类中有没有这个方法,运行的时候是看子类中有没复写该方法,如没有复写,那执行的就是从父类继承过来count();方法
回复 使用道具 举报
Parent p = new Child();
这不是父类引用指向子类对象吗?多态的定义。
p.count();方法是非静态的,编译看左边,运行看右边,调用的是子类的方法。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马