一、问题
泛型者,前期不定,后期确定的参数类型者也;在我们处理参数类型比较多的时候,可以抽象成一个模糊的类型,这个类型在没有传参之前是模糊的,但是一旦传值了,就不能再改变参数的类型了,只能使用之前传的参数类型;
二、举例
1.理解概念;
先看下面的一个例子;我们创建了一个汽车对象,汽车内可以装各种类型的参数,这个可以用来类比泛型的原理;
/**
* @author Auther
* @title: Car
* @projectName generatorSqlmapCustom
* @description: TODO
* @date 08/05/201916:32
*/
public class Car {
private Object object;
public Car(Object o){
this.object = o;
}
public Object getObject() {
return object;
}
public void setObject(Object object) {
this.object = object;
}
public static void main(String[] args) {
Car car = new Car(new Customer());
Customer customer = (Customer) car.getObject();
String s = customer.getCustName();
}
}
2.简单的泛型例子;
我们又造了一部车,这部车没有使用Object对象,这个就是一个真正意义上的泛型了;
package com.caliper.body.domain;
import com.caliper.tail.batch.domain.Customer;
/**
* @author Auther
* @title: Car3
* @projectName generatorSqlmapCustom
* @description: TODO
* @date 08/05/201916:37
*/
public class Car3<T> {
private T a;
public Car3(T a){
this.a = a;
};
public T getA() {
return a;
}
public void setA(T a) {
this.a = a;
}
public static void main(String[] args) {
Car3<Customer> customerCar = new Car3<Customer>(new Customer());
Customer customer = customerCar.getA();
}
}
3.复杂的泛型的例子
我们有了泛型,有什么意思呢,下面看一个复杂的例子;
package com.caliper.body.domain;
/**
* @author Auther
* @title: LinkedStack
* @projectName generatorSqlmapCustom
* @description: TODO
* @date 08/05/201917:15
*/
public class LinkedStack<T> {
/**
* 静态内部类
*
* @param <U>
*/
private static class Node<U> {
U item;
Node<U> next;
/**
* 无参构造
*/
Node() {
item = null;
next = null;
}
/**
* 有参构造
*
* @param item 泛型1
* @param next 泛型2
*/
Node(U item, Node<U> next) {
this.item = item;
this.next = next;
}
boolean end() {
return item == null && next == null;
}
}
/**
* 末端哨兵 End sentinel;
*/
private Node<T> top = new Node<T>();
/**
* push 添加方法
*
* @param item 入参
*/
public void push(T item) {
top = new Node<T>(item, top);
}
/**
* pop弹出方法
*
* @return T 泛型
*/
public T pop() {
T result = top.item;
if (!top.end()) {
top = top.next;
return result;
}
return result;
}
/**
* main 方法
*
* @param args 命令参数
*/
public static void main(String[] args) {
LinkedStack<String> lss = new LinkedStack<String>();
for (String s : "Phasers on stun".split(" ")) {
lss.push(s);
}
String s;
while ((s = lss.pop()) != null) {
System.out.println("" + s);
}
}
}
三、总结
所以,使用Object和泛型最根本的区别是什么呢?Object是所有类的父类,范围特别广,特别模糊,而泛型在确定了入参后,就不再模糊;
打个比方就是,瞎一辈子和瞎上半辈子的区别;
四、附录
The Java Tutorials have been written for JDK 8. Examples and practices described in this page don’t take advantage of improvements introduced in later releases.
Lesson: Generics (Updated)
In any nontrivial software project, bugs are simply a fact of life. Careful planning, programming, and testing can help reduce their pervasiveness, but somehow, somewhere, they’ll always find a way to creep into your code. This becomes especially apparent as new features are introduced and your code base grows in size and complexity.
Fortunately, some bugs are easier to detect than others. Compile-time bugs, for example, can be detected early on; you can use the compiler’s error messages to figure out what the problem is and fix it, right then and there. Runtime bugs, however, can be much more problematic; they don’t always surface immediately, and when they do, it may be at a point in the program that is far removed from the actual cause of the problem.
Generics add stability to your code by making more of your bugs detectable at compile time. After completing this lesson, you may want to follow up with the Generics tutorial by Gilad Bracha.
1.欢迎关注我的微信公众号:幕桥社区
2.优快云博客:https://blog.youkuaiyun.com/River_Continent
3.知乎地址:https://www.zhihu.com/people/zhang-mu-ye-37-76/columns
4.简书地址:https://www.jianshu.com/u/02c0096cbfd3