-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Intermediate tour: Classes chapter #4564
base: intermediate-tour-staging
Are you sure you want to change the base?
Intermediate tour: Classes chapter #4564
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huge work, maybe the most the useful content on the website
My only concern again is the size of these chapters
I think It could be three pages, for example
- Classes and interfaces 2. Objects 3. Open and other special classes
the 3rd page could be even a part of the Advanced tour
abstract class Animal | ||
``` | ||
|
||
To declare a function **without** an implementation, you also use the `abstract` keyword: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it's too detailed (after all, it's an intermediate tour) and can be shortened to
To declare a function or a property without implementation, you also use the
abstract
keyword:
with one code example for both
to define different product categories: | ||
|
||
```kotlin | ||
abstract class Product( val name: String, var price: Double ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here and in the code blocks below, spaces are unnecessary around the class properties inside the brackets
Although abstract classes are great for sharing code in this way, they are restricted by the fact that classes in Kotlin | ||
only support single inheritance. If you need to inherit from multiple sources, consider using interfaces. | ||
|
||
For more information about abstract classes, see [Abstract classes](classes.md#abstract-classes). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For more information about abstract classes, see [Abstract classes](classes.md#abstract-classes). | |
For more information, see [Abstract classes](classes.md#abstract-classes). |
In the example: | ||
|
||
* `PaymentMethod` is an interface that has an `initiatePayment()` function without an implementation. | ||
* `PaymentType` is an interface that has a `paymentType` property that isn't initialized. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* `PaymentType` is an interface that has a `paymentType` property that isn't initialized. | |
* `PaymentType` is an interface that has the `paymentType` property that isn't initialized. |
|
||
In the example: | ||
|
||
* `PaymentMethod` is an interface that has an `initiatePayment()` function without an implementation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* `PaymentMethod` is an interface that has an `initiatePayment()` function without an implementation. | |
* `PaymentMethod` is an interface that has the `initiatePayment()` function without an implementation. |
|
||
Enum classes can have properties and member functions just like normal classes. | ||
|
||
For example, let's say that you are working with HTML and you want to create an enum class that contains some colors. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example, let's say that you are working with HTML and you want to create an enum class that contains some colors. | |
For example, let's say you're working with HTML and want to create an enum class containing some colors. |
In this example, the `containsRed()` member function accesses the value of the enum constant's `rgb` property using the | ||
`this` keyword, and checks if the hexadecimal value contains `FF` as its first bits to return a boolean value. | ||
|
||
For more information about enum classes, see [Enum classes](enum-classes.md). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For more information about enum classes, see [Enum classes](enum-classes.md). | |
For more information, see [Enum classes](enum-classes.md). |
``` | ||
|
||
In this example, the `containsRed()` member function accesses the value of the enum constant's `rgb` property using the | ||
`this` keyword, and checks if the hexadecimal value contains `FF` as its first bits to return a boolean value. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
`this` keyword, and checks if the hexadecimal value contains `FF` as its first bits to return a boolean value. | |
`this` keyword and checks if the hexadecimal value contains `FF` as its first bits to return a boolean value. |
### Inline value classes | ||
|
||
Sometimes in your code, you may want to create small objects from classes and use them only briefly. This approach can | ||
have a performance impact. Inline value classes are a special type of class that avoid this performance impact. However, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
have a performance impact. Inline value classes are a special type of class that avoid this performance impact. However, | |
have a performance impact. Inline value classes are a special type of class that avoids this performance impact. However, |
* Creates an instance of the `Email` class called `email`. | ||
* Calls the `sendEmail()` function on the `email` object. | ||
|
||
By using an inline value class, the class is inlined and used directly in your code without creating an object. This can |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By using an inline value class, the class is inlined and used directly in your code without creating an object. This can | |
By using an inline value class, you make the class inlined and can use it directly in your code without creating an object. This can |
This PR is to add the second chapter of the intermediate tour to the staging branch.