Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
//sampleStart
class InitOrderDemo(name: String) {
val firstProperty = "First property: $name".also(::println)

init {
println("First initializer block that prints $name")
}

val secondProperty = "Second property: ${name.length}".also(::println)

init {
println("Second initializer block that prints ${name.length}")
}
// Person class with a primary constructor
// that initializes the name property
class Person(val name: String) {
// Class body with age property
var age: Int = 0
}
//sampleEnd

fun main() {
InitOrderDemo("hello")
// Creates an instance of the Person class by calling the constructor
val person = Person("Alice")

// Accesses the instance's properties
println(person.name)
// Alice
println(person.age)
// 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Abstract class with a primary constructor that declares name and age
abstract class Person(
val name: String,
val age: Int
) {
// Abstract member
// Doesn't provide implementation,
// and it must be implemented by subclasses
abstract fun introduce()

// Non-abstract member (has an implementation)
fun greet() {
println("Hello, my name is $name.")
}
}

// Subclass that provides an implementation for the abstract member
class Student(
name: String,
age: Int,
val school: String
) : Person(name, age) {
override fun introduce() {
println("I am $name, $age years old, and I study at $school.")
}
}

fun main() {
// Creates an instance of the Student class
val student = Student("Alice", 20, "Engineering University")

// Calls the non-abstract member
student.greet()
// Hello, my name is Alice.

// Calls the overridden abstract member
student.introduce()
// I am Alice, 20 years old, and I study at Engineering University.
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Class with a primary constructor that declares the name property
class Person(
val name: String
) {
// Class body with a companion object
companion object {
fun createAnonymous() = Person("Anonymous")
}
}

fun main() {
// Calls the function without creating an instance of the class
val anonymous = Person.createAnonymous()
println(anonymous.name)
// Anonymous
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
//sampleStart
class Constructors {
init {
println("Init block")
}

constructor(i: Int) {
println("Constructor $i")
}
}
//sampleEnd
// Class header with a primary constructor
// that initializes name with a default value
class Person(val name: String = "Sebastian")

fun main() {
Constructors(1)
// Creates an instance using the default constructor's value
val anonymousUser = Person()

// Creates an instance by passing a specific value
val namedUser = Person("Joe")

// Accesses the instances' name property
println(anonymousUser.name)
// Sebastian
println(namedUser.name)
// Joe
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Class with a primary constructor
// including default values for name and age
class Person(val name: String = "John", var age: Int = 30)

fun main() {
// Creates an instance using default values
val person = Person()
println("Name: ${person.name}, Age: ${person.age}")
// Name: John, Age: 30
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Class with a primary constructor
// including default values for name and age
class Person(
val name: String = "John",
var age: Int = 30
) {
// Initializes the description property
// from the primary constructor parameters
val description: String = "Name: $name, Age: $age"
}

fun main() {
// Creates an instance of the Person class
val person = Person()
// Accesses the description property
println(person.description)
// Name: John, Age: 30
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Class with a primary constructor that initializes name and age
class Person(val name: String, var age: Int) {
init {
// Initializer block runs when an instance is created
println("Person created: $name, age $age.")
}
}

fun main() {
// Creates an instance of the Person class
Person("John", 30)
// Person created: John, age 30.
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//sampleStart
// Class with a primary constructor that initializes name and age
class Person(val name: String, var age: Int) {
// First initializer block
init {
// Runs first when an instance is created
println("Person created: $name, age $age.")
}

// Second initializer block
init {
// Runs after the first initializer block
if (age < 18) {
println("$name is a minor.")
} else {
println("$name is an adult.")
}
}
}

fun main() {
// Creates an instance of the Person class
Person("John", 30)
// Person created: John, age 30.
// John is an adult.
}
//sampleEnd
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Class header with a primary constructor that initializes name and age
class Person(val name: String, var age: Int) {

// Secondary constructor that takes age as a
// String and converts it to an Int
constructor(name: String, age: String) : this(name, age.toIntOrNull() ?: 0) {
println("$name created with converted age: $age")
}
}

fun main() {
// Uses the secondary constructor with age as a String
Person("Bob", "8")
// Bob created with converted age: 8
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Class header with a primary constructor that initializes name and age
class Person(
val name: String,
var age: Int
) {
// Secondary constructor with direct delegation
// to the primary constructor
constructor(name: String) : this(name, 0) {
println("Person created with default age: $age and name: $name.")
}

// Secondary constructor with indirect delegation:
// this("Bob") -> constructor(name: String) -> primary constructor
constructor() : this("Bob") {
println("New person created with default age: $age and name: $name.")
}
}

fun main() {
// Creates an instance based on the direct delegation
Person("Alice")
// Person created with default age: 0 and name: Alice.

// Creates an instance based on the indirect delegation
Person()
// Person created with default age: 0 and name: Bob.
// New person created with default age: 0 and name: Bob.
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Class header with no primary constructor
class Person {
// Initializer block runs when an instance is created
init {
// Runs before the secondary constructor
println("1. First initializer block runs")
}

// Secondary constructor that takes an integer parameter
constructor(i: Int) {
// Runs after the initializer block
println("2. Person $i is created")
}
}

fun main() {
// Creates an instance of the Person class
Person(1)
// 1. First initializer block runs
// 2. Person 1 created
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
fun main() {
//sampleStart
println("""
Not
trimmed
text
"""
)
println("""
Not
trimmed
text
"""
)

println("""
Trimmed
text
""".trimIndent()
)
println("""
Trimmed
text
""".trimIndent()
)

println()
println()

val a = """Trimmed to margin text:
|if(a > 1) {
| return a
|}""".trimMargin()
val a = """Trimmed to margin text:
|if(a > 1) {
| return a
|}""".trimMargin()

println(a)
//sampleEnd
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
data class Employee(val name: String, val roles: MutableList<String>)

fun main() {
val original = Employee("Jamie", mutableListOf("developer"))
val duplicate = original.copy()

duplicate.roles.add("team lead")

println(original)
// Employee(name=Jamie, roles=[developer, team lead])
println(duplicate)
// Employee(name=Jamie, roles=[developer, team lead])
}
Loading
Loading