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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 风飘逸 中级黑马   /  2015-3-26 18:46  /  1165 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

在继承过程中如果父类中有一个变量int  num,如果子类继承了该父类,那么子类中也有一个变量int num,那么Fu fu = new Zi();fu.num调用的是哪一个啊?

3 个回复

倒序浏览
编译看左边,运行看右边.
回复 使用道具 举报
父类的变量num,自己手动写一个很快就测出来了。
回复 使用道具 举报
看代码运行的结果。
  1. package com.itheima.test2;

  2. class Fu {
  3.        
  4.         int num= 3;
  5.        
  6.        
  7. }

  8. class Zi extends Fu{
  9.        
  10.         int num= 4;
  11. }


  12. public class ExtendsDemo {

  13.         public static void main(String[] args) {
  14.                 // TODO Auto-generated method stub
  15.             Fu fu = new Zi();
  16.             
  17.             System.out.println(fu.num);//3
  18.             
  19.             
  20.         }

  21. }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马