Y**G 发帖数: 1089 | 1 目测结果。很奇怪,我碰到不少5年经验的Java程序员都会做错。
public class Base {
public int x = 1;
public void showX1() {
System.out.printf("%dn", x);
}
public void showX2() {
System.out.printf("%dn", x);
}
}
public class Derived extends Base {
public int x = 3;
@Override public void showX2() {
System.out.printf("%dn", x);
}
}
public class Main {
public static void main(String[] args) {
Derived a = new Derived();
a.showX1();
a.showX2();
Base b = (Base)a;
b.showX1();
b.showX2();
}
} |
d****i 发帖数: 4809 | 2 The principle of polymorphism is the same. Consider the following equivalent
C++ code, it yields the same results. You can even better change to: Base *
a = new Derived(); and the results are the same.
#include
using namespace std;
class Base {
private:
int x;
public:
Base() : x(1) {}
virtual ~Base() {};
void showX1() {
cout<
}
virtual void showX2() {
cout<
}
};
class Derived : public Base {
private:
int x;
public:
Derived() : x(3) {}
void showX2() {
cout<
}
};
int main(int argc, char* argv[])
{
Derived *a = new Derived();
a->showX1();
a->showX2();
Base *b = (Base*) a;
b->showX1();
b->showX2();
delete a;
} |
k**********g 发帖数: 989 | 3 容易错,证明override不是好东西,就是不能用。 快找goodbug来讨个说法。 |
N******K 发帖数: 10202 | 4 override是有些问题
比如你用某个开源软件 override一个函数
过几个月 此开源软件升级 你得检查所有override是不是正确
【在 k**********g 的大作中提到】 : 容易错,证明override不是好东西,就是不能用。 快找goodbug来讨个说法。
|
w**z 发帖数: 8232 | 5 很多人更喜欢delegation
【在 k**********g 的大作中提到】 : 容易错,证明override不是好东西,就是不能用。 快找goodbug来讨个说法。
|
Y**G 发帖数: 1089 | 6 delegation 要写很多 boilerplate code,继承只要写需要改动的 code.
不过继承是紧耦合,delegate 或 composite 是松耦合
【在 w**z 的大作中提到】 : 很多人更喜欢delegation
|
F*M 发帖数: 104 | 7 没什么奇怪的,基本功不扎实而已。
【在 Y**G 的大作中提到】 : 目测结果。很奇怪,我碰到不少5年经验的Java程序员都会做错。 : public class Base { : public int x = 1; : public void showX1() { : System.out.printf("%dn", x); : } : public void showX2() { : System.out.printf("%dn", x); : } : }
|
s******e 发帖数: 114 | 8 x没被override吧?只是hide/shadow?
还真想不起java如何override x, c#倒是可以override property. |
a*******d 发帖数: 72 | 9 1 3 1 3 ba?
【在 Y**G 的大作中提到】 : 目测结果。很奇怪,我碰到不少5年经验的Java程序员都会做错。 : public class Base { : public int x = 1; : public void showX1() { : System.out.printf("%dn", x); : } : public void showX2() { : System.out.printf("%dn", x); : } : }
|
Y**G 发帖数: 1089 | |
k**********g 发帖数: 989 | 11
This is why when universities only teach a single language to students,
students will not understand the subtle differences between those "abstract
concepts".
A simple saying is that all Java methods are virtual. (To be exact, it
should be "non-static, non-final, non-private Java methods" http://stackoverflow.com/questions/12752343/are-all-method-in-java-implictly-virtual )
However, not actually having seen a language that is not "all-virtual", it
is hard to understand what "virtual" means in the first place.
【在 Y**G 的大作中提到】 : 对
|
d****i 发帖数: 4809 | 12 Java is derived from C++, it doesn't use "virtual" keyword although all
methods are implicitly virtual in Java.
abstract
【在 k**********g 的大作中提到】 : : This is why when universities only teach a single language to students, : students will not understand the subtle differences between those "abstract : concepts". : A simple saying is that all Java methods are virtual. (To be exact, it : should be "non-static, non-final, non-private Java methods" http://stackoverflow.com/questions/12752343/are-all-method-in-java-implictly-virtual ) : However, not actually having seen a language that is not "all-virtual", it : is hard to understand what "virtual" means in the first place.
|
j********x 发帖数: 2330 | 13 答案是1 3 1 3的话我不同意lz的看法,这是很基础的概念
如果不是我就土了 |
j********x 发帖数: 2330 | 14 答案是1 3 1 3的话我不同意lz的看法,这是很基础的概念
如果不是我就土了 |