java针对泛型创建对象的中存在擦除的弥补方案
2012-02-15 21:48
344 查看
在java中不能像才c++那样,直接声明泛型对象即使 T t=new T(); 但是java中针对这种问题也有一些解决方案,在这里提供三种方案
/**
*
* although in c++ could use new T() to creating a instance of type, but java not support this approach, this class
* provider 3 solutions for this situation
* @version $Revision: $ $Name: $
*/
public class CreatingTypesSolutions {
public static void main(String[] args) {
// solution 1: pass in a factory object, and use that to make the new instance
ClassAsFactory<Employ> caf1 = new ClassAsFactory<Employ>(Employ.class);
/*
* This compiles, but fails with ClassAsFactory<Integer> because Integer has no default constructor. Because the
* error is not caught at compile time, this approach is frowned upon by the Sun folks. They suggest instead
* that you use solution2
*/
// ClassAsFactory<Integer> caf2 = new ClassAsFactory<Integer>(Integer.class);
// solution 2:
ClassAsFactory2<Employ> caf21 = new ClassAsFactory2<Employ>(new EmployFactory());
ClassAsFactory2<Integer> caf22 = new ClassAsFactory2<Integer>(new IntegerFactory());
// solution 3: Template Method design pattern
IntegerGeneric ig1=new IntegerGeneric();
System.out.println(ig1.create());
}
}
class Employ {
}
class ClassAsFactory<T> {
public ClassAsFactory(Class<T> t) {
try {
T tObj = t.newInstance();
System.out.println(tObj.getClass().getSimpleName() + " object!");
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
// solution 2 :intgerface
interface FactoryI<T> {
T create();
}
class ClassAsFactory2<T> {
public <F extends FactoryI<T>> ClassAsFactory2(F factory) {
System.out.println(factory.create().getClass().getSimpleName() + " create successful");
}
}
class EmployFactory implements FactoryI<Employ> {
public Employ create() {
return new Employ();
}
}
class IntegerFactory implements FactoryI<Integer> {
public Integer create() {
return new Integer(0);
}
}
// solution 3: Template Method design pattern
abstract class GenericWithCreate<T> {
final T element;
GenericWithCreate() {
element = create();
}
abstract T create();
}
class IntegerGeneric extends GenericWithCreate<Integer> {
@Override
Integer create() {
return new Integer("4");
}
}
/**
*
* although in c++ could use new T() to creating a instance of type, but java not support this approach, this class
* provider 3 solutions for this situation
* @version $Revision: $ $Name: $
*/
public class CreatingTypesSolutions {
public static void main(String[] args) {
// solution 1: pass in a factory object, and use that to make the new instance
ClassAsFactory<Employ> caf1 = new ClassAsFactory<Employ>(Employ.class);
/*
* This compiles, but fails with ClassAsFactory<Integer> because Integer has no default constructor. Because the
* error is not caught at compile time, this approach is frowned upon by the Sun folks. They suggest instead
* that you use solution2
*/
// ClassAsFactory<Integer> caf2 = new ClassAsFactory<Integer>(Integer.class);
// solution 2:
ClassAsFactory2<Employ> caf21 = new ClassAsFactory2<Employ>(new EmployFactory());
ClassAsFactory2<Integer> caf22 = new ClassAsFactory2<Integer>(new IntegerFactory());
// solution 3: Template Method design pattern
IntegerGeneric ig1=new IntegerGeneric();
System.out.println(ig1.create());
}
}
class Employ {
}
class ClassAsFactory<T> {
public ClassAsFactory(Class<T> t) {
try {
T tObj = t.newInstance();
System.out.println(tObj.getClass().getSimpleName() + " object!");
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
// solution 2 :intgerface
interface FactoryI<T> {
T create();
}
class ClassAsFactory2<T> {
public <F extends FactoryI<T>> ClassAsFactory2(F factory) {
System.out.println(factory.create().getClass().getSimpleName() + " create successful");
}
}
class EmployFactory implements FactoryI<Employ> {
public Employ create() {
return new Employ();
}
}
class IntegerFactory implements FactoryI<Integer> {
public Integer create() {
return new Integer(0);
}
}
// solution 3: Template Method design pattern
abstract class GenericWithCreate<T> {
final T element;
GenericWithCreate() {
element = create();
}
abstract T create();
}
class IntegerGeneric extends GenericWithCreate<Integer> {
@Override
Integer create() {
return new Integer("4");
}
}
相关文章推荐
- java 创建一个Class的对象来获取泛型的class
- 使用Java继承与泛型特性创建高效与安全的DAO对象
- java创建一个对象获取泛型的class
- java创建对象的四种方法
- 将java类的泛型集合转换成json对象
- Java 创建对象有哪几种方式
- [ java ] java语言中的子类对象和父类对象的创建过程!
- 1. Java面向对象之泛型-认识泛型
- Java之创建对象>7.Avoid finalizers
- java-对象的创建
- [转]JAVA中对象创建和初始化过程
- 【Java学习笔记】创建对象时类的加载顺序——基于Java SE8
- JAVA创建对象的几种方式
- java加载类创建对象的过程以及静态代码段的执行时机
- Java中共享对象的创建与销毁详解(附源码)
- 一种更快的针对Java对象深拷贝的方法(翻译)
- Java几种创建对象速度的比较
- Java 判断文件夹、文件是否存在、否则创建文件夹
- 深入理解java对象的创建过程:类的初始化和实例化
- 关于java创建对象的四种方法