java中class t obj_如何在Java中使用Class <T>?

我们所知道的是“任何类的所有实例都共享该类类型的java.lang.Class对象”

例如)

Student a = new Student();

Student b = new Student();

然后 a.getClass() == b.getClass() 是真的 .

现在假设

Teacher t = new Teacher();

没有泛型,下面是可能的 .

Class studentClassRef = t.getClass();

但现在这是错的..?

例如 public void printStudentClassInfo(Class studentClassRef) {} 可以用 Teacher.class 调用

使用泛型可以避免这种情况 .

Class studentClassRef = t.getClass(); //Compilation error.

现在什么是T ?? T是类型参数(也称为类型变量);由尖括号(<>)分隔,跟在类名后面 .

T只是一个符号,就像在编写类文件时声明的变量名(可以是任何名称) . 后来T将被替换

初始化期间的有效类名称( HashMap map = new HashMap(); )

例如) class name

所以 Class 表示特定类类型' T '的类对象 .

假设您的类方法必须使用下面的未知类型参数

/**

* Generic version of the Car class.

* @param the type of the value

*/

public class Car {

// T stands for "Type"

private T t;

public void set(T t) { this.t = t; }

public T get() { return t; }

}

这里T可以用作 String 类型 CarName

OR T可以用作 Integer 类型 modelNumber ,

OR T可以用作 Object 类型 valid car instance .

现在,上面是简单的POJO,它可以在运行时以不同的方式使用 .

集合例如List,Set,Hashmap是根据T的声明使用不同对象的最佳示例,但是一旦我们将T声明为String

例如 HashMap map = new HashMap(); 然后它只接受String Class实例对象 .

Generic Methods

通用方法是引入其自己的类型参数的方法 . 这类似于声明泛型类型,但类型参数的范围仅限于声明它的方法 . 允许使用静态和非静态泛型方法,以及泛型类构造函数 .

泛型方法的语法包括一个类型参数,在尖括号内,并出现在方法的返回类型之前 . 对于泛型方法,类型参数部分必须出现在方法的返回类型之前 .

class Util {

// Generic static method

public static boolean compare(Pair p1, Pair p2) {

return p1.getKey().equals(p2.getKey()) &&

p1.getValue().equals(p2.getValue());

}

}

class Pair {

private K key;

private V value;

}

这里 是方法参数中使用的类型声明,它应该在返回类型之前 boolean 这里 .

在下面;类型声明 在方法级别不是必需的,因为它已在类级别声明 .

class MyClass {

private T myMethod(T a){

return a;

}

}

但是下面的错误是因为类级别的参数K,V,Z和Y不能在静态上下文中使用(这里是静态方法) .

class Util {

// Generic static method

public static boolean compare(Pair p1, Pair p2) {

return p1.getKey().equals(p2.getKey()) &&

p1.getValue().equals(p2.getValue());

}

}

OTHER VALID SCENARIOS ARE

class MyClass {

//Type declaration already done at class level

private T myMethod(T a){

return a;

}

// is overriding the T declared at Class level;

//So There is no ClassCastException though a is not the type of T declared at MyClass.

private T myMethod1(Object a){

return (T) a;

}

//Runtime ClassCastException will be thrown if a is not the type T (MyClass).

private T myMethod1(Object a){

return (T) a;

}

// No ClassCastException

// MyClass obj= new MyClass();

// obj.myMethod2(Integer.valueOf("1"));

// Since type T is redefined at this method level.

private T myMethod2(T a){

return a;

}

// No ClassCastException for the below

// MyClass o= new MyClass();

// o.myMethod3(Integer.valueOf("1").getClass())

// Since is undefined within this method;

// And MyClass don't have impact here

private T myMethod3(Class a){

return (T) a;

}

// ClassCastException for o.myMethod3(Integer.valueOf("1").getClass())

// Should be o.myMethod3(String.valueOf("1").getClass())

private T myMethod3(Class a){

return (T) a;

}

// Class a :: a is Class object of type T

// is overriding of class level type declaration;

private Class myMethod4(Class a){

return a;

}

}

最后,静态方法总是需要显式 声明;它不会从 class Class 派生 . 这是因为类级别T与实例绑定 .

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值