由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 问一道关于java type cast的面试题
相关主题
请各位大牛来做小学题用一个C++面试题challenge一下大家
微软C++面试题请教: 想用java实现解析parse一个log文件,多谢指点
[合集] google 面试题一个新鲜twitter面经
这道设计面试题这样解对吗新鲜RocketFuels电面
问个java List的问题dropbox 面经
打车公司一题求解为什么会有很多人认识年纪大了刷不过年轻人?
被面试的人无理抱怨怎么办问道高频设计题
请教一个C++试题!Can I extend my OPT if my employer uses E-verify?
相关话题的讨论汇总
话题: animal话题: dog话题: animals话题: reflection
进入JobHunting版参与讨论
1 (共1页)
z***5
发帖数: 35
1
已知:
class Animal{}
class Dog extends Animal{
void bark();
}
需要实现下面这个函数
void makeTheDogsBark(List animals){
}
要求用java type cast实现,且不能用try catch。。。。
e***a
发帖数: 1661
2
void makeTheDogsBark(List animals){
((Dog) animals.get(0)).bark();
}
java generics is tricky
z***5
发帖数: 35
3
谢谢解答。请问如果我的animals的第一个是cat,Type cast dog会throw exception吗
?我自己在eclipse上实验了一下,好像也会有exception。
今天这个面试算是考到我的知识盲点了。

【在 e***a 的大作中提到】
: void makeTheDogsBark(List animals){
: ((Dog) animals.get(0)).bark();
: }
: java generics is tricky

b********0
发帖数: 62
4
用instanceof?这样不是dog的animal的其它子类对象可以忽略
m*****g
发帖数: 71
5
void makeTheDogsBark(List animals){
for (Animal a : animals) {
if (a instanceof Dog) {
Dog d = (Dog) a;
d.bark();
}
}
}
z***5
发帖数: 35
6
这个应该是对的,instanceof 我从来没用过,惭愧
多谢解答。

【在 m*****g 的大作中提到】
: void makeTheDogsBark(List animals){
: for (Animal a : animals) {
: if (a instanceof Dog) {
: Dog d = (Dog) a;
: d.bark();
: }
: }
: }

c**********t
发帖数: 23
7
Reflection或许也可
void makeTheDogsBark(List animals){
for(Animal animal : animals) {
if(animal.getClass() == Dog.class) {
Dog dog = (Dog) animal;
dog.bark();
}
}
}
不知道对不对,欢迎指导
w**z
发帖数: 8232
8
谁工作中写这种玩意,一脚踹死。

【在 z***5 的大作中提到】
: 已知:
: class Animal{}
: class Dog extends Animal{
: void bark();
: }
: 需要实现下面这个函数
: void makeTheDogsBark(List animals){
: }
: 要求用java type cast实现,且不能用try catch。。。。

z***5
发帖数: 35
9
对,这个也可以,我当时没想起来。

【在 c**********t 的大作中提到】
: Reflection或许也可
: void makeTheDogsBark(List animals){
: for(Animal animal : animals) {
: if(animal.getClass() == Dog.class) {
: Dog dog = (Dog) animal;
: dog.bark();
: }
: }
: }
: 不知道对不对,欢迎指导

z***5
发帖数: 35
10
忘说了,这是G家面经。

【在 w**z 的大作中提到】
: 谁工作中写这种玩意,一脚踹死。
相关主题
打车公司一题求解用一个C++面试题challenge一下大家
被面试的人无理抱怨怎么办请教: 想用java实现解析parse一个log文件,多谢指点
请教一个C++试题!一个新鲜twitter面经
进入JobHunting版参与讨论
w**z
发帖数: 8232
11
G 家用Java?

【在 z***5 的大作中提到】
: 忘说了,这是G家面经。
b**********5
发帖数: 7881
12
别搞了, 工作时, reflection, intanceOf都用吧, 你他妈的create 一个 log4j在
一个class里, 就用XXX.class...

【在 w**z 的大作中提到】
: 谁工作中写这种玩意,一脚踹死。
w**z
发帖数: 8232
13
你看懂题没有?which one is reflection here?
http://www.mkyong.com/logging/log4j-hello-world-example/
logger final static initialized once only.

【在 b**********5 的大作中提到】
: 别搞了, 工作时, reflection, intanceOf都用吧, 你他妈的create 一个 log4j在
: 一个class里, 就用XXX.class...

b**********5
发帖数: 7881
14
protected static final Logger logger = LoggerFactory.getLogger(XXX.class)
XXX.class is reflection...

【在 w**z 的大作中提到】
: 你看懂题没有?which one is reflection here?
: http://www.mkyong.com/logging/log4j-hello-world-example/
: logger final static initialized once only.

g*****g
发帖数: 34805
15
This is not reflection, it's just passing a unique identifier so later on
Class.getName() can be used.

【在 b**********5 的大作中提到】
: protected static final Logger logger = LoggerFactory.getLogger(XXX.class)
: XXX.class is reflection...

b**********5
发帖数: 7881
16
hmm.. are u saying that for all calls to XXX.class, it's not reflection?

【在 g*****g 的大作中提到】
: This is not reflection, it's just passing a unique identifier so later on
: Class.getName() can be used.

g*****g
发帖数: 34805
17
It's pointless to argue the term. I wouldn't call it reflection when no
instance for the passed class is created. The static class information is
always available in
Class API. It's more like a shortcut way to pass a fully qualified class
name in String. The point is that all these information are already
available at compile time.

【在 b**********5 的大作中提到】
: hmm.. are u saying that for all calls to XXX.class, it's not reflection?
1 (共1页)
进入JobHunting版参与讨论
相关主题
Can I extend my OPT if my employer uses E-verify?问个java List的问题
H1B pending,请问今年被拒的多么打车公司一题求解
大家一般在哪里找opening?被面试的人无理抱怨怎么办
上周五面试心得,顺求祝福!请教一个C++试题!
请各位大牛来做小学题用一个C++面试题challenge一下大家
微软C++面试题请教: 想用java实现解析parse一个log文件,多谢指点
[合集] google 面试题一个新鲜twitter面经
这道设计面试题这样解对吗新鲜RocketFuels电面
相关话题的讨论汇总
话题: animal话题: dog话题: animals话题: reflection