Redian新闻
>
2012Fall求offer比较,美国chem VS 加国ChemE
avatar
2012Fall求offer比较,美国chem VS 加国ChemE# ChemEng - 化学工程
m*j
1
ClassB1,B2,...,Bn都是ClassA的subclass,现在有某个ClassBi的一个instanceB,
但是我取到它的时候只知道它是A,不知道他具体是哪个B。
e.g.我拿到一个苹果(instanceB),但是现在只知道这是个水果类(ClassA)而不知道
到底是什么水果。我的目的是把instaceB cast成苹果类。
我现在能用reflection取得instanceB的class以及class name,可是不知道怎么才能用
这个信息将instanceB cast成他自己的真实的subclass。因为只有cast后才能用B的某
些方法?我试了如下两种方法都不work。根据(http://
stackoverflow.com/questions/2127318/java-how-can-i-do-dynamic-casting-of-a-v
ariable-from-one-type-to-another),似乎只能将ClassB1...ClassBn一个一个用ins
tanceof试一遍?
多谢!
code:
ClassA instanceB = xxx.getInstance(yyy); // this is third party API
Class classB = instanceB.getClass(); // this returns the correct subclass of
instanceB
// method 1
instanceB = (classB)instanceB; // this does NOT work
// method 2
Object o = classB.cast(instanceB); // this works, but since the returned is
an Object, I still canNOT use classB methods
avatar
d*e
2
Boeing reported positive initial results Sunday from a test in which
engineers bent the wings of a 787 Dreamliner ground test airplane until the
load was more than one-and-a-half times anything the jet will experience in
service.
Seattle Times business
Boeing Completes Ultimate-Load Wing Test on 787 K64913
In a carefully planned extreme test inside Boeing's Everett plant Sunday,
engineers bent the wings of a 787 Dreamliner ground-test airplane until the
load was more than one-and-a-half times an
avatar
f*y
3
高分子方向。目前有两个offer,均为PhD。
Carnegie Mellon化学offer,导师未定
Queen's的化工offer,导师做聚合反应工程。
未来规划是在北美工作几年后再考虑是否回国发展。借此宝地,求大家的建议,哪个
program更好呢?都说化学不好找工作,但我感觉很多化工组里也干着挺纯化学的
research。这俩offer的工作前景各如何呢?
avatar
N*m
4
没太看懂,估摸着写吧
suppose class Bar implemnts Foo, and in bar, we have a method called "run()"
, then
final Foo f = new Bar();
for (final Method m : f.getClass().getDeclaredMethods()) {
if (m.getName().equals("run")) {
m.invoke(f, null);
}
}
Is this what you want?
avatar
r*e
5
博士和专业没关系,完全和老板相关。盯着老板就好

【在 f***y 的大作中提到】
: 高分子方向。目前有两个offer,均为PhD。
: Carnegie Mellon化学offer,导师未定
: Queen's的化工offer,导师做聚合反应工程。
: 未来规划是在北美工作几年后再考虑是否回国发展。借此宝地,求大家的建议,哪个
: program更好呢?都说化学不好找工作,但我感觉很多化工组里也干着挺纯化学的
: research。这俩offer的工作前景各如何呢?

avatar
m*j
6
多谢。不过似乎不是我想要的。我又修改了一下原帖,麻烦再看看?
把你代码改成Bar extends Foo就差不多了。我现在只知道某个object是Foo,需要把它
cast成Bar,不知道该怎么cast。

)"

【在 N***m 的大作中提到】
: 没太看懂,估摸着写吧
: suppose class Bar implemnts Foo, and in bar, we have a method called "run()"
: , then
: final Foo f = new Bar();
: for (final Method m : f.getClass().getDeclaredMethods()) {
: if (m.getName().equals("run")) {
: m.invoke(f, null);
: }
: }
: Is this what you want?

avatar
z*u
7
别读化学
不过你可以考虑到cmu读phd
顺便拿一下cs的master 很好找工作的
avatar
N*m
8
你原文里面写的
"因为只有cast后才能用B的某些方法?"
如果只是运行某个指定的方法,给的方法已经可以了.
不明白你为什么还要cast呢?还有什么其它原因没说?

【在 m*****j 的大作中提到】
: 多谢。不过似乎不是我想要的。我又修改了一下原帖,麻烦再看看?
: 把你代码改成Bar extends Foo就差不多了。我现在只知道某个object是Foo,需要把它
: cast成Bar,不知道该怎么cast。
:
: )"

