HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
In the previous chapter, we created an object of a class, and specified the properties inside the class, like this
In the previous chapter, we created an object of a class, and specified the properties inside the class, like this:
class Car {
var brand = ""
var model = ""
var year = 0
}
fun main() {
val c1 = Car()
c1.brand = "Ford"
c1.model = "Mustang"
c1.year = 1969
}
In Kotlin, there's a faster way of doing this, by using a constructor.
A
constructor is like a special function, and it is defined by using two parantheses ()
after the class name. You can specify the properties inside of the parantheses (like passing
parameters into a regular function).
The constructor will initialize the properties when you create an object of a class. Just remember to specify the type of the property/variable:
class Car(var brand: String, var model: String, var year: Int)
fun main() {
val c1 = Car("Ford", "Mustang", 1969)
}
Now it's even easier to specify multiple objects of one class:
class Car(var brand: String, var model: String, var year: Int)
fun main() {
val c1 = Car("Ford", "Mustang", 1969)
val c2 = Car("BMW", "X5", 1999)
val c3 = Car("Tesla", "Model S", 2020)
}