You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
_Data classes_ make it easy to declare classes that are used to store some values.
4
-
Everything that's needed to use them in collections, have a useful string
5
-
representation, and create copies is auto-generated.
3
+
[Data classes](https://kotlinlang.org/docs/reference/data-classes.html) make it easy to create classes that are used to store values. Such classes are automatically provided with methods for copying, getting a string representation, and using instances in collections.
Enum classes are used to model types that represent a finite set of distinct values, such as directions, states, modes and so forth.
4
-
5
-
They may contain properties and methods like other classes, separated from the enum instances by a semicolon.
3
+
[Enum classes](https://kotlinlang.org/docs/reference/enum-classes.html) are used to model types that represent a finite set of distinct values, such as directions, states, modes, and so forth.
[Sealed classes](https://kotlinlang.org/docs/reference/sealed-classes.html) let you restrict the use of inheritance. Once you declare a class sealed, nobody else can create its subclasses.
isHuman->return"Hello ${mammal.name}; You're working as a ${mammal.job}"// 3
14
-
isCat->return"Hello ${mammal.name}"// 4
15
-
// 5
16
-
}
14
+
when (mammal) { // 3
15
+
isHuman->return"Hello ${mammal.name}; You're working as a ${mammal.job}"// 4
16
+
isCat->return"Hello ${mammal.name}"// 5
17
+
} // 6
17
18
}
18
19
19
20
funmain() {
@@ -24,11 +25,11 @@ fun main() {
24
25
</div>
25
26
26
27
27
-
1.A sealed class is defined with two subclasses which must be in the same file
28
-
2.The sealed class is used as an argument for a *when* expression
29
-
3.A smartcast is performed, casting Mammal to Human
30
-
4. A smartcast is performed, casting Mammal to Cat
31
-
5.The *else* clause is not necessary because all subclasses of the sealed class are covered.
32
-
This is contrary to ordinary super classes, where an *else* would always be needed.
28
+
1.Defines a sealed class.
29
+
2.Defines subclasses. Note that all subclasses must be in the same file.
30
+
3.Uses an instance of the sealed class as an argument in a `when` expression.
31
+
4. A smartcast is performed, casting `Mammal` to `Human`.
32
+
5.A smartcast is performed, casting `Mammal` to `Cat`.
33
+
6. The `else`-case is not necessary here since all possible subclasses of the sealed class are covered. With a non-sealed superclass `else` would be required.
First of all, let's start with some basic OOP concepts: a *class* is a blueprint, and an *object* is an instance of a class.
4
-
You define a class, then create multiple instances of that class:
3
+
Classes and objects in Kotlin work the same way as in most object-oriented languages: a *class* is a blueprint, and an *object* is an instance of a class. Usually, you define a class and then create multiple instances of that class:
Easy: we create two objects, both *instances* of LuckDispatcher class.
33
+
In Kotlin you also have the [**object** keyword](https://kotlinlang.org/docs/reference/object-declarations.html). It is used to obtain a *data type with a single implementation*.
35
34
36
-
In Kotlin you have also an **object** keyword. What is? is a *data type with a single implementation*.
35
+
If you are a Java user and want to understand what "*single*" means, you can think of the **Singleton** pattern:
36
+
it ensures you that only one instance of that class is created even if 2 threads try to create it.
37
37
38
-
If you are a Java user and want to understand what "*single*" means, you can think to Singleton pattern:
39
-
it allows you to check that one (and only one) instance of that class will be created, even if 2 threads access it.
38
+
To achieve this in Kotlin, you only need to declare an `object`: no class, no constructor, only a lazy instance.
39
+
Why lazy? Because it will be created once when the object is accessed. Otherwise, it won't even be created.
40
40
41
-
To achieve that in Kotlin, you only need to declare an **object**: no class, no constructor, only a lazy instance.
42
-
Why lazy? because it will be created one time, if object is used, otherwise, no.
41
+
### `object` Expression
43
42
44
-
In this example, you see a typical basic usage of an **objectexpression**: a simple object/properties structure.
45
-
No need of class declaration: create a single object, declare members and access it.
46
-
Object like that, is often used like anonymous class in Java.
43
+
Here is a basic typical usage of an `object`**expression**: a simple object/properties structure.
44
+
There is no need to do so in class declaration: you create a single object, declare its members and access it within one function.
45
+
Objects like this are often created in Java as anonymous class instances.
@@ -62,22 +61,22 @@ fun rentPrice(standardDays: Int, festivityDays: Int, specialDays: Int): Unit {
62
61
63
62
}
64
63
65
-
66
-
funmain(){
64
+
funmain() {
67
65
rentPrice(10, 2, 1) //5
68
66
}
69
67
```
70
68
71
69
</div>
72
70
73
-
1.Create a rentPrice function, w/ parameters (regular days, festivity days, special days)
74
-
2.Create rates object, where you set vars values
75
-
3.Access object's vars
76
-
4.Print total
77
-
5.Access the instance (initialization), calling fun
71
+
1.Creates a function with parameters.
72
+
2.Creates an object to use when calculating the result value.
73
+
3.Accesses the object's properties.
74
+
4.Prints the result.
75
+
5.Calls the function. This is when the object is actually created.
78
76
77
+
### `object` Declaration
79
78
80
-
You can have also **objectdeclaration**: is not an expression, and cannot be used in a variable assignment, must be used directly:
79
+
You can also use the `object`**declaration**. It isn't an expression, and can't be used in a variable assignment. You should use it to directly access its members:
0 commit comments