栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Android学习记录043

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android学习记录043

一、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=============");
        //因为Fruit不是Apple的子类,所以 generically.GenericType无法转换为generically.GenericType
//        print1(genericTypeFruit); //不兼容的类型: generically.GenericType无法转换为generically.GenericType
        print1(genericTypeHongFushi);
        print1(genericTypeApple);
        //泛型的上界
        GenericType 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=============");
//        print2(genericTypeFruit);
        // 因为HongFushi不是Apple的超类,所以不能强转
//        print2(genericTypeHongFushi); // 不兼容的类型: generically.GenericType无法转换为generically.GenericType
        print2(genericTypeApple);

        //泛型的下界
        GenericType 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 data) {
        Apple apple = data.getData();
    }

    public static void print2(GenericType 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()
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/356421.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号