原创转载请注明出处:http://agilestyle.iteye.com/blog/2333901
同Java类似,使用extends关键字实现继承
package org.fool.scala.inheritance class Person { val height = 180 val weight = 160 } class Teacher extends Person class Student extends Person object Person extends App { def display(p: Person) = s"height: ${p.height} weight: ${p.weight}" println(display(new Person)) println(display(new Teacher)) println(display(new Student)) }
Console Output
使用构造器参数重写Person
package org.fool.scala.inheritance class Person2(val height: Int, val weight: Int) class Teacher2(height: Int, weight: Int) extends Person2(height, weight) class Student2(height: Int, weight: Int) extends Person2(height, weight) object Person2 extends App { def display(p: Person2) = s"height: ${p.height} weight: ${p.weight}" println(display(new Person2(180, 160))) println(display(new Teacher2(167, 90))) println(display(new Student2(178, 130))) }
Console Output
在继承时,子类构造器必须调用基类的主构造器。如果基类中有辅助(重载的)构造器,那么可以选择改为调用这些构造器中的某一个。子类的构造器必须将适合的参数传递给基类构造器:
package org.fool.scala.inheritance class House(val address: String, val state: String, val zip: String) { def this(state: String, zip: String) = this("address?", state, zip) def this(zip: String) = this("address?", "state?", zip) } class Home(address: String, state: String, zip: String, val name: String) extends House(address, state, zip) { override def toString = s"$name: $address $state $zip" } class VacationHouse(state: String, zip: String, val startMonth: Int, val endMonth: Int) extends House(state, zip) class TreeHouse(val name: String, zip: String) extends House(zip) object AuxiliaryInitialization extends App { val home = new Home("District JingAn", "Shanghai", "200000", "JingAnSi") println(home.address) println(home.state) println(home.zip) println(home.name) println(home) val vacationHouse = new VacationHouse("Shanghai downtown", "200001", 7, 8) println(vacationHouse.state) println(vacationHouse.zip) println(vacationHouse.startMonth) println(vacationHouse.endMonth) println(vacationHouse) val treeHouse = new TreeHouse("Shanghai Center", "200002") println(treeHouse.name) println(treeHouse.zip) println(treeHouse) }
Console Output
使用override关键字覆盖方法
package org.fool.scala.inheritance class Person3 { def call = "Person invoked.." var energy = 100; def eat() = { energy += 10; energy } def work(x: Int) = energy -= x } class Teacher3 extends Person3 { override def call: String = "Teacher invoked.." // Modify the base-class var energy = 80 override def eat() = super.eat() * 2 def say() = "Teacher say.." } class Student3 extends Person3 { override def call: String = "Student invoked.." override def eat() = super.eat() * 3 def doHomework() = "Student do homework.." var word = "Teacher is a sb!" } object Person3 extends App { def show(p: Person3) = { p.eat() p.call + p.eat() } println(show(new Person3)) println(show(new Teacher3)) println(show(new Student3)) }
Console Output
相关推荐
-type member inheritance, multiple inheritance, and compositionFunctional concepts and patterns--immutability, applicative functors, and monads ==================================================Table ...
and more * Becoming familiar with object-oriented programming in Scala: classes, inheritance, and traits * Using Scala for real-world programming tasks: working with files, regular expressions, and ...
Scala中的面向对象编程部分,采用了传统面向对象设计中的关键概念,如类(class)、对象(object)、继承(inheritance)、多态(polymorphism)和封装(encapsulation)。然而,Scala的面向对象编程与传统的OOP语言...
### 编程在Scala:全面理解与应用 #### 标题解析:“Programming in Scala” - **Scala语言**:Scala是一种现代、多范式编程语言,它融合了面向对象编程和函数式编程的特点,旨在提高代码的简洁性和可读性。 - **...
OOP强调封装(Encapsulation)、继承(Inheritance)、多态(Polymorphism)等核心概念。 - **过渡挑战**:对于长期从事OOP开发的程序员而言,转向FP会面临诸多挑战。这不仅涉及编程思维模式的根本转变,还需要学习...
2. 继承(Inheritance) 3. 多态/动态绑定(Polymorphism/dynamic binding) 4. 所有预定义类型都是对象(All predefined types are objects) 5. 所有操作都是通过发送消息给对象来实现的(All operations are ...
- **多重特质继承(Multiple Trait Inheritance)**: 类可以实现多个Trait。 7. **Actor模型与并发** - **Akka框架**: Akka是Scala中的一个并发和分布式处理库,基于Actor模型。 - **Actor**: 独立的、并发执行的...
**2.2 继承 (Inheritance)** 解释如何在 OrientDB 中实现继承机制,以及它对数据模型的影响。 **2.3 模式 (Schema)** 详细介绍 OrientDB 的模式系统,包括如何设计和维护模式。 #### 三、检索策略 (Fetching ...
Kotlin首次发布于2010年,并在2011年开源,它汲取了多种编程语言的优点,包括Java、Scala、Groovy、C#、Gosu、JavaScript和Swift等。 Kotlin具有以下特点: - **语法简洁**:Kotlin的语法简洁明了,减少了冗余代码...