Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Properties and Methods | Classes
/
Introduction to Kotlin

bookProperties and Methods

Desliza para mostrar el menú

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

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 4. Capítulo 2

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Sección 4. Capítulo 2
some-alt