一、java泛型
package generically;
public class Main {
public static void main(String[] args) {
GenericType genericTypeFruit = new GenericType<>();
GenericType genericTypeApple = new GenericType<>();
GenericType genericTypeOrange = new GenericType<>();
GenericType genericTypeHongFushi = new GenericType<>();
System.out.println("=============GenericType extends Apple>=============");
//因为Fruit不是Apple的子类,所以 generically.GenericType无法转换为generically.GenericType extends generically.Apple>
// print1(genericTypeFruit); //不兼容的类型: generically.GenericType无法转换为generically.GenericType extends generically.Apple>
print1(genericTypeHongFushi);
print1(genericTypeApple);
//泛型的上界
GenericType extends Apple> extendsVariable = new GenericType<>();
// extendsVariable.setData(new Object());// 不兼容的类型: java.lang.Object无法转换为capture#1, 共 ? extends generically.Apple
// extendsVariable.setData(new HongFushi()); // 不兼容的类型: generically.HongFushi无法转换为capture#1, 共 ? extends generically.Apple
// extendsVariable.setData(new Apple()); //不兼容的类型: generically.HongFushi无法转换为capture#1, 共 ? extends generically.Apple
// extendsVariable.setData(new Fruit()); //不兼容的类型: generically.HongFushi无法转换为capture#1, 共 ? extends generically.Apple
Apple apple = extendsVariable.getData();
System.out.println("=============GenericType super Apple>=============");
// print2(genericTypeFruit);
// 因为HongFushi不是Apple的超类,所以不能强转
// print2(genericTypeHongFushi); // 不兼容的类型: generically.GenericType无法转换为generically.GenericType super generically.Apple>
print2(genericTypeApple);
//泛型的下界
GenericType super Apple> superVariable = new GenericType<>();
superVariable.setData(new HongFushi());
superVariable.setData(new Apple());
// superVariable.setData(new Fruit());//不兼容的类型: generically.Fruit无法转换为capture#1, 共 ? super generically.Apple
Object object = superVariable.getData();
}
public static void print1(GenericType extends Apple> data) {
Apple apple = data.getData();
}
public static void print2(GenericType super Apple> data) {
Object object = data.getData();
}
}
class GenericType {
private T data;
public void setData(T data) {
this.data = data;
}
public T getData() {
return data;
}
}
二、kotlin泛型
package com.study
open class Fruit()
open class Apple() : Fruit()
open class HongFushi() : Apple()
open class Orange() : Fruit()
class GenericType {
private var data: T? = null
fun setData(data: T) {
this.data = data
}
fun getData(): T? = data
}
class GenericOutType {
private var data: T? = null
// fun setData(data: T) {
// this.data = data
// }
fun getData(): T? = data
}
class GenericInType {
private var data: T? = null
fun setData(data: T) {
this.data = data
}
// fun getData(): T? = data
}
fun main() {
val genericTypeFruit = GenericType()
val genericTypeApple = GenericType()
val genericTypeOrange = GenericType()
val genericTypeHongFushi = GenericType()
// print1(genericTypeFruit) // Type mismatch: inferred type is GenericType but GenericType was expected
print1(genericTypeApple)
print1(genericTypeHongFushi)
// val outVariable = GenericType() //Projections are not allowed on type arguments of functions and properties
// val inVariable = GenericType() //Projections are not allowed on type arguments of functions and properties
print2(genericTypeFruit)
print2(genericTypeApple)
}
fun print1(data: GenericType) {
val apple:Apple? = data.getData()
}
fun print2(data: GenericType) {
val `object`:Any? = data.getData()
}