scala基础内容-继承、组合
Composition and Inheritance
- abstract classes
- parameterless methods
- extending classes
- overriding methods and fields
- parametric fields
- invoking superclass constructors
- polymorphism and dynamic binding
- final members and classes
- factory objects and methods.
/// other file
import Element.elem
abstract class Element { // 1
def contents: Array[String] // 2
def height: Int = contents.length // 2
def width: Int = if(height == 0) 0 else contents(0).length // 2
// val height = contents.length
// val width = if(height == 0) 0 else contents(0).length
def demo(){
println("Element's implementation invokde")
}
def above(that: Element): Element =
new ArrayElement(this.contents ++ that.contents)
// def above(that: Element): Element =
// elem(this.contents ++ that.contents)
def beside(that: Element): Element = {
// val contents = new Array[String](this.contents.length)
// for(i <- 0 until this.contents.length)
// contents(i) = this.contents(i) + that.contents(i)
// new ArrayElement(contents)
new ArrayElement(
for(
(line1, line2) <- this.contents zip that.contents
) yield line1 + line2
)
}
override def toString = contents.mkString("\n")
}
// these definition can be moved to object Element
final class ArrayElement(conts: Array[String]) extends Element { // 3, 8
def contents: Array[String] = conts // 4
// val contents: Array[String] = conts // 4
final override def demo(){ // 8
println("ArrayElement's implementation invokde")
}
}
class LineElement(s: String) extends ArrayElement(Array(s)) { // 6
override def width = s.length
override def height = 1
override def demo(){
println("LineElement's implementation invokde")
}
}
class UniformElement(ch: Char, override val width: Int,
override val height: Int) extends Element {
private val line = ch.toString * width
def contents = Array.fill(height)(line)
}
class Cat {
val dangerous = false
}
class Tiger (
override val dangerous: Boolean, // 4
private var age: Int // 5
) extends Cat
val e1: Element = new ArrayElement(Array("hello", "world")) // 7
val ae = new ArrayElement(Array("hello"))
val e2: Element = ae // 7
val e3: Element = new UniformElement('x', 2, 3) // 7
def invokeDemo(e: Element){
e.demo()
}
invokeDemo(e2) // 7
invokeDemo(new LineElement("test"))
object Element{ // 9
def elem(contents: Array[String]): Element =
new ArrayElement(contents)
def elem(chr: Char, width: Int, height: Int): Element =
new UniformElement(chr, width, height)
def elem(line: String): Element =
new LineElement(line)
}
val col1 = elem("hello") above elem("***")
val col2 = elem("***") above elem("world")
col1 beside col2
Methods defined with empty parentheses, such as def height(): Int, are called empty-paren methods. The recommended convention is to use a parameterless method whenever
there are not parameters and the method accesses mutable state only by reading fields of the containing object. This convention supports the uniform access principle,
which says that client code should not be affected by a decision to implement an attribute as a field or method.
On the other hand, in Scala it is forbidden to define a field and method with the same name in the same class, whereas it is allowed in Java.
Generally, Scala has just two namespaces for definitions in place of Java's four. Java’s four namespaces are fields, methods, types, and packages. By contrast, Scala’s two namespaces are:
- values (fields, methods, packages, and singleton objects)
- types (class and trait names)
Using override modifiers
- Scala requires such a modifier for all members that override a concrete member in a parent class.
- The modifier is optional if a member implements an abstract member with the same name.
- The modifier is forbidden if a member does not override or implement some other member in a base class.
To ensure a member cannot be overriden by subclasses, we add final modifier to the member.
factory object
A factory object contains methods that construct other objects. Clients would then use these factory methods for object construction rather than constructing the object directly with new.
浙公网安备 33010602011771号