Day13 泛型
泛型
泛型定义
在一个类型(类,接口,方法)之后,定义一个类型参数。
原生类型:类型后面没有指定具体的类型参数。
好处
使用泛型的好处在于,它在编译的时候进行类型安全检查,并且在运行时所有的转换都是强制的,隐式的,大大提高了代码的重用率。
语法
class Point<T>{}
通配符(?)
无界通配符(?)可以接受任何的类类型。
public void show(Point<?> p){}
(? extends 上限类)只能接受上限类和上限类子类,如只能接受数字类型,Byte,Short,Integer,Long,Float,Double
public void show(Point<? extends Number> p){}
(? super 下限类)下限,下限类和下限类的父类,Number和Number的父类
public void show(Point<? super Number> p){}
注意
Point<Object>不是Point<String>、Point<Integer>等的父类
public void add(Point<String> ps,Point<Object> po){//错误
po = ps
} public void add(Point<String> ps,Point<? extends Object> po){//正确
类型参数与通配符的区别
类型参数与 通配符的区别:
<T> 和 <?>
1.类型参数可以指定上限,也只能指定上限;
统配符可以指定上限,也可以指定下限;
2.类型参数 可以指定多个 上限;
通配符 只能指定一个上限;
3.类型参数 可以作为一种类型存在;
统配符不能表示 类型
泛型构造
//定义了一个泛型类
class PointN<T>{//类型参数 形式类型参数
private T x;
private T y;
//定义了一个泛型构造器
<E>PointN(E e){
System.out.println(e);
}
public T getX() {
return x;
}
public void setX(T x) {
this.x = x;
}
public T getY() {
return y;
}
public void setY(T y) {
this.y = y;
}
}
public class TestPoint2 {
public static void main(String[] args) {
// 指定具体的类型参数
PointN<String> p1 = new <Integer>PointN<String>();
//类型推断: 根据参数 的类型 自动推断出 构造的类型参数 是Integer类型
PointN<String> p2 = new PointN<String>();
} }
泛型方法
定义泛型方法时,必须在返回值前边加一个<T>,来声明这是一个泛型方法,持有一个泛型T,然后才可以用泛型T作为方法的返回值。
class Demo{
//泛型方法
public <E> void f(E e) {
System.out.println(e);
}
public <E> E ff(E e) {
return e;
}
public <E extends Number> void fff(E e) {
System.out.println(e);
//自动推断的方式
f("hello");
//指定的方式,显示,this
this.<String>f("hello");
}
}
public class TestPoint3 {
public static void main(String[] args) {
// 泛型方法应用
Demo d = new Demo();
//具体指定 泛型方法参数的类型
d.<String>f("hello");
//可以使用类型推断: 根据参数的类型 自动推断
d.f();
d.fff(); } }
泛型擦除
1.参数化类型:Point<String>,擦除后为原生类型Point
2.类型参数:用上界来替换
Point<T>:无界类型参数,用Object来替换
类型参数有上限,用上限来替换。
有多个上限,用第一个上限替换。
重载
class PointNew<T>{}
interface Info1{}
interface Info2{} class Demo1{
//为了实现重载
//生成的字节码文件中,泛型的信息被擦除了
public void f(String p) {}
public void f(PointNew<Integer> p) {}//擦除后:PointNew p
public <E> void f(E e) {}//擦除后:Object e
public <E extends Info1> void f(E e) {}//擦除后:Info1 e
public <E extends Info2 & Info1> void f(E e) {}//擦除后:Info2 e }
重写
class Parent{
//重写:参数父类中参数擦除后与子类中参数相同
public void f(PointNew<String> p) {}
}
class Child1 extends Parent{
public void f(PointNew<String> p) {}
}
泛型接口
//泛型接口
interface Info<T>{
void f(T t);
}
//在实现接口时 指定具体的类型参数
class InfoImpl1 implements Info<String>{
@Override
public void f(String t) {
System.out.println(t);
}
}
//在实现 接口时 不能确定类型
class InfoImpl2<T> implements Info<T>{
@Override
public void f(T t) {
System.out.println(t);
}
}
public class TestPoint5 { public static void main(String[] args) {
InfoImpl1 i1 = new InfoImpl1();
i1.f("hello");
InfoImpl2<String> i2 = new InfoImpl2<>();
i2.f("tom");
} }
比较器
Comparable
Comparator
对象进行自然方式排序,必须是实现了Comparable接口的。
Arrays.sort(数组);//默认 自然排序
Comparable<T> 泛型接口 比较器实现自然排序
Comparator<T> 泛型接口 外部比较器
package day13; import java.util.Arrays;
import java.util.Comparator;
class Student implements Comparable<Student>{
private int age;
private int no;
public Student(int no,int age) {
this.age = age;
this.no= no;
}
public int getAge() {
return age;
}
public int getNo() {
return no;
}
@Override
public String toString() {
return "编号:"+no+",年龄:"+age;
}
/* @Override
public int compareTo(Object o) {
//this 与 o比较
//this.age o.age
Student stu = (Student)o;
//this.age stu.age return 0;
}*/
@Override
public int compareTo(Student o) {
// this.age o.age
//升序的方式
/*if(this.age > o.age ) {
return 1;//正数
}else if(this.age < o.age) {
return -1;//负数
}else {
return 0;//相等
}*/
return this.age - o.age;
}
}
//按照 编号升序排序
class NoComparator implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
// o1 和 o2比较
/* if(o1.getNo() > o2.getNo()) {
return 1;
}else if(o1.getNo() < o2.getNo()) {
return -1;
}else {
return 0;
}*/
return o1.getNo()- o2.getNo();
}
}
public class TestSort {
public static void main(String[] args) {
Student zhang = new Student(,);
Student wang = new Student(,);
Student zhao = new Student(,);
Student [] stus = {zhang,wang,zhao};
//Arrays.sort()按照自然方式排序的
// Arrays.sort(stus);
//1.自定义类 实现接口
// Arrays.sort(stus, new NoComparator());
//2.匿名内部类实现接口
Arrays.sort(stus,new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getNo() - o2.getNo();
}
});
//3 Lambda
Arrays.sort(stus,(s1,s2)->s1.getNo()-s2.getNo()); Arrays.stream(stus).forEach(System.out::println);
//--------------------------------------------
int [] arr = {,,,,};
Arrays.sort(arr);
Arrays.stream(arr).forEach(System.out::println);
//--------------------------------------------
String [] arrs = {"cc","dd","aa","ee"};
Arrays.sort(arrs);
Arrays.stream(arrs).forEach(System.out::println);
//------------------------------------------------
} }
枚举类型
语法
enum 枚举类型{ }
package day13; import java.util.Scanner; //RGB
/*class Color{
public static final int RED = 1;
public static final int GREEN = 2;
public static final int BLUE = 3;
}*/
interface ColorInfo{
void fv();//抽象方法
}
enum Color{
RED(,"红色") {
@Override
void f() {
// TODO Auto-generated method stub }
},GREEN(,"绿色") {
@Override
void f() {
// TODO Auto-generated method stub }
},BLUE(,"蓝色") {
@Override
void f() {
// TODO Auto-generated method stub }
}; private Color(int no, String name) {
this.no = no;
this.name = name;
}
private int no;
private String name;
//抽象方法
abstract void f();
}
/*enum Color implements ColorInfo{
//定义枚举成员,对象,实例
RED(1,"红色"){
public void fv() {
System.out.println("红色的");
}
},GREEN(2,"绿色"){
public void fv() {
System.out.println("绿色的");
}
},BLUE(3,"蓝色"){
public void fv() {
System.out.println("蓝色的");
}
}; //public static final
private int no;//编号
private String name;//名字 private Color(int no, String name) {
this.no = no;
this.name = name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void f() {}
@Override
public String toString() {
// TODO Auto-generated method stub
return this.no+","+this.name();
}
@Override
public void fv() {
// TODO Auto-generated method stub } }*/
public class TestEnum {
public static void main(String[] args) {
// System.out.println(Color.RED.getNo()+","+Color.RED.getName());
// System.out.println(Color.BLUE.getNo()+","+Color.BLUE.getName());
/* Color.RED.setName("红色");
System.out.println(Color.RED.getName());
Color.RED.f();*/
/* Color.RED.no = 11;
Color.GREEN.name = "绿色";
System.out.println(Color.RED.no);
System.out.println(Color.GREEN.name);*/ /* Scanner input = new Scanner(System.in);
System.out.println("输入一个颜色:");
String s = input.next();
Color co = Color.valueOf(s);
switch(co) {
case RED:
System.out.println("进行红色的操作");
break;
case GREEN:
System.out.println("进行绿色的操作");
break;
case BLUE:
System.out.println("进行蓝色的操作");
break;
}*/ //可以遍历枚举值
/* for(Color c: Color.values()) {
System.out.println(c.name());
System.out.println(c.ordinal());
}*/ /* // 类型不安全
int red = Color.RED;
int a = red +5;
System.out.println(a);
//意思不明确
System.out.println(Color.RED);*/
// System.out.println(Color.RED); } }
注意
1.所有的枚举类型的对象都在枚举类第一行显示定义出来。
2.对象默认是public static final的
3.枚举类的构造都是private的
4.枚举类继承自java.lang.Enum类
5.对于一个非抽象枚举类来说,都是final
一个枚举类的所有对象都实现了接口中的抽象方法,此枚举类就是抽象类,abstract的了
枚举类中定义抽象abstract方法,所有对象都需要实现此抽象方法,那么此枚举类是abstract的了
强制垃圾回收
class Person{ @Override
protected void finalize() throws Throwable {
System.out.println("this---->"+this);
} }
public class TestGC { public static void main(String[] args) {
// 强制垃圾回收
Person per = new Person();
System.out.println(per);
per = null;//断开引用
System.gc();//强制通知垃圾回收器
// Runtime.getRuntime().gc();// } }
最新文章
- Spring mvc Security安全配置
- VS2010默认属性文件配置
- Java 自定义注解
- DedeCMS的分页标签pagelist支持中英文的修改方法[转]
- segmentation fault
- 【POJ】【2348】Euclid‘s Game
- bnu 4359 无爱编号(规律)
- Android滚动截屏,ScrollView截屏
- 监听键盘 防止输入时覆盖掉textfiled
- CentOS7 PostgreSQL 主从配置( 一)
- Appium+Python自动化 1 环境搭建(适用windows系统-Android移动端自动化)
- 浅析JavaScript之数组
- Spring Security(十):3. What’s New in Spring Security 4.2 (新功能)
- Android中如何使用xmlns
- 百度前端技术学院task34源码——会指令的小块2
- android 发送短信的两种方式,以及接收报告和发送报告
- 使用 Mirantis Fuel9.0 部署 OpenStack M
- TF Boys (TensorFlow Boys ) 养成记(二): TensorFlow 数据读取
- linux命令总结之route命令
- java 如何取前32位全是1的int型数据的后八位