由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 一个关于generics的问题
相关主题
Apply lock on a class.今天下午要面一个老印
thread safe Singleton 的几种方法?Singleton vs. static class?
发现 synchronized 的一个问题三论abstract class
问一个关于access object instance的问题synchronized method does lock the object that passed into the method as a parameter?
synchronization 锁住了什么?CC150 16.6答案是不是有问题? (转载)
question: how to implement thisConverge of languages and design pattern
static getInstance()synchronization for counters
看到一个关于singleton的面试题Talk a little more about How to lock a file
相关话题的讨论汇总
话题: class话题: static话题: singleton话题: object话题: map
进入Java版参与讨论
1 (共1页)
q**q
发帖数: 266
1
careercup上有这样一道题:implement a singleton design pattern as a template
such that for any given class Foo, you can call Singleton::instance() and
get a pointer to an instance of a singleton of type Foo
答案是用c++给出的。我用java写出下面的代码,但显然不对,请问上面的问题应该怎
么用java实现呢?
另外对下面不对的代码我也有一些疑问:
public class SingleTon {
private static T t; //报语法错误“annot make a static reference to the
non-static type T”,请问这是什么缘故?
public synchronized static T instance() {
if( t == null ) {
t = new T();//即使t非static,此处也无法instantiate,我想是因为很多
class没有default constructor的原因,这样理解是否对?
}
return t;
}
}
谢谢!
g*****g
发帖数: 34805
2
Since Java uses type erasure, generics is a compile time trick.
If it can't decide the type in compile time, it can't do instantiation.

template
the

【在 q**q 的大作中提到】
: careercup上有这样一道题:implement a singleton design pattern as a template
: such that for any given class Foo, you can call Singleton::instance() and
: get a pointer to an instance of a singleton of type Foo
: 答案是用c++给出的。我用java写出下面的代码,但显然不对,请问上面的问题应该怎
: 么用java实现呢?
: 另外对下面不对的代码我也有一些疑问:
: public class SingleTon {
: private static T t; //报语法错误“annot make a static reference to the
: non-static type T”,请问这是什么缘故?
: public synchronized static T instance() {

q**q
发帖数: 266
3
明白。那么对于上面描述的问题,能用java实现吗?

【在 g*****g 的大作中提到】
: Since Java uses type erasure, generics is a compile time trick.
: If it can't decide the type in compile time, it can't do instantiation.
:
: template
: the

m****r
发帖数: 6639
4
since you need to return any class, what you need is a map from Class to
Object.
private static Map, or Map, i am not sure.
and use reflection to instantiate the object.

template
the

【在 q**q 的大作中提到】
: careercup上有这样一道题:implement a singleton design pattern as a template
: such that for any given class Foo, you can call Singleton::instance() and
: get a pointer to an instance of a singleton of type Foo
: 答案是用c++给出的。我用java写出下面的代码,但显然不对,请问上面的问题应该怎
: 么用java实现呢?
: 另外对下面不对的代码我也有一些疑问:
: public class SingleTon {
: private static T t; //报语法错误“annot make a static reference to the
: non-static type T”,请问这是什么缘故?
: public synchronized static T instance() {

r*****l
发帖数: 2859
5
public class Singleton {
private static final Map, Object> map = new HashMap,
Object>();
public synchronized static T instance(Class c)
throws InstantiationException, IllegalAccessException {
if (!map.containsKey(c)) {
T o = c.newInstance();
map.put(c, o);
}
return (T) map.get(c);
}
}

template
the

【在 q**q 的大作中提到】
: careercup上有这样一道题:implement a singleton design pattern as a template
: such that for any given class Foo, you can call Singleton::instance() and
: get a pointer to an instance of a singleton of type Foo
: 答案是用c++给出的。我用java写出下面的代码,但显然不对,请问上面的问题应该怎
: 么用java实现呢?
: 另外对下面不对的代码我也有一些疑问:
: public class SingleTon {
: private static T t; //报语法错误“annot make a static reference to the
: non-static type T”,请问这是什么缘故?
: public synchronized static T instance() {

q**q
发帖数: 266
6
Thank you! I got it.

【在 r*****l 的大作中提到】
: public class Singleton {
: private static final Map, Object> map = new HashMap,
: Object>();
: public synchronized static T instance(Class c)
: throws InstantiationException, IllegalAccessException {
: if (!map.containsKey(c)) {
: T o = c.newInstance();
: map.put(c, o);
: }
: return (T) map.get(c);

q**q
发帖数: 266
7
Thank you!

【在 m****r 的大作中提到】
: since you need to return any class, what you need is a map from Class to
: Object.
: private static Map, or Map, i am not sure.
: and use reflection to instantiate the object.
:
: template
: the

r*****l
发帖数: 2859
8
懒惰,只给说明,不写code。

【在 m****r 的大作中提到】
: since you need to return any class, what you need is a map from Class to
: Object.
: private static Map, or Map, i am not sure.
: and use reflection to instantiate the object.
:
: template
: the

m****r
发帖数: 6639
9
我刚起床. 还不在状态.

【在 r*****l 的大作中提到】
: 懒惰,只给说明,不写code。
S****h
发帖数: 115
10
赞~

【在 r*****l 的大作中提到】
: public class Singleton {
: private static final Map, Object> map = new HashMap,
: Object>();
: public synchronized static T instance(Class c)
: throws InstantiationException, IllegalAccessException {
: if (!map.containsKey(c)) {
: T o = c.newInstance();
: map.put(c, o);
: }
: return (T) map.get(c);

1 (共1页)
进入Java版参与讨论
相关主题
Talk a little more about How to lock a filesynchronization 锁住了什么?
how to do this?question: how to implement this
basic java questionstatic getInstance()
请问有没有generic的array看到一个关于singleton的面试题
Apply lock on a class.今天下午要面一个老印
thread safe Singleton 的几种方法?Singleton vs. static class?
发现 synchronized 的一个问题三论abstract class
问一个关于access object instance的问题synchronized method does lock the object that passed into the method as a parameter?
相关话题的讨论汇总
话题: class话题: static话题: singleton话题: object话题: map