avatar
m*j
9
哦哦 明白了 这样不用cast也可以调用ClassB的方法了
我光想着cast完了用instanceB.method()了
刚才试了已经成功了
多谢多谢!
不过话说回来,这样的cast的确是不可能的吗?

【在 N***m 的大作中提到】
: 你原文里面写的
: "因为只有cast后才能用B的某些方法?"
: 如果只是运行某个指定的方法,给的方法已经可以了.
: 不明白你为什么还要cast呢?还有什么其它原因没说?

avatar
r*s
10
是的,
你原先的想法就是有违OO以及reflection精神的。

【在 m*****j 的大作中提到】
: 哦哦 明白了 这样不用cast也可以调用ClassB的方法了
: 我光想着cast完了用instanceB.method()了
: 刚才试了已经成功了
: 多谢多谢!
: 不过话说回来,这样的cast的确是不可能的吗?

avatar
N*m
11
可不可能我不知道,但是显然没必要啊.
设想你可以cast,不妨就是你原先写的那样,
object o = f.cast() ....
或者
Bar b = f.cast() ....
这里,情形1 :你还得用object这个变量来hold f,得不偿失,
scope变大了,还不如原先的定义;
情形2,你都知道Bar类型了,直接转换不就得了?

【在 m*****j 的大作中提到】
: 哦哦 明白了 这样不用cast也可以调用ClassB的方法了
: 我光想着cast完了用instanceB.method()了
: 刚才试了已经成功了
: 多谢多谢!
: 不过话说回来,这样的cast的确是不可能的吗?

avatar
m*j
12
哦?看来该补补OO的课了。。。
能不能展开讲讲具体怎么个违背法?

【在 r*****s 的大作中提到】
: 是的,
: 你原先的想法就是有违OO以及reflection精神的。

avatar
B*g
13
co-ask,红芽开奖做吧

【在 m*****j 的大作中提到】
: 哦?看来该补补OO的课了。。。
: 能不能展开讲讲具体怎么个违背法?

avatar
m*j
14
情形1就是我原帖里的method2,的确有这样的问题
情形2的话,问题就是我不知道具体是哪个Bar类,有可能是Bar1,Bar2,...BarN,所以
我原来想的理想状态是取得instanceB的class之后让他自己cast到自己的Bar去,就像这
样:
instanceB = (instanceB.getClass())instanceB
当然,这是不work的

【在 N***m 的大作中提到】
: 可不可能我不知道,但是显然没必要啊.
: 设想你可以cast,不妨就是你原先写的那样,
: object o = f.cast() ....
: 或者
: Bar b = f.cast() ....
: 这里,情形1 :你还得用object这个变量来hold f,得不偿失,
: scope变大了,还不如原先的定义;
: 情形2,你都知道Bar类型了,直接转换不就得了?

avatar
N*m
15
"我原来想的理想状态是取得instanceB的class之后让他自己cast到自己的Bar去"
你的意思是如下类似的转换(假定这样的cast存在)?
String actualClassName = f.getClass().getName(); // "Bar" here
f = cast(f,actualClassName );
//
问题是f总归是Foo类型的...
所以你只能用另外一个Bar类型的变量来hold返回值.
可是正如前文所说的,这岂不是多此一举...

所以
像这

【在 m*****j 的大作中提到】
: 情形1就是我原帖里的method2,的确有这样的问题
: 情形2的话,问题就是我不知道具体是哪个Bar类,有可能是Bar1,Bar2,...BarN,所以
: 我原来想的理想状态是取得instanceB的class之后让他自己cast到自己的Bar去,就像这
: 样:
: instanceB = (instanceB.getClass())instanceB
: 当然,这是不work的

avatar
m*j
16
我原来想的是还用这个变量。。。只是让他从Foo类变成Bar类。。。
我估计这就是redbuds说的不符合OO原则的地方吧?

【在 N***m 的大作中提到】
: "我原来想的理想状态是取得instanceB的class之后让他自己cast到自己的Bar去"
: 你的意思是如下类似的转换(假定这样的cast存在)?
: String actualClassName = f.getClass().getName(); // "Bar" here
: f = cast(f,actualClassName );
: //
: 问题是f总归是Foo类型的...
: 所以你只能用另外一个Bar类型的变量来hold返回值.
: 可是正如前文所说的,这岂不是多此一举...
:
: 所以

