Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Properties and Methods | Classes
Practice
Projects
Quizzes & Challenges
Вікторини
Challenges
/
Introduction to Kotlin

bookProperties and Methods

Свайпніть щоб показати меню

When you define a class in Kotlin, you can give it properties and methods. Properties are variables that hold data about each object created from the class. Methods are functions that describe what the object can do. By combining properties and methods, you can model real-world things and their behaviors in your code.

Person.kt

Person.kt

copy
1234567891011121314151617
package com.example class Person { var name: String = "" var age: Int = 0 fun greet() { println("Hello, my name is $name and I am $age years old.") } } fun main() { val person = Person() person.name = "Alice" person.age = 28 person.greet() }

In this example, the Person class has two properties: name and age. These properties store information about each Person object. The greet method is a function inside the class that prints a message using the properties. When you create a new Person and assign values to name and age, you can call greet() to see a personalized greeting. You access properties with dot notation, like person.name, and call methods the same way, like person.greet().

question mark

Which statement about properties and methods in Kotlin classes is correct?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 2

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Секція 4. Розділ 2
some-alt