一、常规案例
总结:出现类似安全问题
代码片段:
class Auto{
private Object X;
private Object Y;
public Object getX() {
return X;
}
public void setX(Object x) {
this.X = x;
}
public Object getY() {
return Y;
}
public void setY(Object y) {
this.Y = y;
}
}
public class TestType {
public static void main(String[] args) {
//案例1
Auto auto = new Auto();
auto.setX(10);
auto.setY(10);
int x = (Integer)youceedu.getX();
int y = (Integer)youceedu.getY();
System.out.println("x=" + x);
System.out.println("y=" + y);
//案例2
/*
Auto auto= new Auto();
auto.setX(10.3);
auto.setY(10.6);
Float x = (Float)auto.getX();
Float y = (Float)auto.getY();
System.out.println("x=" + x);
System.out.println("y=" + y);
*/
//案例3
/*
Auto auto= new Auto();
auto.setX(10.3);
auto.setY("测试");
Float x = (Float)auto.getX();
Float y = (Float)auto.getY();
System.out.println("x=" + x);
System.out.println("y=" + y);
*/
}
}
二、泛型
①、价值:解决数据类型安全性问题
②、原理:在类声明时通过一个标识来识别类中某个属性的类型,某个方法的返回值及参数类型
1、泛型类格式:
[访问权限] class 类命令<泛型类型标识1,泛型类型标识2,泛型类型标识3>{
[访问权限] 泛型类型标识 变量名称;
[访问权限] 泛型类型标识 方法名称(){};
[访问权限] 返回值类型声明 方法名称(泛型类型标识 变量名称){}
}
注:泛型类型标识任意字母都行,<A>,<B>,之所以使用<T>是因为T标识type,给传参时都需传类,而不是数据类型
2、泛型对象格式:
类名称<具体类> 对象名称 = new 类名称<具体类>();
代码片段:
3、泛型构造方法格式:
[访问权限] 构造方法([泛型类型 参数名称]){
}
代码片段:
package com.auto.tools.type;
class Auto<K,V>{
//此属性类型有外部决定
private K X;
private V Y;
public Auto(K x, V y) {
this.X = x;
this.Y = y;
}
public K getX() {
return X;
}
public void setX(K x) {
this.X = x;
}
public V getY() {
return Y;
}
public void setY(V y) {
this.Y = y;
}
}
public class TestType {
public static void main(String[] args) {
//案例1
Auto<Integer,String> auto= new Auto<Integer,String>(4,"testdev");
}
}
4、泛型通配符:
以上操作都设置了一个固定的类型,泛型操作中可以通过通配符接受任意指定泛型类型的对象
格式见以下代码:
package com.auto.tools.type;
import java.util.ArrayList;
import java.util.List;
public class TestType {
public static void main(String[] args) {
List<String> name = new ArrayList<String>();
List<Integer> age = new ArrayList<Integer>();
List<Number> number = new ArrayList<Number>();
name.add("icon");
age.add(18);
number.add(314);
getData(name);
getData(age);
getData(number);
}
public static void getData(List<?> data) {
System.out.println("data :" + data.get(0));
}
}
5、、泛型范围-上限和下限
上限
声明:使用extends关键字声明,表示泛型可能是所指定的类型或者是此类型的子类
格式:
声明对象:类名称<? extends 类> 对象名称
定义类:[访问权限] 类名称 <泛型标识 extends 类>{}
注:可知类,方法都可用
案例1:
package com.auto.tools.type;
//此处泛型只能是数字类型,接收对象是Byte,Short,Long,Integer,Float,Double
class Info<T extends Number>{
private T var;// 定义泛型变量
public void setVar(T var){
this.var = var ;
}
public T getVar(){
return this.var ;
}
public String toString(){
return this.var.toString() ;
}
}
public class TestType {
public static void main(String args[]){
// 声明Integer的泛型对象
Info<Integer> i1 = new Info<Integer>();
i1.setVar(30);
System.out.println(i1.toString());
}
}
案例2:
package com.auto.tools.type;
class Info<T>{
// 定义泛型变量
private T var ;
public void setVar(T var){
this.var = var ;
}
public T getVar(){
return this.var ;
}
public String toString(){
return this.var.toString() ;
}
}
public class TestType {
public static void main(String args[]){
// 声明Integer,Float泛型对象
Info<Integer> i1 = new Info<Integer>();
Info<Float> i2 = new Info<Float>();
i1.setVar(30);
i2.setVar(30.1f);
fun(i1) ;
fun(i2) ;
}
public static void fun(Info<? extends Number> temp){// 只能接收Number及其Number的子类
System.out.print(temp + "、") ;
}
}
下限:
声明:使用super关键字声明,表示泛型的类型可能是所指定的类型或者是此类型的父类或是Object类
格式:
声明对象:类名称<? super 类 >对象名称
定义类:[访问权限] 类名称 <泛型标识 super类>{}
代码部分:
package com.auto.tools.type;
class Info<T>{
private T var;// 定义泛型变量
public void setVar(T var){
this.var = var ;
}
public T getVar(){
return this.var ;
}
public String toString(){
return this.var.toString() ;
}
}
public class TestType {
public static void main(String args[]){
//声明String,Object的泛型对象
Info<String> i1 = new Info<String>();
Info<Object> i2 = new Info<Object>();
i1.setVar("hello") ;
i2.setVar(new Object()) ;
fun(i1) ;
fun(i2) ;
}
//只能接收String或Object类型的泛型,String类的父类只有Object类
public static void fun(Info<? super String> temp){
System.out.print(temp + "、") ;
}
}
7、、泛型接口
格式:[访问权限] interface 接口名称<泛型标识>{
}
接口实现方式:
方式一:
直接在子类后声明泛型
代码部分:
package com.auto.tools.type;
interface Info<T>{
public T getVar();
}
class InfoImpl<T> implements Info<T>{
private T var;// 定义泛型变量
public void setVar(T var){
this.var = var ;
}
public T getVar(){
return this.var ;
}
}
public class TestType {
public static void main(String args[]){
InfoImpl<String> i1 = new InfoImpl<String>();
i1.setVar("hello") ;
System.out.println(i1.getVar());
}
}
方式二:直接在子类实现的接口中明确的给出泛型类型
代码部分:
package com.auto.tools.type;
interface Info<T>{
public T getVar();
}
class InfoImpl implements Info<String>{
private String var;// 定义泛型变量
public void setVar(String var){
this.var = var ;
}
public String getVar(){
return this.var ;
}
}
public class TestType {
public static void main(String args[]){
InfoImpl i1 = new InfoImpl();
i1.setVar("hello") ;
System.out.println(i1.getVar());
}
}