1. 返回值是泛型怎么写
是 C++ 语言语法吗?
#include "stdafx.h"
#include
using namespace std;
//返回泛型的全局函数
template T func(T t)
{
return t;
}
template class A
{
public:
//返回泛型的类成员函数函数
T func(T t)
{
return t;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
cout a;
cout
2. 泛型这样写,什么意思呢
在静态方法 及非静态方法的调用中 一般会写 ClassName.methodName() 或者objectName.methodName() 泛型方法同样适用前面说的这种语法 但如果想加个泛型列表的话 必须写成 ClassName. E - Element (在集合中使用,因为集合中存放的是元素) T - Type(Java 类) K - Key(键) V - Value(值) N - Number(数值类型) ? - 表示不确定的java类型举例说明: Set 泛型的作用: 1、用泛型: Java代码 收藏代码List 要么就这样public class Main { public void test() public void handleCallBack() { new CallBack() .onFinish(new Info()); } public interface CallBack ; public class Info }必须要泛型指定的类型T 在方法内是可见的才能指定吧。 不过这样写和你原本的意图应当不一样了。 因为这样回调和定义必须写在一起 否则就不能指定泛型T的类型了。 public class AccountInfo { public AccountInfo ( string userName, string password, string email) { this.UserName = userName; this.Password = password; this.Email = email; } public string UserName { get; set; } public string Password { get; set; } public string Email { get; set; } } System.Collections.Generic.IList<AccountInfo> list = new System.Collections.Generic.List<AccountInfo> ( ); list.Add ( new AccountInfo ( "User Name 1", "Password 1", "Email 1" ) ); list.Add ( new AccountInfo ( "User Name 2", "Password 2", "Email 2" ) ); list.Add ( new AccountInfo ( "User Name 3", "Password 3", "Email 3" ) ); this.grdEmployees.DataSource = dstXML.Tables [ 0 ].DefaultView;// grdEmployees是GridView this.grdEmployees.DataBind ( ); //补充泛型方法 private System.Collections.Generic.IList<AccountInfo> GetAccounts() { System.Collections.Generic.IList<AccountInfo> list = new System.Collections.Generic.List<AccountInfo> ( ); list.Add ( new AccountInfo ( "User Name 1", "Password 1", "Email 1" ) ); list.Add ( new AccountInfo ( "User Name 2", "Password 2", "Email 2" ) ); list.Add ( new AccountInfo ( "User Name 3", "Password 3", "Email 3" ) ); return list; } public class GenericTest { public static void main(String[] args) { List list = new ArrayList(); list.add("qqyumidi"); list.add("corn"); list.add(100); for (int i = 0; i < list.size(); i++) { String name = (String) list.get(i); // 1 System.out.println("name:" + name); } } } 定义了一个List类型的集合,先向其中加入了两个字符串类型的值,随后加入一个Integer类型的值。 这是完全允许的,因为此时list默认的类型为Object类型。在之后的循环中,由于忘记了之前在list中也加入了Integer类型的值或其他编码原因,很容易出现类似于//1中的错误。 因为编译阶段正常,而运行时会出现“java.lang.ClassCastException”异常。因此,导致此类错误编码过程中不易发现。 在如上的编码过程中,我们发现主要存在两个问题:1.当我们将一个对象放入集合中,集合不会记住此对象的类型,当再次从集合中取出此对象时,改对象的编译类型变成了Object类型,但其运行时类型任然为其本身类型。2.因此,//1处取出集合元素时需要人为的强制类型转化到具体的目标类型,且很容易出现“java.lang.ClassCastException”异常。 那么有没有什么办法可以使集合能够记住集合内元素各类型,且能够达到只要编译时不出现问题,运行时就不会出现“java.lang.ClassCastException”异常呢?答案就是使用泛型。二.什么是泛型?泛型,即“参数化类型”。 一提到参数,最熟悉的就是定义方法时有形参,然后调用此方法时传递实参。那么参数化类型怎么理解呢?顾名思义,就是将类型由原来的具体的类型参数化,类似于方法中的变量参数,此时类型也定义成参数形式(可以称之为类型形参),然后在使用/调用时传入具体的类型(类型实参)。 采用泛型的代码 public class GenericTest { 2 3 public static void main(String[] args) { 4 /* 5 List list = new ArrayList(); 6 list.add("qqyumidi"); 7 list.add("corn"); 8 list.add(100); 9 */10 11 List 且get()方法的返回结果也直接是此形参类型(也就是对应的传入的类型实参)。究其原因,在于Java中的泛型这一概念提出的目的,导致其只是作用于代码编译阶段,在编译过程中,对于正确检验泛型结果后,会将泛型的相关信息擦出,也就是说,成功编译过后的class文件中是不包含任何泛型信息的。 泛型信息不会进入到运行时阶段。3. java class
4. JAVA 回调泛型参数应该怎么写
5. c#泛型,懂c#的进来帮忙下,泛型方法写呢
6. java中泛型的使用多么