Skip to content

Commit 02d330b

Browse files
kotlin-samples-pusher-botdkrasnoff
authored andcommitted
test(samples): add new samples
1 parent 7e802df commit 02d330b

11 files changed

+214
-27
lines changed
Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
//sampleStart
2-
class InitOrderDemo(name: String) {
3-
val firstProperty = "First property: $name".also(::println)
4-
5-
init {
6-
println("First initializer block that prints $name")
7-
}
8-
9-
val secondProperty = "Second property: ${name.length}".also(::println)
10-
11-
init {
12-
println("Second initializer block that prints ${name.length}")
13-
}
1+
// Person class with a primary constructor
2+
// that initializes the name property
3+
class Person(val name: String) {
4+
// Class body with age property
5+
var age: Int = 0
146
}
15-
//sampleEnd
167

178
fun main() {
18-
InitOrderDemo("hello")
9+
// Creates an instance of the Person class by calling the constructor
10+
val person = Person("Alice")
11+
12+
// Accesses the instance's properties
13+
println(person.name)
14+
// Alice
15+
println(person.age)
16+
// 0
1917
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Abstract class with a primary constructor that declares name and age
2+
abstract class Person(
3+
val name: String,
4+
val age: Int
5+
) {
6+
// Abstract member
7+
// Doesn't provide implementation,
8+
// and it must be implemented by subclasses
9+
abstract fun introduce()
10+
11+
// Non-abstract member (has an implementation)
12+
fun greet() {
13+
println("Hello, my name is $name.")
14+
}
15+
}
16+
17+
// Subclass that provides an implementation for the abstract member
18+
class Student(
19+
name: String,
20+
age: Int,
21+
val school: String
22+
) : Person(name, age) {
23+
override fun introduce() {
24+
println("I am $name, $age years old, and I study at $school.")
25+
}
26+
}
27+
28+
fun main() {
29+
// Creates an instance of the Student class
30+
val student = Student("Alice", 20, "Engineering University")
31+
32+
// Calls the non-abstract member
33+
student.greet()
34+
// Hello, my name is Alice.
35+
36+
// Calls the overridden abstract member
37+
student.introduce()
38+
// I am Alice, 20 years old, and I study at Engineering University.
39+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Class with a primary constructor that declares the name property
2+
class Person(
3+
val name: String
4+
) {
5+
// Class body with a companion object
6+
companion object {
7+
fun createAnonymous() = Person("Anonymous")
8+
}
9+
}
10+
11+
fun main() {
12+
// Calls the function without creating an instance of the class
13+
val anonymous = Person.createAnonymous()
14+
println(anonymous.name)
15+
// Anonymous
16+
}
Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
//sampleStart
2-
class Constructors {
3-
init {
4-
println("Init block")
5-
}
6-
7-
constructor(i: Int) {
8-
println("Constructor $i")
9-
}
10-
}
11-
//sampleEnd
1+
// Class header with a primary constructor
2+
// that initializes name with a default value
3+
class Person(val name: String = "Sebastian")
124

135
fun main() {
14-
Constructors(1)
6+
// Creates an instance using the default constructor's value
7+
val anonymousUser = Person()
8+
9+
// Creates an instance by passing a specific value
10+
val namedUser = Person("Joe")
11+
12+
// Accesses the instances' name property
13+
println(anonymousUser.name)
14+
// Sebastian
15+
println(namedUser.name)
16+
// Joe
1517
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Class with a primary constructor
2+
// including default values for name and age
3+
class Person(val name: String = "John", var age: Int = 30)
4+
5+
fun main() {
6+
// Creates an instance using default values
7+
val person = Person()
8+
println("Name: ${person.name}, Age: ${person.age}")
9+
// Name: John, Age: 30
10+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Class with a primary constructor
2+
// including default values for name and age
3+
class Person(
4+
val name: String = "John",
5+
var age: Int = 30
6+
) {
7+
// Initializes the description property
8+
// from the primary constructor parameters
9+
val description: String = "Name: $name, Age: $age"
10+
}
11+
12+
fun main() {
13+
// Creates an instance of the Person class
14+
val person = Person()
15+
// Accesses the description property
16+
println(person.description)
17+
// Name: John, Age: 30
18+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Class with a primary constructor that initializes name and age
2+
class Person(val name: String, var age: Int) {
3+
init {
4+
// Initializer block runs when an instance is created
5+
println("Person created: $name, age $age.")
6+
}
7+
}
8+
9+
fun main() {
10+
// Creates an instance of the Person class
11+
Person("John", 30)
12+
// Person created: John, age 30.
13+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//sampleStart
2+
// Class with a primary constructor that initializes name and age
3+
class Person(val name: String, var age: Int) {
4+
// First initializer block
5+
init {
6+
// Runs first when an instance is created
7+
println("Person created: $name, age $age.")
8+
}
9+
10+
// Second initializer block
11+
init {
12+
// Runs after the first initializer block
13+
if (age < 18) {
14+
println("$name is a minor.")
15+
} else {
16+
println("$name is an adult.")
17+
}
18+
}
19+
}
20+
21+
fun main() {
22+
// Creates an instance of the Person class
23+
Person("John", 30)
24+
// Person created: John, age 30.
25+
// John is an adult.
26+
}
27+
//sampleEnd
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Class header with a primary constructor that initializes name and age
2+
class Person(val name: String, var age: Int) {
3+
4+
// Secondary constructor that takes age as a
5+
// String and converts it to an Int
6+
constructor(name: String, age: String) : this(name, age.toIntOrNull() ?: 0) {
7+
println("$name created with converted age: $age")
8+
}
9+
}
10+
11+
fun main() {
12+
// Uses the secondary constructor with age as a String
13+
Person("Bob", "8")
14+
// Bob created with converted age: 8
15+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Class header with a primary constructor that initializes name and age
2+
class Person(
3+
val name: String,
4+
var age: Int
5+
) {
6+
// Secondary constructor with direct delegation
7+
// to the primary constructor
8+
constructor(name: String) : this(name, 0) {
9+
println("Person created with default age: $age and name: $name.")
10+
}
11+
12+
// Secondary constructor with indirect delegation:
13+
// this("Bob") -> constructor(name: String) -> primary constructor
14+
constructor() : this("Bob") {
15+
println("New person created with default age: $age and name: $name.")
16+
}
17+
}
18+
19+
fun main() {
20+
// Creates an instance based on the direct delegation
21+
Person("Alice")
22+
// Person created with default age: 0 and name: Alice.
23+
24+
// Creates an instance based on the indirect delegation
25+
Person()
26+
// Person created with default age: 0 and name: Bob.
27+
// New person created with default age: 0 and name: Bob.
28+
}

0 commit comments

Comments
 (0)