3

Use classes and objects in Kotlin

 2 years ago
source link: https://developer.android.com/codelabs/basic-android-kotlin-compose-classes-and-objects#0
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Use classes and objects in Kotlin

About this codelab

subjectLast updated May 25, 2022
account_circleWritten by Google Developers Training team

1. Before you begin

This codelab teaches you how to use classes and objects in Kotlin.

Classes provide blueprints from which objects can be constructed. An object is an instance of a class that consists of data specific to that object. You can use objects or class instances interchangeably.

As an analogy, imagine that you build a house. A class is similar to an architect's design plan, also known as a blueprint. The blueprint isn't the house; it's the instruction for how to build the house. The house is the actual thing, or object, which is built based on the blueprint.

Just like the house blueprint specifies multiple rooms and each room has its own design and purpose, each class has its own design and purpose. To know how to design your classes, you need to get familiar with object-oriented programming (OOP), a framework that teaches you to enclose data, logic, and behavior in objects.

OOP helps you simplify complex, real-world problems into smaller objects. There are four basic concepts of OOP, each of which you learn more about later in this codelab:

  • Encapsulation. Wraps the related properties and methods that perform action on those properties in a class. For example, consider your mobile phone. It encapsulates a camera, display, memory cards, and several other hardware and software components. You don't have to worry about how components are wired internally.
  • Abstraction. An extension to encapsulation. The idea is to hide the internal implementation logic as much as possible. For example, to take a photo with your mobile phone, all you need to do is open the camera app, point your phone to the scene that you want to capture, and click a button to capture the photo. You don't need to know how the camera app is built or how the camera hardware on your mobile phone actually works. In short, the internal mechanics of the camera app and how a mobile camera captures the photos are abstracted to let you perform the tasks that matter.
  • Inheritance. Enables you to build a class upon the characteristics and behavior of other classes by establishing a parent-child relationship. For example, there are different manufacturers who produce a variety of mobile devices that run Android OS, but the UI for each of the devices is different. In other words, the manufacturers inherit the Android OS feature and build their customizations on top of it.
  • Polymorphism. The word is an adaptation of the Greek root poly-, which means many, and -morphism, which means forms. Polymorphism is the ability to use different objects in a single, common way. For example, when you connect a Bluetooth speaker to your mobile phone, the phone only needs to know that there's a device that can play audio over Bluetooth. However, there are a variety of Bluetooth speakers that you can choose from and your phone doesn't need to know how to work with each of them specifically.

Lastly, you learn about property delegates, which provide reusable code to manage property values with a concise syntax. In this codelab, you learn these concepts when you build a class structure for a smart-home app.

Note: Smart devices make our lives convenient and easier. There are many smart-home solutions available on the market that let you control smart devices with your smartphone. With a single tap on your mobile device, you can control a variety of devices, such as smart TVs, lights, thermostats, and other household appliances.

Prerequisites

  • How to open, edit, and run code in Kotlin Playground.
  • Knowledge of Kotlin programming basics, including variables, functions, and the println() and main() functions

What you'll learn

  • An overview of OOP
  • What classes are.
  • How to define a class with constructors, functions, and properties.
  • How to instantiate an object.
  • What inheritance is.
  • The difference between IS-A and HAS-A relationships
  • How to override properties and functions.
  • What visibility modifiers are.
  • What a delegate is and how to use the by delegate.

What you'll build

  • A smart-home class structure.
  • Classes that represent smart devices, such as a smart TV and a smart light.

Note: The code that you write won't interact with real hardware devices. Instead, you print actions in the console with the println() function to simulate the interactions.

What you'll need

  • A computer with internet access and a web browser

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK