Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:关于继承的深入讨论 [Re:jameszhang]
ccikk





发贴: 2
积分: 0
于 2005-08-31 11:14 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我觉得楼上的朋友用C++对比来说明很能说明问题,我也试了一下:
#include "stdio.h"

class TestA {
public:
  int m;

  TestA() {
    m = 1;
  }

  virtual int getM() {
    return m;
  }
};

class TestB : public TestA {
  
public:
  int m;

  TestB() {
    m = 2;
  }

  int getM() {
    return m;
  }
};

void main() {
  
  TestA *a = new TestB();
  
  printf("%d\r\n", a->m);
  printf("%d\r\n", a->getM());
}

打印结果:
1
2

如果把TestA中getM的virtual修饰符号去掉,
打印结果:
1
1

这样我们就明白了,java的默认方式是lata binding(也叫动态帮顶),而C++默认的是静态绑定,除非使用了virtual修饰符。
还有,无论c++还是java,对成员变量都不能动态绑定的。




话题树型展开
人气 标题 作者 字数 发贴时间
15765 关于继承的深入讨论 jameszhang 402 2005-08-11 19:24
14198 Re:关于继承的深入讨论 truthawp 54 2005-08-11 23:22
14074 Re:关于继承的深入讨论 jameszhang 26 2005-08-20 10:09
14016 Re:关于继承的深入讨论 jameszhang 178 2005-08-28 14:29
13981 加精 ww1ww1 30 2005-08-28 23:13
14212 Re:关于继承的深入讨论 hxz5830 304 2005-08-29 15:03
13940 Re:关于继承的深入讨论 ww1ww1 369 2005-08-29 23:02
13947 Re:关于继承的深入讨论 jameszhang 48 2005-08-29 21:21
13991 Re:关于继承的深入讨论 ccikk 692 2005-08-31 10:53
13838 Re:关于继承的深入讨论 ccikk 552 2005-08-31 11:14
16437 Re:关于继承的深入讨论 zcjl 58 2005-09-05 21:14
14606 Re:关于继承的深入讨论 hxz5830 127 2005-08-15 18:38
14264 Re:关于继承的深入讨论 ww1ww1 11 2005-08-15 20:35
14229 Re:关于继承的深入讨论 jigsaw 13 2005-08-15 22:12
14325 Re:关于继承的深入讨论 whyuaou 128 2005-08-16 09:17
14146 Re:关于继承的深入讨论 jameszhang 216 2005-08-16 19:25
14178 Re:关于继承的深入讨论 whyuaou 161 2005-08-17 14:12
14298 Re:关于继承的深入讨论 ww1ww1 1399 2005-08-16 10:03
14201 Re:关于继承的深入讨论 ww1ww1 253 2005-08-16 20:38
14201 Re:关于继承的深入讨论 ww1ww1 273 2005-08-17 17:20
14145 Re:关于继承的深入讨论 breezehou 52 2005-08-17 12:58
14004 Re:关于继承的深入讨论 jameszhang 137 2005-08-17 21:09
14238 Re:关于继承的深入讨论 jokehan 289 2005-08-17 15:56
14084 Re:关于继承的深入讨论 jameszhang 1629 2005-08-17 21:41

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923