由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 很多有多年经验的Java程序员都会弄错的题目
相关主题
谁能深入浅出的讲讲protocol和delegate的要点?问个Winform与工作类的互动问题。
GO更像prototype based languagedelegate用中文怎么说?
问一个.NET与C++的问题ajax in VB.net
求支持:在把C++编的cmake转成C#。。一个c++ constructor的问题, thanks
iOS程序开发有前途吗?How many people use design patterns when coding?
Question about delegate in C#IOS APP 开发指南
还没被劝退C++的都来看看这个吧问个C#的问题
C# 的问题, event 这个关键字有什么用?You Don’t Know JS: this & Object Prototypes
相关话题的讨论汇总
话题: base话题: derived话题: public话题: showx2话题: java
进入Programming版参与讨论
1 (共1页)
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
10
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的看法,这是很基础的概念
如果不是我就土了
1 (共1页)
进入Programming版参与讨论
相关主题
You Don’t Know JS: this & Object PrototypesiOS程序开发有前途吗?
怎么学习python的函数里面调用函数本身(函数递归)?Question about delegate in C#
小公司面经还没被劝退C++的都来看看这个吧
对中国出口的“打癞”效应C# 的问题, event 这个关键字有什么用?
谁能深入浅出的讲讲protocol和delegate的要点?问个Winform与工作类的互动问题。
GO更像prototype based languagedelegate用中文怎么说?
问一个.NET与C++的问题ajax in VB.net
求支持:在把C++编的cmake转成C#。。一个c++ constructor的问题, thanks
相关话题的讨论汇总
话题: base话题: derived话题: public话题: showx2话题: java