Skip to Content
Head First Kotlin程序设计
book

Head First Kotlin程序设计

by Dawn Griffiths, David Griffiths
August 2020
Intermediate to advanced
483 pages
11h 24m
Chinese
China Machine Press
Content preview from Head First Kotlin程序设计
你现在的位置 4 93
classes and objects
对象
如何设计自己的类
当你在定义类的时候,你需要想想该类所创建的对象需要什么。
你需要考虑:
每个对象自身的特点
¥
每个对象的行为
¥
对象自身的特点称为属性properties。它们代表了对象自
身的状态(数据),并且该类中的每一个对象都有自己独特
的数值。例如,一个狗(Dog)类可能有名字(name)、体
重(weight)和品种(breed)属性。一个歌曲(Song)类
可能有标题(title)和演唱者(artist)属性。
对象的行为是它们的函数functions。它们决定了对象的
行为,并且可能回使用对象的属性。例如,之前提到的Dog
类,可能具有吠叫(bark)函数;Song这个类可能会有播放
play)函数。
下面是一些类以及它们所具有的属性和函数的例子:
对象自身的
特点称为属性
(properties)。
对象的行为是它的
函数。
当你知道类应该具有什么属性和函数时,就可以编写代码来
创建它了。
Dog
name
weight
breed
bark()
ShoppingCart
cartContents
addToCart()
removeFromCart()
checkout()
Song
title
artist
play()
stop()
Alarm
alarmTime
alarmMode
alarmSound
setAlarm()
isAlarmSet()
snooze()
属性
函数
属性就是一个对象自
身的特点
例如
首歌有它自己的歌名
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

面向对象的思考过程(原书第5版)

面向对象的思考过程(原书第5版)

马特 魏斯费尔德
GraphQL 学习指南

GraphQL 学习指南

Eve Porcello, Alex Banks
前端架构设计

前端架构设计

Micah Godbolt

Publisher Resources

ISBN: 9787111662358