public static <T> T foo(X<T> param)比如
public static <T> T foo(Class<T> clazz)
String str = foo(String.class);
public static <T> T foo(Class<T> clazz)
String str = foo(String.class);
调试欢乐多
JAVA 泛型蛮好用 public static <T> T get(String classname) throws Exception{
return (T)Class.forName(classname).newInstance();
}
public static <T extends Comparable<? super T>> void sort(List<T> list)
这样的
单例并不是一个好的泛型练习对象。严格、简洁并且线程安全的单例应该使用enum。
连语法糖都称不上
Python是强类型!咕~~(╯﹏╰)bnum = 1;
string = 'This is string'
num + string //Error: unsupported operand type(s) for +: 'int' and 'str'
Python是强类型!咕~~(╯﹏╰)bnum = 1;
string = 'This is string'
num + string //Error: unsupported operand type(s) for +: 'int' and 'str'
Java是弱类型
int num = 1;
String string = "This is string";
System.out.println(num + string); // 1 This is string
Python是强类型!咕~~(╯﹏╰)bnum = 1;
string = 'This is string'
num + string //Error: unsupported operand type(s) for +: 'int' and 'str'
Java是弱类型
int num = 1;
String string = "This is string";
System.out.println(num + string); // 1 This is string
关键不是operator+可不可用,而是要注意错误提示,Python是区分int类型和string类型的,明确Python是强类型!
Python是强类型!咕~~(╯﹏╰)bnum = 1;
string = 'This is string'
num + string //Error: unsupported operand type(s) for +: 'int' and 'str'
Java是弱类型
int num = 1;
String string = "This is string";
System.out.println(num + string); // 1 This is string
关键不是operator+可不可用,而是要注意错误提示,Python是区分int类型和string类型的,明确Python是强类型!
科普一下强弱类型:http://www.oschina.net/question/127301_37313
Python是强类型!咕~~(╯﹏╰)bnum = 1;
string = 'This is string'
num + string //Error: unsupported operand type(s) for +: 'int' and 'str'
Java是弱类型
int num = 1;
String string = "This is string";
System.out.println(num + string); // 1 This is string
关键不是operator+可不可用,而是要注意错误提示,Python是区分int类型和string类型的,明确Python是强类型!
科普一下强弱类型:http://www.oschina.net/question/127301_37313
以上“科普”为个人理解,以下也为个人理解:
强类型由编译器(解释器)来管理变量的类型,高级语言基本都支持强类型
弱类型需要编码人员自己来管理变量类型。但是说某一门语言是强弱类型有点不太准确的,只能说支持强弱类型。我们常说C是强类型的,而在某些框架下,比如Win32,会有HANDLE一类的变量,真正的类型是什么编译器是不进行处理的,需要程序员在使用的时候给其定义。“科普”中说的是动态类型和静态类型的区别,而这个我们可以明确的说,C是静态类型语言,Python是动态类型语言。
C++对泛型的支持是其他语言望尘莫及的,包括Python;可能是因为我学的语言不多,说Python泛型强的原因是像Python这样简单,明了的泛型,还是让我比较吃惊的。
Python是强类型!咕~~(╯﹏╰)bnum = 1;
string = 'This is string'
num + string //Error: unsupported operand type(s) for +: 'int' and 'str'
Java是弱类型
int num = 1;
String string = "This is string";
System.out.println(num + string); // 1 This is string
关键不是operator+可不可用,而是要注意错误提示,Python是区分int类型和string类型的,明确Python是强类型!
科普一下强弱类型:http://www.oschina.net/question/127301_37313
以上“科普”为个人理解,以下也为个人理解:
强类型由编译器(解释器)来管理变量的类型,高级语言基本都支持强类型
弱类型需要编码人员自己来管理变量类型。但是说某一门语言是强弱类型有点不太准确的,只能说支持强弱类型。我们常说C是强类型的,而在某些框架下,比如Win32,会有HANDLE一类的变量,真正的类型是什么编译器是不进行处理的,需要程序员在使用的时候给其定义。“科普”中说的是动态类型和静态类型的区别,而这个我们可以明确的说,C是静态类型语言,Python是动态类型语言。
说得很有道理,我把静态动态跟强弱类型弄混了。
查了一下wiki,发现很有意思的是,语言的“强弱”类型也是相对的,wiki说C#, Java等被认为是相对C更强的类型,而Pascal则是“太强”了一些。
wiki上也说对于强弱类型的定义有很多种,不过最常见的还是该语言提供了多少隐式类型转换的功能。比如Java也许没有Python“强”,因为"a" + 2这种在java合法,2被隐式转换为了"2"。
最有趣的还是VB,它提供了从String类型到int类型的自动转换
Python是强类型!咕~~(╯﹏╰)bnum = 1;
string = 'This is string'
num + string //Error: unsupported operand type(s) for +: 'int' and 'str'
Java是弱类型
int num = 1;
String string = "This is string";
System.out.println(num + string); // 1 This is string
关键不是operator+可不可用,而是要注意错误提示,Python是区分int类型和string类型的,明确Python是强类型!
科普一下强弱类型:http://www.oschina.net/question/127301_37313
以上“科普”为个人理解,以下也为个人理解:
强类型由编译器(解释器)来管理变量的类型,高级语言基本都支持强类型
弱类型需要编码人员自己来管理变量类型。但是说某一门语言是强弱类型有点不太准确的,只能说支持强弱类型。我们常说C是强类型的,而在某些框架下,比如Win32,会有HANDLE一类的变量,真正的类型是什么编译器是不进行处理的,需要程序员在使用的时候给其定义。“科普”中说的是动态类型和静态类型的区别,而这个我们可以明确的说,C是静态类型语言,Python是动态类型语言。
说得很有道理,我把静态动态跟强弱类型弄混了。
查了一下wiki,发现很有意思的是,语言的“强弱”类型也是相对的,wiki说C#, Java等被认为是相对C更强的类型,而Pascal则是“太强”了一些。
wiki上也说对于强弱类型的定义有很多种,不过最常见的还是该语言提供了多少隐式类型转换的功能。比如Java也许没有Python“强”,因为"a" + 2这种在java合法,2被隐式转换为了"2"。
最有趣的还是VB,它提供了从String类型到int类型的自动转换最有趣的的是C++,你可以自定转换//为了省事,只写窄字节版
#include <iostream>
#include <sstream>
#include <string>template<typename DestType, typename SourceType>
DestType lexical_cast(const SourceType& src)
{
std::stringstream ss;
ss << src; DestType dest;
ss >> dest; return dest;
}template<typename T>
T operator+(const T& lhs, const std::string& rhs)
{
return lhs + lexical_cast<T>(rhs);
}template<typename T>
T operator+(const std::string& lhs, const T& rhs)
{
return lexical_cast<T>(lhs) + rhs;
}int main()
{
int num = 0;
std::string str = "123";
num = str + num;
std::cout << num + str << std::endl;
return 0;
}