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 的大作中提到】 : 谁工作中写这种玩意,一脚踹死。
|
|
|
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?
|