//java.lang.Integr Int--->int
var intNumber:Number = 39
//java.lang.Float Float--->float
var floatNumber:Number = 39.0f
//java.lang.Double Double--->double
var doubleNumber:Number = 39.000000000000000
//java.lang.Long Long--->long
var longNumber:Number = 39L
2.1.Kotlin预定义的基本数据类型
var intType:Int = 39 //int
var shortType:Short = 39 //short
var byteType:Byte = 0 //byte
var boolType:Boolean = true //boolean
var stringType:String = "miku" //String
var floatType:Float = 39f //float
var doubleType:Double = 39.0000000000 //double
var longType:Long = 39L //long
var numberType:Number = 39 //Integr、Float、Double、Long
var charType:Char = '0' //char
字符串拼接,通过${}直接引用变量,不再需要引号 + 引号的方式
val name = "komine"
val text = "私の名前は${name}と申します。"
print(text)
2.2.元组
元组是一个固定长度,且不能被修改的数据结构
//二阶元组
var person1 = Pair("komin",16)
//三阶元组
var person2 = Triple("komine",18,"女")
person2.first //第一个元素的值
person2.second //第二个元素的值
person2.third //第三个元素的值
public class MyPair<First,Second> {
private final First mFirstValue;
private final Second mSecond;
public MyPair(First first, Second second){
this.mFirstValue = first;
this.mSecond = second;
}
public First getFirst() {
return mFirstValue;
}
public Second getSecond() {
return mSecond;
}
}
var str:String? = null //在类型的后面添加?表示该类型可空
print(str?.length) //通过?.方式来访问可空类型的成员方法或变量,如果str是null则后面的调用会返回null,不会报错
//如果你明确知道,str在某个时候肯定不为空,可以通过!!操作该变量,但如果str为空则会抛出空指针异常
print(str!!.length)
2.4.数组
在Kotlin中定义一个数组非常简单.调用arrayOf()即可创建一个数组.
var names = arrayOf("miku","rin","luka")
var numbers = arrayOf(16,14,17)
//跟Java一样,也可以通过[下标]的方式访问数组的元素
print(names[0])
//创建一个指定长度的数组,值为null
var fixedArray = arrayOfNulls<Int>(39)
//创建一个空数组
var emptyArray = emptyArray<Int>()
//MutableList集合,该集合可以包含相同元素
var mutableList = mutableListOf("miku","rin","luka")
//键值对,包含Key和Value的集合
var mutableMap = mutableMapOf<String,String>()
var linkedHashMap = linkedMapOf<String,String>() //等价mutableMap
//MutableSet集合,该集合不会出现相同的元素,如果集合已经包含某个值,添加的时候会忽略添加操作
var mutableSet = mutableSetOf<Object>()
var linkedSet = linkedSetOf<String>() //等价mutableSet
不可变集合
//List集合,无法进行添加操作
var list = listOf("miku","rin","luka")
//Map集合,无法进行添加操作
var map = mapOf(Pair("miku",16))
var linkedMap =
//Set集合,无法进行添加操作
var set = setOf<Int>(0,1,2,3,4,5,6,7,8,9)
var mutableList = mutableListOf("miku","rin","luka")
val result:Boolean = mutableList.any(){
it == "miku" //有一个元素的值为miku
}
all 判断集合中的所有元素是都否满足条件
var mutableList = mutableListOf("miku","rin","luka")
val result:Boolean = mutableList.all(){
it.isNotEmpty() //元素的长度大于0
}
none 判断集合中的所有元素是否都不满足条件,满足则返回true
var mutableList = mutableListOf("miku","rin","luka")
val result:Boolean = mutableList.none(){
it.isNotEmpty() //元素的长度等于0 false
}
count 返回满足条件的元素个数,类似sql中的select count(*) from table where xx = xx
var mutableList = mutableListOf("miku","rin","luka")
val result: Int = mutableList.count {
it == "miku" //返回集合中元素值为miku的元素个数
}
reduce 从第一个元素到最后一个元素的累加
val numbers = mutableListOf(1,2,3,4,5,6,7,8,9)
val result: Int = numbers.reduce() {
sum: Int, i -> sum + i //sum:声明一个用于接收累加结果的变量 i->:循环每一个元素,sum + i:累加每个元素到sum
}
reduceRight 从最后一个元素到第一个元素的累加
val numbers = mutableListOf(1,2,3,4,5,6,7,8,9)
val result: Int = numbers.reduceRight() {
sum: Int, i -> sum + i
}
fold 跟reduce类似,不过可以设置初始化,从初始值开始累加
val numbers = mutableListOf(1,2,3,4,5,6,7,8,9)
val result: Int = numbers.fold(10){
sum, i -> sum + i
}
foldRight...
sumOf 返回集合中所有元素的总和,该集合的元素必须是Number类型
val numbers = mutableListOf(1,2,3,4,5,6,7,8,9)
val result = numbers.sumOf{
it
}
dropWhile 去除满足条件的元素,直到不满足为止,返回剩余元素的集合
val numbers = mutableListOf(1,2,3,4,5,6,7,8,9)
val result = numbers.dropWhile {
it < 5
}
filter 过滤不满足条件的元素,返回满足元素的新集合
val numbers = mutableListOf(1,2,3,4,5,6,7,8,9)
val result = numbers.filter {
it < 5
}
filterNot 过滤满足条件的元素,返回不满足元素的新集合
val numbers = mutableListOf(1,2,3,4,5,6,7,8,9)
val result = numbers.filterNot {
it < 5
}
take 返回从第一个元素开始的N个元素
val numbers = mutableListOf(1,2,3,4,5,6,7,8,9)
val result = numbers.take(5) //{1,2,3,4,5}
val numbers = mutableListOf(1,2,3,4,5,6,7,8,9)
val result = numbers.takeWhile {
it > 0
}
drop 返回去掉N个元素之后的列表
val numbers = mutableListOf(1,2,3,4,5,6,7,8,9)
val result = numbers.drop(1)
dropLastWhile 返回从最后一个元素开始,去掉满足条件的元素
val numbers = mutableListOf(1,2,3,4,5,6,7,8,9)
val result = numbers.dropLastWhile {
it > 5
}
slice 保留指定下标对应的元素
val numbers = mutableListOf(1,2,3,4,5,6,7,8,9)
val result = numbers.slice(listOf(1,2,3))
map 将集合中的元素通过某种方法转换后,返回一个新集合
val numbers = mutableListOf(1,2,3,4,5,6,7,8,9)
val result = numbers.map {
val diff = 10
it * diff
}
后续更新... 2.6.类型转换
Kotlin通过as关键字将一个类型转换为另一个类型.
var numberType:Number = 39
var intType:Int = numberType as Int
Kotlin可以通过 is 关键字自动完成装箱的操作.
open class GameConsole{
}
class PS4 : GameConsole() {
}
class NintendoSwitch : GameConsole() {
public val version = "10.0"
}
fun main(args: Array<String>) {
val gameConsoles = arrayOf(PS4(),NintendoSwitch())
val gameConsole = gameConsoles[1]
//会自动完成装箱操作,在当前调用范围内将gameConsole识别NitendoSwitch,可以直接访问对象的成员,不需要手动转换
if(gameConsole is NintendoSwitch){
print(gameConsole.version)
}
}
var obj1 = Object()
var obj2 = Object()
if(obj1 === obj2){
println("恒等")
}
if(obj1 !== obj2){
println("非恒等")
}
3.2.位运算
只有Int和Long类型可以使用.
val i:Int = 10
val j:Int = 20
val result = i shl j //有符号左移
result = i shr j //有符号右移
result = i ushr //无符号右移
result = i and j //按位与
result = i or j //按位或
result = i xor //按位异或
result = i inv j //按位取反
3.3.区间运算符
表示某个值是否在某个范围或者集合之中.
//i >=0 && i <= 100
if(i in 0..100){
print(i)
}
数组或者集合是否包含某个值
val names = arrayOf("miku","rin","luka")
if("miku" in names){
print("包含")
}
val names = arrayOf("miku","rin","luka")
for (name in names){
println(name)
}
//遍历数组或集合带索引
for ((index,name) in names.withIndex()){
}
val i = 0
for (i in 0..99){ //i = 0;i <= 99;i++
println(i)
}
var i = 10
while (i > 0){
println(i)
i--
}
do {
i--
println(i)
}while (i > 0)
//foreach
val numbers = mutableListOf(1,2,3,4,5,6,7,8,9)
numbers.forEach{ it ->
println(it)
}
//foreach 带索引
val numbers = mutableListOf(1,2,3,4,5,6,7,8,9)
numbers.forEachIndexed{index, i ->
println("index[${index}] = $i")
}
6.函数
Kotlin中定义函数使用fun关键字.如果方法不需要返回值可以不写,或者写Unit
fun sum(number1:Int,number2:Int):Int{
return number1 + number2
}
fun main(args: Array<String>) {
call(::method)
}
fun call(m:(number:Int) -> Int){
println(m(39))
}
fun method(number:Int):Int{
return number * number
}
fun main(args: Array<String>) {
test()
}
inline fun test(){
for (i in 0..99){
println(i)
}
for (i in 0..99){
println(i)
}
for (i in 0..99){
println(i)
}
for (i in 0..99){
println(i)
}
for (i in 0..99){
println(i)
}
for (i in 0..99){
println(i)
}
}
fun main(){
val ps4List = mutableListOf<PS4>()
setGameConsoles(ps4List)
val gameConsoles = getGameConsoles()
for (gameConsole in gameConsoles){
val ps4 = gameConsole as PS4
ps4.play()
}
}
// out ----> ? extends GamesConsole
fun setGameConsoles(gamConsoles:MutableList<out GameConsole>){
}
//in -----> ? super PS4
fun getGameConsoles():MutableList<in PS4>{
val gameConsoles = mutableListOf<GameConsole>()
gameConsoles.add(PS4())
gameConsoles.add(PS4())
return gameConsoles
}
open class GameConsole{
open fun play(){
}
}
class PS4 : GameConsole(){
override fun play() {
println("ps4 play...")
}
}
class NintendoSwitch : GameConsole(){
override fun play() {
println("ns play...")
}
}
4.委托
其实就是代理设计模式. 4.1委托的使用场景
Java中的代理模式实现
//支付接口
public interface IPay {
void pay();
}
//支付宝支付
public class AliPay implements IPay{
private float mMoney;
public AliPay(float money){
mMoney = money;
}
@Override
public void pay() {
Log.d("Alipay","支付宝支付" + mMoney + "元...");
}
}
//微信支付
public class WeChatPay implements IPay{
private float mMoney;
public WeChatPay(float money){
mMoney = money;
}
@Override
public void pay() {
Log.d("WeChatPay","微信支付" + mMoney + "元...");
}
}
//代理对象
public class ProxyPay implements IPay{
private IPay mPay;
public ProxyPay(IPay pay){
mPay = pay;
}
@Override
public void pay() {
mPay.pay();
}
}
Kotlin的代理模式实现
interface IPay{
fun pay()
}
class Alipay(private val money:Float) :IPay{
override fun pay() {
println("支付宝支付$money...")
}
}
class WeChatPay(private val money: Float) :IPay{
override fun pay() {
println("微信支付$money...")
}
}
class PayDelegate(private val play:IPay) :IPay by play{
//什么都不需要做,系统帮我们完成一系列操作
}
//声明一个接口,不一定要接口也可以是一个类
interface IPropertyDelegate
class PS4 :IPropertyDelegate{
var version:String by DataDelegate()
}
class NintendoSwitch :IPropertyDelegate{
var version:String by DataDelegate()
}
class DataDelegate {
private var version:String = ""
operator fun setValue(thisRef: IPropertyDelegate, property: KProperty<*>, value: String) {
this.version = value
}
operator fun getValue(thisRef: IPropertyDelegate, property: KProperty<*>): String {
return this.version
}
}