Kotlin 型变 + 星号投影(扯蛋)
Kotlin中的型变:
1. in,顾名思义,就是只能作为传入参数的参数类型
2.out, ..............,就是只能作为返回类型参数的参数类型
星号投影:
我们引用官网的吧--
- For
Foo<out T>, whereTis a covariant type parameter with the upper boundTUpper,Foo<*>is equivalent toFoo<out TUpper>. It means that when theTis unknown you can safely read values ofTUpperfromFoo<*>.
就是说返回参数类型可以不具体指定,用*号来引用上界即可,也就是我们用Any类型就行,它的含义Foo<*>=Foo<out Any>
好吧,我给个例子,里边有型变和投影,型变的概念是确定无疑,但是星号投影就是扯蛋,kotlin说了"我们还不成熟"
// 1
我们老老实实的写写
interface MyIfe<out C, in D> {
fun MyFun(d: D) {
println(d)
}
}
class MyClsEx<out A, in B>(val a:A, b:B):MyIfe<Any,Int> {
init {
println(b)
}
fun foo():A {
return a
}
}
fun main(args: Array<String>) {
val res = MyClsEx<Any,Int>("Any", 13)
println("${res.a}+b refering to ahead")
res.MyFun(1)
}
result:
Any+b refering to ahead
1
Ok, 没问题
// 2
我们用上星号投影吧
interface MyIfe<out C, in D> {
fun MyFun(d: D) {
println(d)
}
}
class MyClsEx<out A, in B>(val a:A, b:B):MyIfe<*,Int> {
init {
println(b)
}
fun foo():A {
return a
}
}
fun main(args: Array<String>) {
val res = MyClsEx<Any,Int>("Any", 13)
println("${res.a}+b refering to ahead")
res.MyFun(1)
}
//编译就错误
error: projections are not allowed for immediate arguments of a supertype
class MyClsEx<out A, in B>(val a:A, b:B):MyIfe<*,Int> {
扯蛋第一步产生了,这个星号根本不能用嘛
// 3
怎么办呢?
看看官网,For example, if the type is declared as interface Function<in T, out U> we can imagine the following star-projections
其中interface Function让我想到----“可能在泛型函数中能实现星号投影”
所以
interface MyIfe {
fun <out C, in D> MyFun(d: D) {
println(d)
}
}
class MyClsEx<out A, in B>(val a:A, b:B):MyIfe {
init {
println(b)
}
fun foo():A {
return a
}
}
//编译也错误
error: projections are not allowed for immediate arguments of a supertype
class MyClsEx<out A, in B>(val a:A, b:B):MyIfe<*,Int> {
//结论
泛型三大类别,(类,接口)和函数全都不能星号投影,那么你认为还能投影吗?
我认为不能
Finally:
但我就是对Kotlin有信心,就是这么固执,乍办呢!
哈哈哈
Kotlin 型变 + 星号投影(扯蛋)的更多相关文章
- SEO高手在扯蛋?
真正的高手SEO你在扯蛋吗?当大家都很会扯的时候,高手扯得肯定比你疼,不是他们 蛋比较敏感,而是他们的确更用力. 当你说我是SEO时,高手肯定说现在我在做的是SEM. 当你说我是SEM时,高手肯定在说 ...
- scala 型变
型变是复杂类型的子类型关系与其组件类型的子类型关系的相关性. Scala支持 泛型类 的类型参数的型变注释,允许它们是协变的,逆变的,或在没有使用注释的情况下是不变的. 在类型系统中使用型变允许我们在 ...
- Android之TextView密码输入变星号时间
private static class Visible extends Handler implements UpdateLayout, Runnable{ public Visible(Spann ...
- Cocostudio学习笔记(1) 扯扯蛋 + 环境搭建
转眼七月份就到了,2014已经过了一半,而我也最终算是有"一年工作经验"了,开心ing. 回想这一年Cocos2dx的游戏开发经历,去年下半年重心主要在游戏的逻辑上,而今年上半年重 ...
- Omi框架学习之旅 - 之开篇扯蛋
说实话, 我也不知道Omi是干啥的, 只因此框架是alloyTeam出的, dntzhang写的, 也有其他腾讯大神参与了, 还有一些其他贡献者, 以上我也不太清楚, 当我胡说八嘎. 因其写法有人说好 ...
- kotlin泛型中星号投射
如果一个泛型类型中存在多个类型的参数,那么每个类型的参数都可以单独投射,例如:如果类型定义为:"interface Function<in T,out>",那么可以出现 ...
- 虾扯蛋:Android View动画 Animation不完全解析
本文结合一些周知的概念和源码片段,对View动画的工作原理进行挖掘和分析.以下不是对源码一丝不苟的分析过程,只是以搞清楚Animation的执行过程.如何被周期性调用为目标粗略分析下相关方法的执行细节 ...
- [虾扯蛋] android界面框架-Window
从纯sdk及framwork的角度看,android中界面框架相关的类型有:Window,WindowManager,View等.下面就以这几个类为出发点来概览下安卓开发的"界面架构&quo ...
- 扯蛋css
使网页旋转代码: javascript:window.i=1;setInterval(function(){i++;document.body.style.cssText+="-webkit ...
随机推荐
- C# 方法中的this参数
x 先看下面的代码: public static class StringExtension { public static void Foo(this string s) { Console.Wri ...
- LeetCode 4 - 两个排序数组的中位数 - [分治]
题目链接:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/description/ 给定两个大小为 m 和 n 的有序数组 n ...
- arcengine新建要素类
ArcGIS里面新建数据集,看起来简单,平时都是默认创建,实际上好多细节问题我们都没注意到 一.在数据集上新建要素类: How to create a feature class within a f ...
- 单周期CPU设计的理论基础
写在前面:本博客内容为本人老师原创,严禁任何形式的转载!本博客只允许放在博客园(.cnblogs.com),如果您在其他网站看到这篇博文,请通过下面这个唯一的合法链接转到原文! 本博客全网唯一合法UR ...
- init,service和systemctl的区别
参考http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html 1.service是一个脚本命令,分析service可知 ...
- [development][PCRE] old PCRE
介绍, man手册 txt版 http://www.pcre.org/original/pcre.txt html版 http://www.pcre.org/original/doc/html/pcr ...
- Copycat - StateMachine
看下用户注册StateMachine的过程, CopycatServer.Builder builder = CopycatServer.builder(address); builder.withS ...
- dp单调性优化
跟着书上的思路学习dp的单调性优化觉得还是很容易想的. 数据范围: dp,数据范围是百万,这应该是O(n)的算法了. 首先不难想到设f[i]表示到第i个百米所能达到的最大能量,那么f[n]即为所求. ...
- 内部排序->插入排序->其它插入排序->折半插入排序
文字描述 和直接插入排序比较,只是把“查找”操作利用“折半查找”来实现,由此进行的插入排序叫做折半插入排序. 示意图 略 算法分析 和直接插入排序比,减少了比较次数,但是移动次数没有变,所以折半插入排 ...
- python学习目录(转载)
python基础篇 python 基础知识 python 初始python python 字符编码 python 类型及变量 python 字符串详解 python 列表详解 ...