本文是根据慕课网kotlin相关课程本人笔记和个人理解所作
代码部分参考慕课网新kotlin入门课程
这一章主要是kotlin的类的一些操作,转换、拓展之类的,令我印象深刻的还是智能转换,感觉挺不错的
java
public class SimpleClass
extends AbsClass
implements SimpleInf {
public int x;
public SimpleClass(int x) {
this.x = x;
}
public void y(){
}
@Override
public void simpleMethod() {
}
@Override
public void absMethod() {
}
}
kotlin
open class SimpleClass(var x: Int, val y: String)
: AbsClass(), SimpleInf {
override val simpleProperty: Int
get() {
return 2
}
val z : Long
get() {
return simpleProperty * 2L
}
override fun absMethod() {}
override fun simpleMethod() {}
fun y(){}
fun zzz(string: String){
}
final override fun overridable(){
}
}
java
public abstract class AbsClass {
public abstract void absMethod();
protected void overridable(){ }
public final void nonOverridable(){ }
}
kotlin
abstract class AbsClass {
abstract fun absMethod()
open fun overridable(){}
fun nonOverridable(){}
}
java
public interface SimpleInf {
void simpleMethod();
}
kotlin
interface SimpleInf {
val simpleProperty: Int // property
fun simpleMethod()
}
kotlin
class PoorGuy {
var pocket: Double = 0.0
}
fun PoorGuy.noMoney() {
}
//property = backing field + getter + setter
var PoorGuy.moneyLeft: Double
get() {
return this.pocket
}
set(value) {
pocket = value
}
interface Guy {
var moneyLeft: Double
get() {
return 0.0
}
set(value) {
}
fun noMoney() {
println("no money called.")
}
}
fun String.padding(count: Int, char: Char = ' '): String {
val padding = (1..count).joinToString("") { char.toString() }
return "${padding}${this}${padding}"
}
fun String.isEmail(): Boolean {
return matches(Regex("(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])"))
}
fun String.times(count: Int): String {
return (1..count).joinToString("") { this }
}
fun main() {
"[email protected]".isEmail()
println("Hello".padding(1))
println("*".times(10))
val stringTimes = String::times
val stringTimesBound = "*"::times
arrayOf(1, 3, 3).forEach { }
val x = 2
}
java
public class SimpleClass
extends AbsClass
implements SimpleInf {
public int x;
public SimpleClass(int x) {
this.x = x;
}
public void y(){
}
@Override
public void simpleMethod() {
}
@Override
public void absMethod() {
}
}
kotlin
open class SimpleClass(var x: Int, val y: String)
: AbsClass(), SimpleInf {
override val simpleProperty: Int
get() {
return 2
}
val z : Long
get() {
return simpleProperty * 2L
}
override fun absMethod() {}
override fun simpleMethod() {}
fun y(){}
fun zzz(string: String){
}
final override fun overridable(){
}
}
kotlin
fun main() {
// var nonNull: String = "Hello"
// // nonNull = null
// val length = nonNull.length
var nullable: String? = "Hello"
//val length = nullable?.length
val length = nullable?.length ?: 0 //elvis boolean? a : b
var x: String = "Hello"
var y: String? = "World"
// x = y // Type mismatch
y = x // OK
var a: Int = 2
var b: Number = 10.0
// a = b // Type mismatch
b = a // OK
val person = Person()
val title = person.title
val titleLength = title?.length
val file = File("abc")
val files = file.listFiles()
println(files.size)
}
java
Kotliner kotliner = new Person("benny", 20);
if (kotliner instanceof Person) {
System.out.println(((Person) kotliner).name);
}
kotlin
val kotliner: Kotliner = Person("benny", 20)
if (kotliner is Person) {
println((kotliner as? Person)?.name)
}
var value: String? = null
value = "benny"
if (value != null) {
println(value.length)
}