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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Uncle.Lee 中级黑马   /  2013-11-26 21:51  /  1282 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 Uncle.Lee 于 2013-11-26 22:50 编辑

这个周末就要考java了,复习才发现,

静态和继承的问题还没完全解决

大神帮我解释下下面一个例子,运行结果为什么是 9 :

最好从本质上用内存来解释 谢谢

  1. class Father{
  2.          static int g(int n){
  3.                 return n*n;
  4.         }
  5. }

  6. class Son extends Father{
  7.         static int g(int n){
  8.                 int m=Father.g(n);
  9.                 return m+n;
  10.         }
  11. }

  12. public class Dem{
  13.         public static void main(String[] args){
  14.                
  15.                 Father f=new Son();
  16.                 System.out.println(f.g(3));        
  17.         }
  18. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
FFF + 1 淡定

查看全部评分

8 个回复

倒序浏览
  1. public class Dem{
  2.         public static void main(String[] args){
  3.                
  4.                 Father f=new Son();
  5.                 System.out.println(f.g(3));      
  6.         }
复制代码
在你的主函数中,你是以多态的形式创建对象,但静态方法不存在多态性,静态方法是属于类的,并不用创建对象就可以直接调用
而f是父类的引用,因此调用的是父类下的静态g函数


评分

参与人数 1技术分 +1 收起 理由
FFF + 1 赞一个!

查看全部评分

回复 使用道具 举报
记住一句话
多态就是
只有非静态的成员函数是编译看左边,运行看右边,
其余的,不管是成员属性,还是成员函数,左边是什么类型,就执行什么类型的方法。


这个够好记吧


关于你这个代码,同样适合这句话。。
Father f=new Son();
首先,  System.out.println(f.g(3));
这个g()函数式静态的,等号左边是Father类型的
那么就执行父类的方法

记住这句话,搞定多态都不怕

评分

参与人数 1技术分 +1 收起 理由
狼王 + 1 不错哦,亲,谢谢哈。。。

查看全部评分

回复 使用道具 举报 2 0
FFF 金牌黑马 2013-11-26 22:44:14
板凳
ysunday 发表于 2013-11-26 22:09
记住一句话
多态就是
只有非静态的成员函数是编译看左边,运行看右边,

学习了!很感谢!
回复 使用道具 举报
ysunday 发表于 2013-11-26 22:09
记住一句话
多态就是
只有非静态的成员函数是编译看左边,运行看右边,

记住是可以的,但是我想从本质上理解,,

回复 使用道具 举报
quan23355 发表于 2013-11-26 22:03
在你的主函数中,你是以多态的形式创建对象,但静态方法不存在多态性,静态方法是属于类的,并不用创建对象 ...

谢谢 ,我想从内存上理解
回复 使用道具 举报
非静态方法执行的时候好像是内存重定向到子类的方法上,这个,就这点印象了
回复 使用道具 举报
  1. package cn.itcast.duotai;

  2. public class Test{
  3.         public static void main(String[] args) {
  4.                 Father f = new Son();
  5.                 int x = f.counter(3);  //非静态方法,编译看左边,运行看右边
  6.                 System.out.println(x);
  7.                
  8.                 int y = f.counter(3,3);
  9.                 System.out.println(y);        //静态方法,编译和运行都看左边
  10.                
  11.                 String s = ((Son) f).sayHello();
  12.                 System.out.println(s);        //若是要用子类自己的方法,的强转成子类
  13.         }
  14. }

  15. class Father {
  16.        
  17.         public int counter(int n){
  18.                 return n*n;
  19.                
  20.         }
  21.         public static int counter(int m,int n){
  22.                 return m*n;
  23.                
  24.         }

  25. }

  26. class Son extends Father{
  27.         @Override
  28.         public int counter(int n) {
  29.                
  30.                 return n*n+1;
  31.         }
  32.        
  33.         public static int counter(int m,int n){
  34.                 return m*n+1;
  35.                
  36.         }
  37.        
  38.         public String sayHello(){
  39.                 return "Hello,World!!!";
  40.         }
  41. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
狼王 + 1 继续努力哈。。。

查看全部评分

回复 使用道具 举报
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马