- 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.
- UI圖標創意設計
- JBoss Weld CDI for Java Platform
- JavaScript前端開發模塊化教程
- 青少年美育趣味課堂:XMind思維導圖制作
- 基于免疫進化的算法及應用研究
- 薛定宇教授大講堂(卷Ⅳ):MATLAB最優化計算
- Data Analysis with Stata
- Symfony2 Essentials
- jQuery Mobile移動應用開發實戰(第3版)
- Kotlin開發教程(全2冊)
- 寫給程序員的Python教程
- 現代C:概念剖析和編程實踐
- Keil Cx51 V7.0單片機高級語言編程與μVision2應用實踐
- 金融商業數據分析:基于Python和SAS
- PHP高性能開發:基礎、框架與項目實戰