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程序设计
134
5
open classes
Animal
image
food
habitat
hunger
makeNoise()
eat()
roam()
sleep()
open class Animal {
open val image = ""
open val food = ""
open val habitat = ""
var hunger = 10
open fun makeNoise() {
println("The Animal is making a noise")
}
open fun eat() {
println("The Animal is eating")
}
open fun roam() {
println("The Animal is roaming")
}
fun sleep() {
println("The Animal is sleeping")
}
}
src
Animals.kt
Animals
使用
open
关键字声明父类及它的属
性和方法
将一个类用作父类之前,必须显式地告诉编译器这是被允许的。你
可以通过给类名(及任何想要覆盖的属性和方法名)添加关键字
open作为前缀来达到此目的。这告诉编译器你设计的类将作为父类,
你定义的属性和方法将可以被覆盖。
Animal类层次结构中,我们希望能够将Animal作为父类,并覆
盖它的大部分属性和方法。如下代码允许我们这样做——请更新你
Animal.kt版本(更改的代码以粗体显示):
我们已经将Animal类,以及所有需要在子类中覆盖的属性和方法声 ...
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