avatar
g*g
17
你要从头想,为啥要这个变量,不就是为了调用某个方法吗?
假设这个方法叫做blah。如果Foo可以blah,那blah就应该
在Foo里声明,没有这个问题。如果Foo不能blah,Bar可以,
当初接口为啥要传Foo进来呢,传Bar不好吗?
这不是反射能不能实现的问题,但凡碰到类似的地方,先考虑
重构。除非是第三方代码,没法动。

【在 m*****j 的大作中提到】
: 我原来想的是还用这个变量。。。只是让他从Foo类变成Bar类。。。
: 我估计这就是redbuds说的不符合OO原则的地方吧?

avatar
m*j
18
嗯 多谢了
那的确是个第三方的API 直接返回的是一个代表所有建筑构件的类
用户具体选择可能是门啊窗啊啥的,都是其子类,各自又有不同的方法
so。。。

【在 g*****g 的大作中提到】
: 你要从头想,为啥要这个变量,不就是为了调用某个方法吗?
: 假设这个方法叫做blah。如果Foo可以blah,那blah就应该
: 在Foo里声明,没有这个问题。如果Foo不能blah,Bar可以,
: 当初接口为啥要传Foo进来呢,传Bar不好吗?
: 这不是反射能不能实现的问题,但凡碰到类似的地方,先考虑
: 重构。除非是第三方代码,没法动。

avatar
r*l
19
You need to know ClassB to call its method.
If you don't use generics, do something like this:
if (instanceB instanceof ClassB) {
// call the method
}
If you use generics, change your getInstance() to:
T getInstance(Class, ...).
Don't see the reason why you want to dynamically cast.

知道
-v
ins

【在 m*****j 的大作中提到】
: ClassB1,B2,...,Bn都是ClassA的subclass,现在有某个ClassBi的一个instanceB,
: 但是我取到它的时候只知道它是A,不知道他具体是哪个B。
: e.g.我拿到一个苹果(instanceB),但是现在只知道这是个水果类(ClassA)而不知道
: 到底是什么水果。我的目的是把instaceB cast成苹果类。
: 我现在能用reflection取得instanceB的class以及class name,可是不知道怎么才能用
: 这个信息将instanceB cast成他自己的真实的subclass。因为只有cast后才能用B的某
: 些方法?我试了如下两种方法都不work。根据(http://
: stackoverflow.com/questions/2127318/java-how-can-i-do-dynamic-casting-of-a-v
: ariable-from-one-type-to-another),似乎只能将ClassB1...ClassBn一个一个用ins
: tanceof试一遍?

avatar
m*j
20
thanks~

【在 r*****l 的大作中提到】
: You need to know ClassB to call its method.
: If you don't use generics, do something like this:
: if (instanceB instanceof ClassB) {
: // call the method
: }
: If you use generics, change your getInstance() to:
: T getInstance(Class, ...).
: Don't see the reason why you want to dynamically cast.
:
: 知道

avatar
d*3
21
Define virtual function GetType in Class A and overwrite it in all the
subclasses. You will not meet this issue.

知道
-v
ins

【在 m*****j 的大作中提到】
: ClassB1,B2,...,Bn都是ClassA的subclass,现在有某个ClassBi的一个instanceB,
: 但是我取到它的时候只知道它是A,不知道他具体是哪个B。
: e.g.我拿到一个苹果(instanceB),但是现在只知道这是个水果类(ClassA)而不知道
: 到底是什么水果。我的目的是把instaceB cast成苹果类。
: 我现在能用reflection取得instanceB的class以及class name,可是不知道怎么才能用
: 这个信息将instanceB cast成他自己的真实的subclass。因为只有cast后才能用B的某
: 些方法?我试了如下两种方法都不work。根据(http://
: stackoverflow.com/questions/2127318/java-how-can-i-do-dynamic-casting-of-a-v
: ariable-from-one-type-to-another),似乎只能将ClassB1...ClassBn一个一个用ins
: tanceof试一遍?

avatar
m*j
22
多谢~ class定义是第三方的自己没法改。。。
不过不是太明白,getType返回什么?自己的class吗?那和getClass()不是一样了吗?

【在 d******3 的大作中提到】
: Define virtual function GetType in Class A and overwrite it in all the
: subclasses. You will not meet this issue.
:
: 知道
: -v
: ins

avatar
r*l
23
co 不是太明白

【在 m*****j 的大作中提到】
: 多谢~ class定义是第三方的自己没法改。。。
: 不过不是太明白,getType返回什么?自己的class吗?那和getClass()不是一样了吗?

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。