Kotlin-属性-接口-修饰符-数据类
来源:互联网 发布:下载软件的软件有哪些 编辑:程序博客网 时间:2024/05/21 17:25
属性定义:
var 可变变量
val 只读变量,不可再赋值
class Address { var name: String = ... var street: String = ... var city: String = ... var state: String? = ... var zip: String = ...}
get和set方法
关键字 field,在get和set方法中获取属性值和重新赋值
var <propertyName>[: <PropertyType>] [= <property_initializer>] [<getter>] [<setter>]实例代码: var name: String?="" get() = field set(value) { field = value println("set========================"+value) } var age = 0 set(value) { if (value>0) field = value }
get or set方法的修饰符
var setterVisibility: String = "abc" private set // the setter is private and has the default implementationvar setterWithAnnotation: Any? = null @Inject set // annotate the setter with Inject
Backing Properties
private var _table: Map<String, Int>? = nullpublic val table: Map<String, Int> get() { if (_table == null) { _table = HashMap() // Type parameters are inferred } return _table ?: throw AssertionError("Set to null by another thread") }
常量:
const关键字
要求:
1,Top-level or member of an object 2,Initialized with a value of type String or a primitive type 3,No custom getter
const val SUBSYSTEM_DEPRECATED: String = "This subsystem is deprecated"class Constant{ fun method() { println("-----------constant-----------") }}使用的时候:导包:import com.heaven.kotlin.demo.entity.SUBSYSTEM_DEPRECATED println(SUBSYSTEM_DEPRECATED)
Interfaces 接口
interface 关键字。跟java区别不大
kotlin接口中的方法是可以有实现体的
interface MyInterface { fun bar() fun foo() { // optional body }}
接口中的属性
interface MyInterface { val prop: Int // abstract val propertyWithImplementation: String get() = "foo" fun foo() { print(prop) }}class Child : MyInterface { override val prop: Int = 29}
Visibility Modifiers 修饰符
private, protected, internal and public。默认是public
internal:在整个module都是可用的
Data Classes 数据类
只是存储数据和取值使用
关键字:data
data class User(val name: String, val age: Int)
数据类自动生成如下方法
equals()/hashCode()
toString()方法
componentN()方法 对应属性的顺序
copy()方法
要求:
主构造函数至少有一个参数
主构造函数中的所有参数必须被标记为val或者var
数据类不能有以下修饰符:abstract,inner,open,sealed
data class只能实现接口(Kotlin1.1以前的规则),现在也可以继承其它类
copy赋值:
copy方法是编译器自动生成的fun copy(name: String = this.name, age: Int = this.age) = User(name, age) 以Cinema类举例:data class Cinema(var id:Int,var cinema_name:String) {} var cinema = Cinema(1,"四川太平洋") var cinema2 = cinema.copy(cinema_name = "百老汇") println(cinema2.component1()) // id值 println(cinema2.component2())// cinema_name 值println(cinema2) // toString方法:Cinema(id=1, cinema_name=百老汇)
阅读全文
0 0
- Kotlin-属性-接口-修饰符-数据类
- [kotlin系列] (s2_2)属性、字段、接口、可见性修饰
- 类、属性、方法、接口 的 修饰符、访问修饰符
- Kotlin学习(四)—— 类和对象,继承,覆盖,抽象类,属性和字段,接口,可见性修饰符,扩展
- Kotlin类和对象(三)——接口与访问修饰符
- Kotlin学习笔记(五)接口,可见性修饰符
- Kotlin最简单的入门教程——可见性修饰符,扩展,数据类,密封类
- Kotlin学习(十): 可见性修饰符和数据类
- java中接口的属性、方法的默认修饰符和类的属性、访问修饰符的区别
- java中接口的属性、方法的默认修饰符和类的属性、访问修饰符的区别
- Kotlin基础教程-访问修饰符
- Kotlin 可见性修饰符
- 学习kotlin第八天_接口、可见性修饰符、扩展
- Kotlin类和对象 (四)--- 可见性修饰符
- C#类,接口,类成员字段,方法,属性的访问修饰符
- java的接口、类、属性、方法各有哪些修饰符
- Java细节02——类、接口、构造器、属性、方法的修饰符
- Java的接口、类、属性、方法各有哪些修饰符
- Linux系统编程学习笔记(4)-对终端进行控制
- socket 并发服务器 多线程模式
- bootstrapvalidator.js的学习
- android虚拟机访问本地Tomcat
- iOS-详解KVO底层实现
- Kotlin-属性-接口-修饰符-数据类
- 对需要映射实体类的项目 进行 hibernate 方面的支持
- Ubuntu学习篇:如何提高Ubuntu系统下载软件的速度。
- socket函数一直返回-1的问题
- linux shell基础语法
- Volatile与Synchronized分析
- ZkShell 命令详解
- MyBatis代码生成工具mybatis-generator在Myeclipse10中的使用
- 数据库读写分离及问题