您的位置:首页 > 编程语言 > Java开发

java几个容易混淆的问题

2009-10-10 00:27 609 查看
java几个容易混淆的问题 收藏
初学者甚至老手都容易混淆的问题
1.关于继承
继承有几个比较典型的问题:
a.子类可以继承父类的一切属性和方法。父类中声明为private的method或者field,创建子类的对象之后,父类中定义的field都会在子类的对象中占据空间,也就是分配内存。只是有的属性子类可以直接访问,有的必须通过父类提供的public或者protected方法来访问。private只是声明可见性,你看不到不等于不存在。简单的例子:
b.方法可以覆盖,但是field不可以。Fields cannot be overridden; they can only be hidden.
如果子类中定义了和父类同名的属性,父类中定义的属性可以通过super来访问。
c.private的方法不能被覆盖,但是你可以在子类中重新定义一个和父类中的方法签名相同的方法,这不是覆盖,因为子类是看不到父类中的private方法的。
d.Static members within a class whether fields or methods cannot be overridden, they are always hidden.
2.关于方法调用时的值传递
这个问题已经争论了好多次了,简单的引用一下书里的说法。
Core Java:The term call by value means that the method gets just the value that the caller provides. In contrast, call by reference means that the method gets the location of the variable that the caller provides. Thus, a method can modify the value stored in a variable that is passed by reference but not in one that is passed by value. The Java programming language always uses call by value. That means that the method gets a copy of all parameter values.
The Java Programming Language:All parameters to methods are passed "by value." In other words, values of parameter variables in a method are copies of the values the invoker specified as arguments.You should note that when the parameter is an object reference, it is the object reference not the object itselfthat is passed "by value."
The Java Tutorials:Primitive arguments, such as an int or a double, are passed into methods by value.Reference data type parameters, such as objects, are also passed into methods by value.
sun的很多认证材料上的说法也是pass by value.
大家可以在google中搜一下java ,pass by reference,看看结果
很多人坚持pass by reference的说法,一直也不知道这个术语是谁提出来的。希望专家指正来源。
3.关于重载和多态
多态是面向对象的特性之一,理解也不是特别困难,但是最近发现很多人喜欢把多态和覆盖(Overriding,暂且这么翻译吧)搅在一起。其实自己刚开始学的时候也曾经有过困惑,什么叫做重载(Overloading),什么叫做覆盖(Overriding),后来看书多了也就慢慢理解了,大体的总结一下:

重载是方法之间的一种关系,多个方法使用相同的方法名,但是签名不同,所以我们可以通过签名来区分。早期的java中因为没有采用模板机制,所以大量的方法都是采用重载的方法写成的,比如最经典的System.out.println();在代码里有十种实现方法,接受各种不同类型的参数。这是 Overloading。
覆盖是类层次结构中的一种关系,比如父类有某个方法,子类中同样实现一个同名并且签名相同的方法,这种情况我们称之为overriding。注意,这里要求签名相同。但是返回值可以有些许不同,这里的不同指的是子类中重写的方法可以返回父类中返回类型的子类型,说起来很绕,简单的举个例子,如果父类中方法的返回类型是Number类型,那么子类中的这个覆盖方法的返回类型可以是Number类型,也可以是其子类型,比如Integer,Double等等。
这样两个概念的区别已经很明了了,而且也应该知道,覆盖是实现多态的关键技术。
两个概念还有个重要的区别是,重载是静态绑定的,因为编译的时候编译器就可以根据签名确定具体要调用的方法,而覆盖是运行时绑定的,因为运行的时候才能根据运行时类型信息,确定具体要调用哪个类中定义的覆盖方法。这里面其实有方法表的概念,具体可以参考Core Java.
最后,引经据典,配合一下自己的说法。
《The Java Programming Language》中:

Overloading a method is what you have already learned: providing more than one method with the same name but with different signatures to distinguish them.
Overriding a method means replacing the superclass's implementation of a method with one of your own. The signatures must be identicalbut the return type can vary in a particular way, as discussed below.

发表于 @ 2008年07月24日 12:47:00 |

读书笔记:String的+操作 收藏
题目:判断输出

System.out.println("H"+"a");
System.out.println('H'+'a');
输出:
Ha
169
注意: 第一句中的+是字符串连接操作符,而第二个中的+是数学运算加法.

书上的总结:
使用字符串连接操作符要格外小心.当且仅当+操作符的操作数中至少有一个是String类型时,才会执行字符串连接操作;否则,执行加法.如果要连接的数值没有一个是字符串类型的,那么可以有几种选择:prepend the empty string; convert the first value to a string explicitly, using String.valueOf; use a string buffer; or if you are using release 5.0, use the printf facility.

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ZangXT/archive/2008/11/02/3206530.aspx

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ZangXT/archive/2008/07/24/2704020.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: