学习一下Kotlin(一)
来源:互联网 发布:mui框架电商app源码 编辑:程序博客网 时间:2024/05/17 03:12
前言
Kotlin出来有几个年头了,不温不火,google粑粑在今年I/O大会上宣布Kotlin成为Android的官方语言(可能是谷歌和甲骨文Java神仙打架后,做出的调整),算是在android开发的老司机和新司机的脑子里都留下了印象。
Class and Objects
( 受面向对象语言的荼毒比较深,我还是比较喜欢先看一下它的Class和Object)
class Hello{ }
这申明熟悉的感觉,没毛病。
构造函数
根据官方文档指出一个class能够有一个primary constructor以及多个secondary constructors (面向对象的一个特征出来了),基本的一个class的构造方法,也就是primary constructo 一个constructor关键字
class Hello constructor(test: String){ }
不过根据官方文档还说明一点:如果primary constructor没有任何的注释说明或者有用的修饰,那么constructor可以省略
class Hello (test:String){}
另外基本的构造方法不能包含任何胆码,我们能够通过init关键字来进行初始化(挺人性化的设计)
class Hello(test:String){ init{ logger.info("Hello init with value ${test}") } }
小提示: 构造方法的变量能够在init初始化的中,也能够通过申明变量引用
class Customer(name: String) { val customerKey = name}
同时也能这么玩
class Person(val firstName: String, val lastName: String, var age: Int) { // ...}
(var和val的区别说变量时再区别,构造方法里变量申明也可以用 val)
上面说的都是没有那些有用的修饰符或者有用的修饰符,如果有那么constructor 就不可以省略,把Visibility Modifiers 搁到constructor之前就可以
class Customer public @Inject constructor(name: String) { ... }
那么被官方成为Secondary Constructors 就是大家熟悉的这样
class Person { constructor(parent: Person) { parent.children.add(this) }}
如果一个class有一个primary constructor,那么每一个secondary constructor需要一个this关键字引用这个变量
class Person(val name: String) { constructor(name: String, parent: Person) : this(name) { parent.children.add(this) }}
如果你的定义一个class 没有构造方法,那么你的constructor就是public ,如果想要定义个私有的构造方法就用private(和Java的一样)
class DontCreateMe private constructor () {}
实例化一个class
kotlin没有new这个关键字,实例化一个类
val hello = Hello() val custom = Customer("hello world");
继承和复写方法
class MyView : View { constructor(ctx: Context) : super(ctx) constructor(ctx: Context, attrs: AttributeSet) : super(ctx, attrs)}
看一下下面的(open关键字,需要继承复写方法用),老司机们看例子,就不用介绍了还是一个override关键字
open class Base { open fun v() {} fun nv() {}}class Derived() : Base() { override fun v() {}}
open class Foo { open val x: Int get { ... }}class Bar1 : Foo() { override val x: Int = ...}
如果进制复写一个方法,那么就引用一个final 关键字
open class AnotherDerived() : Base() { final override fun v() {}}
抽象class
还是 abstract
open class Base { open fun f() {}}abstract class Derived : Base() { override abstract fun f()}
接口
(又是val和var的区别)
interface Foo { val count: Int}class Bar1(override val count: Int) : Fooclass Bar2 : Foo { override var count: Int = 0}
复写规则:
看例子,比口述强,一个super关键字
open class A { open fun f() { print("A") } fun a() { print("a") }}interface B { fun f() { print("B") } // interface members are 'open' by default fun b() { print("b") }}class C() : A(), B { // The compiler requires f() to be overridden: override fun f() { super<A>.f() // call to A.f() super<B>.f() // call to B.f() }}
- 学习一下Kotlin(一)
- Kotlin学习(一)
- kotlin学习(一)
- Kotlin学习(一)
- 学习一下Kotlin(二)
- Kotlin学习(一)Kotlin插件安装
- Kotlin学习笔记(一)
- Kotlin学习笔记(一)
- Kotlin学习笔记(一)
- kotlin学习笔记(一)
- kotlin学习笔记(一)
- Kotlin学习笔记(一)---从零学习Kotlin
- Kotlin学习(一)简单了解下Kotlin
- 一步步学习kotlin for android(一)配置kotlin
- kotlin学习(一)kotlin的基本句法语法
- Android Kotlin学习笔记(一)---初探Kotlin
- Kotlin学习(一)基本语法
- Kotlin学习之路(一)
- dao
- 机器学习简易入门(四)- logistic回归
- JSP 编写的哪一些
- 推荐系统的初体验(关联规则,协同过滤)
- spring和springMVC的简单区别
- 学习一下Kotlin(一)
- AsyncTask源码解析
- VS2017 Asp.net MVC SqliteEF6 调试错误列表
- 仿迅雷播放器教程 -- 提取exe资源
- 函数可以返回局部的指针数组
- python 打开文件错误
- 文章标题 CSU 1846: Assembly line(DP)
- 我也来谈微服务(二)
- 配置IDM不限速下载百度云的大文件