我想知道以下两种方法声明之间的区别:
public Object doSomething(Object obj) {....}
public <T> T doSomething(T t) {....}
是否有东西可以,但不是其他? 我无法在这个地点其他地方找到这个问题。
我想知道以下两种方法声明之间的区别:
public Object doSomething(Object obj) {....}
public <T> T doSomething(T t) {....}
是否有东西可以,但不是其他? 我无法在这个地点其他地方找到这个问题。
脱离情况——没有区别。 obj
页: 1 目标代码>。
但是,在环境方面,如果你有一个通用类别:
MyClass<Foo> my = new MyClass<Foo>();
Foo foo = new Foo();
然后:
Foo newFoo = my.doSomething(foo);
2. 标有物体的混合代码
Foo newFoo = (Foo) my.doSomething(foo);
两项优势:
Object
version is used, you won t be sure that the method always returns Foo
. If it returns Bar
, you ll have a ClassCastException
, at runtime.这里的区别是,首先,我们具体规定,打电话者必须通过反对(任何一类),并将重新提出另一个目标(任何一类,不一定相同)。
在第二种情况下,所交回的类型将与确定该类别时的类型相同。
Example ex = new Example<Integer>();
在此,我们具体说明哪类T将使我们能够对某类或方法施加更多的限制。 例如,我们可以立即发出<条码>LinkedList<Integer>或LinkedList<Example>
。 我们知道,当我们称之为其中一种方法时,我们就会重回Integer或Example例。
这里的主要目标是,电话法可以规定某类物体的类型,而不是依赖类型预测来执行。
* 更新链接。
不同之处在于,采用通用方法,我不需要去做,如果我有错的话,我就会出现汇编错误:
public class App {
public static void main(String[] args) {
String s = process("vv");
String b = process(new Object()); // Compilation error
}
public static <T> T process(T val) {
return val;
}
}
Using object I always need to cast and I don t get any errors when I do wrong:
public class App {
public static void main(String[] args) {
String s = (String)process("vv");
String b = (String)process(new Object());
}
public static Object process(Object val) {
return val;
}
}
你们不需要做额外的分类。 首先,你们总是会得到一门 j。 你们需要向你们阶层提出反对。 在第二种情况下,T将被通用签名中定义的类别所取代,不需要进行分类。
无。 但是,在汇编时间时,二者将进行类型核对,以确保参数的类型和返回价值匹配的类型(或属于亚类)达到任何类型的T决心(第一例也是检查类型,但每一物体都是反对的亚类,因此每一类目标都会被接受)。
T是一种通用类型。 剂量可由任何合格的物体代用。 您可援引以下方法:
String response = doSomething(“hello World”);
页: 1
MyObject response = doSomething(new MyObject());
页: 1
<编码> Integer response = doSomething(31);
你可以看到,这里存在多变。
但是,如果宣布返回该国,它就会返回。 目标,除非你打造物品,否则你可以这样做。
in the first case it takes a parameter of any type e.g.string and return a type foo. In the second case it takes a parameter of type foo and returns an object of type foo.
没有什么理由可以认为一般性原则凌驾于他人之上。 Java的目标类型:
Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...
Check this, List<String> list = new ArrayList<String>(); for (int i = 0; i < 10000; i++) { String value = (""+UUID.randomUUID().getLeastSignificantBits()).substring(3, ...
I am in the middle of solving a problem where I think it s best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set ...
I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far. The stored procedure has 1 input and 1 output parameter. With every combination I ...
I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...
If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...
I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....
I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...