- Kotlin for Enterprise Applications using Java EE
- Raghavendra Rao K
- 185字
- 2021-06-10 18:49:22
Classes in Kotlin
Classes in Kotlin are created using the class keyword, which is depicted as follows:
class User {
}
The structure of the class is as follows:
Class className {
//properties and constructor
//member functions
}
Let's write a class for User.
Consider the following code for 14_Class.kts:
class User {
var name = "George"
}
val user = User()
println(user.name)
The output is as follows:
This class has a property called name. Classes are instantiated with just the class name, followed by parentheses. Note that there is no new keyword in Kotlin to create an object, unlike in Java.
Classes in Kotlin are final by default. We cannot extend from a class directly. To inherit from the classes, we have to open the classes explicitly. Classes need to have an open keyword in their declaration, as shown in the following code:
open class Person {
}
Alternatively, we can use all-open compiler plugins to make all the classes extendable or accessible to frameworks such as JPA and Spring. We will discuss compiler plugins further in the next chapter.
- Kibana Essentials
- 區(qū)塊鏈架構(gòu)與實現(xiàn):Cosmos詳解
- Visual FoxPro 程序設(shè)計
- 程序員面試算法寶典
- Mastering Ubuntu Server
- Apache Karaf Cookbook
- Flux Architecture
- 執(zhí)劍而舞:用代碼創(chuàng)作藝術(shù)
- Mobile Device Exploitation Cookbook
- Swift語言實戰(zhàn)晉級
- SQL Server 入門很輕松(微課超值版)
- Simulation for Data Science with R
- Visual C++從入門到精通(第2版)
- Java從入門到精通(視頻實戰(zhàn)版)
- Java編程指南:語法基礎(chǔ)、面向?qū)ο蟆⒑瘮?shù)式編程與項目實戰(zhàn)