- Kotlin Programming By Example
- Iyanu Adelekan
- 147字
- 2021-08-27 20:00:12
Creating objects
Declaring an instance of a class—or an object—is similar to declaring a variable. We can create an instance of the HelloPrinter class, as shown in the following code:
val printer = HelloPrinter()
The preceding printer is an instance of the HelloPrinter class. The opening and closing brackets after the HelloPrinter class name are used to invoke the primary constructor of the HelloPrinter class. A constructor is similar to a function. A constructor is a special function that initializes an object of a type.
The function declared within the HelloPrinter class can be invoked directly by the printer object at any point in time:
printer.printHello() // Prints hello
Occasionally, you may require a function to be directly invokable with the class without needing to create an object. This can be done with the use of a companion object.
- WebAssembly實戰
- Dynamics 365 Application Development
- Web開發的貴族:ASP.NET 3.5+SQL Server 2008
- 算法大爆炸:面試通關步步為營
- MySQL數據庫基礎實例教程(微課版)
- Hands-On Natural Language Processing with Python
- Learning Python Design Patterns
- Learning OpenStack Networking(Neutron)(Second Edition)
- Protocol-Oriented Programming with Swift
- 零基礎Java學習筆記
- Spring+Spring MVC+MyBatis從零開始學
- R的極客理想:量化投資篇
- 你必須知道的.NET(第2版)
- 一覽眾山小:ASP.NET Web開發修行實錄
- 歐姆龍PLC編程指令與梯形圖快速入門