Beginning Scala study note(3) Object Orientation in Scala
1. The three principles of OOP are encapsulation(封装性), inheritance(继承性) and polymorphism(多态性).
example:
class Shape{ def area: Double = 0.0 } # supertype
# subtypes
class Rectangle(val width: Double, val height: Double) extends Shape{
override def area: Double = width*height
}
class Circle(val radius: Double){
override def area: Double = math.Pi*radius*radius
}
A subtype is guaranteed to have all the members of the supertype. Changing the implementation of a method of the supertype is called overriding. In encapsulation, the fields of an object are accessible only through its methods.
example:
def draw(s: Shape)
val circle = draw(new Circle(3))
val rectangle = draw(new Rectangle(2,3))
Inheritance guarantees that any method we could call on an instance of Shape will be defined in the subtypes. This is the heart of polymorphism.
2. Classes and Objects
(1) example:
scala> class Book # defined Book class, corresponding to java declaration: public class Book{ }
defined class Book
scala> :javap -c Book # decompiling the Book class
Compiled from "Book.scala"
public class Book implements scala.ScalaObject {
public Book();
Code:
0: aload_0
1: invokespecial #10; // Method java/lang/Object."<init>":()V
4: return
}
scala> new Book # create an object or an instance of Book
res1: Book = Book@9206d91
scala> new Book()
res2: Book = Book@3d189f33
(2) Constructors
1) Constructors with Parameters
a. Parameter Declared as a val
In this case, Scala generates only a getter method for it, a setter method is not generated.
scala> class Book(val title: String)
scala> :javap -c Book
Compiled from "Book.scala"
public class Book implements scala.ScalaObject {
public java.lang.String title();
//......
public Book(java.lang.String);
//......
}
Scala generates a title method, which you can use to access the field. The value of the field is immutable by definition, because of val type.
scala> val book = new Book("Beginning Scala")
book: Book = Book@39a02a5d
scala> book.title
res2: String = Beginning Scala
scala> book.title = "new title"
<console>:: error: reassignment to val
book.title = "new title"
^
In Scala, if the constructor or method takes zero parameters, you can omit the parameter list.
b. Parameter Declared as a var
In this case, Scala generates both setter and getter method.
scala> class Book(var title: String)
scala> :javap -c Book
Compiled from "Book.scala"
public class Book implements scala.ScalaObject {
public java.lang.String title();
// ......
public void title_$eq(java.lang.String);
// ......
public Book(java.lang.String);
// ......
}
If you decompile this code, you can see the generated mutator method with an unusual name, title_$eq
scala> val book = new Book("Beginning Scala")
book: Book = Book@5b375aff
scala> book.title
res0: String = Beginning Scala
scala> book.title = "new title"
book.title: String = new title
c. Parameter Declared as a private val or var
In this case, it can prevent getter and setter methods from being generated. The fields could only be accessed from within members of the class.
scala> class Book(private var title: String)
scala> :javap -c Book
Compiled from "Book.scala"
public class Book implements scala.ScalaObject {
public Book(java.lang.String);
// ......
}
scala> class Book(private var title: String){
| def printTitle {println(title)}
| }
defined class Book
scala> val book = new Book("Beginning Scala")
book: Book = Book@70f19734
scala> book.title
<console>:: error: variable title in class Book cannot be accessed in Book
book.title
^
scala> book.printTitle
Beginning Scala
d. Parameter Declared without val or var
Scala does not generate getter or setter when neither val nor var are specified on constructor parameters.
scala> class Book(title: String)
defined class Book
scala> val book = new Book("Beginning Scala")
book: Book = Book@
scala> book.title
<console>:: error: value title is not a member of Book
book.title
^
The different Parameter with val and Parameter without val/var:
scala> class Book(private val title: String){
| def printTitle(b: Book){
| println(b.title)
| }
| }
defined class Book
scala> val book = new Book("Beginning Scala")
book: Book = Book@659a1780
scala> book.printTitle(book)
Beginning Scala
scala> book.printTitle(new Book("Beginning Erlang"))
Beginning Erlang
You can change the title of the Book because the title is a private field that is accessible to "this " object and the other objects of Book.
class Book(title: String){
def printTitle(b: Book){
println(b.title)
}
}
scala> scala Book.scala
Book.scala:: error: value title is not a member of this.Book
println(b.title)
^
one error found
You can provide a default value for a constructor parameter that gives other classes the option of specifying that parameter when calling the constructor.
scala> class Book(val title: String = "Beginning Scala")
defined class Book
scala> val book = new Book # you can call the constructor without specifying a title
book: Book = Book@7a0ad359
scala> book.title
res0: String = Beginning Scala
scala> val book = new Book("new title") # you can specify the title value
book: Book = Book@23a79657
scala> book.title
res2: String = new title
scala> val book = new Book(title="Beginning Scala") # you can also choose to provide named parameters
book: Book = Book@5f6af31
scala> book.title
res3: String = Beginning Scala
2) Auxiliary Constructor(辅助构造函数)
Auxiliary constructors are defined by creating methods named "this". In this way, you can define multiple auxiliary constructors, but they must have different signatures. Each auxiliary must begin with a call to a previously defined constructor.
scala> class Book(var title: String, var ISBN: Int){
| def this(title: String){
| this(title,)
| }
| def this(){
| this("Beginning Scala")
| this.ISBN=
| }
| override def toString = s"$title ISBN- $ISBN"
| }
defined class Book
scala> val book1 = new Book
book1: Book = Beginning Scala ISBN-
scala> val book2 = new Book("Beginning Erlang")
book2: Book = Beginning Erlang ISBN-
scala> val book3 = new Book("Beginning Clojure",)
book3: Book = Beginning Clojure ISBN-
(3) Method Declaration
1) It is recommended to use type inferencing judiciously(明智而谨慎地); if it's not immediately obvious what the return type is, declare it explicitly.
def myMethod(): String = "Moof"
def myOtherMethod() = "Moof"
def foo(a: Int):String = a.toString
def f2(a: Int, b: Boolean):String = if(b) a.toString else "false"
2) You can pass the type of a parameter or the return type as a parameter. The following code takes a parameter p and a type parameter T and returns a List of T.
scala> def list[T](p:T):List[T]=p::Nil
list: [T](p: T)List[T]
scala> list()
res9: List[Int] = List()
scala> list("Hello")
res10: List[String] = List(Hello)
scala> list(List(""))
res11: List[List[String]] = List(List())
3) If the last parameter is a variable-length argument(可变长度参数), it is a Seq of the type of the variable-length argument, so in this case the as parameter is a Seq[Int]:
scala> def largest(as: Int*): Int = as.reduceLeft((a,b)=> a max b)
largest: (as: Int*)Int
scala> largest()
res13: Int =
scala> largest(,,)
res14: Int =
scala> largest(,,,)
res15: Int =
You can mix type parameters with variable-length arguments:
scala> def mkString[T](as: T*):String = as.foldLeft("")(_ + _.toString)
mkString: [T](as: T*)String
scala> mkString()
res31: String =
scala> mkString(,,)
res32: String =
scala> mkString(,,3.0)
res33: String = 1.02.03.0
And you can put bounds on the type parameters. In this case, the types that are passed in must be Number or a subclass of Number:
scala> def sum[T <: Number](as: T*): Double = as.foldLeft(0d)(_ + _.doubleValue)
sum: [T <: Number](as: T*)Double
scala> sum()
res50: Double = 0.0
4) Methods can be declared within any code scope, except at the top level, where classes, traits, and objects are declared. Methods can reference any variables in their scope.
import java.io.BufferedReader
def readLines(br: BufferedReader) = {
var ret: List[String] = Nil
def readAll(): Unit = br.readLine match {
case null =>
case s => ret ::= s ; readAll()
}
readAll()
ret.reverse
}
The readAll method is defined inside the scope of the readLines method, Thus readAll method has access to the variables br and ret. The readAll method calls a method on br, and it updates ret.
5) Method that override declared methods must include the override modifier, except overriding abstract methods.
scala> abstract class Base{
| def thing: String
| }
defined class Base
scala> class One extends Base{
| def thing = "Moof"
| }
defined class One
scala> val one = new One
one: One = One@1bb62bd
scala> one.thing
res1: String = Moof
scala> class Base{
| def thing: String = ""
| }
defined class Base
scala> class One extends Base{
| def thing: String = "Moof"
| }
<console>:: error: overriding method thing in class Base of type => String;
method thing needs `override' modifier
def thing: String = "Moof"
^
Methods that take no parameters and variables can be accessed the same way, and a val can override a def in a superclass.
scala> class Two extends One{
| override val thing = (new java.util.Date).toString
| }
defined class Two
scala> val two = new Two
two: Two = Two@38ec38ad
scala> two.thing
res3: String = Tue Jun :: CST
scala> class Three extends One{
| override lazy val thing = super.thing + (new java.util.Date).toString
| }
defined class Three
scala> val three = new Three
three: Three = Three@779122a0
scala> three.thing
res4: String = MoofTue Jun :: CST
(4) Code Blocks
1) Methods and variables also can be defined in code blocks that are denoted by curly braces:{ }. Code blocks may be nested. The result of a code block is the last line evaluated in the code block.
scala> def meth3(): String = {"Moof"}
meth3: ()String
scala> meth3
res5: String = Moof
scala> def meth4(): String = {
| val d = new java.util.Date()
| d.toString
| }
meth4: ()String
scala> meth4
res6: String = Tue Jun :: CST
Variable definitions can be code blocks as well.
scala> val x3:String = {
| val d = new java.util.Date
| d.toString
| }
x3: String = Tue Jun :: CST
(5) Call-by-Name
1) Scala gives an additional mechanism for passing parameters to methods( and functions): call-by-name, which passes a code block to the callee.
scala> def nano() = {
| println("Getting nano")
| System.nanoTime
| }
nano: ()Long
scala> nano
Getting nano
res7: Long =
scala> def delayed(t: => Long) = { # call by name parameter(按名调用参数)
| println("In delayed method")
| println("Param:"+t)
| t
q | }
delayed: (t: => Long)Long
scala> delayed(nano)
In delayed method
Getting nano
Param:
Getting nano
res10: Long =
This indicates that delayed is entered before the call to nano and that nano is called twice.
scala> def notDelayed(t: Long) = {
| println("In notDelayed method")
| println("Param: "+t)
| t
| }
notDelayed: (t: Long)Long
scala> notDelayed(nano)
Getting nano
In notDelayed method
Param:
res11: Long =
nano is called before notDelayed is called because the parameter nano is calculated before notDelayed is called. This is the way Java programmers expect code to work.
(6) Method Invocation(方法调用)
instance.method() # java dot notation
instance.method # if a method does not take any parameters
instance.method(param) # a single parameter in java
instance.method param # methods that take a single parameter can be invoked without dots or parentheses
instance.method(p1,p2) # invoke multiparameter methods
instance.method[Type Param](p1,p2) # the type param will be inferred by the compiler, also you can explicitly pass
Because Scala allows method names to contain symbols such as +,-,*,?, Scala's dotless method notation creates a syntactically neutral way of invoking methods that are hard-coded operators in Java.
scala> 2.1.*(4.3)
res10: Double = 9.03
scala> 2.1*4.3
res11: Double = 9.03
(7) Objects
In Scala, you can use object to refer to an instance of a class or a keyword.
1) Singleton Objects
Scala does not have static members. Instead, Scala has singleton objects. A singleton is a class that can have only one instance.
scala> object Car{ # define car object
| def drive {println("drive car")}
| }
defined module Car
scala> Car.drive
drive car
scala> object Main extends App{ # Call car methods
| Car.drive
| }
defined module Main
Singleton objects cannot take parameter. There are two ways to create a launching point for your application: define an object with a properly defined main method or define an object that extends the App trait.
object Book extends App{
println("Hello World")
}
Note that in both approaches, Scala applications are launched from an object, not a class.
2) Companion Objects(伴生对象)
When an object shares a name with a class, it's called a companion object, and the class is called a companion class. A companion object is an object that shares the same name and source file with another class or trait. Using this approach lets you create static members on a class. The companion object is useful for implementing helper methods and factory.
We use a companion class Shape and a companion object Shape, which acts as a factory.
trait Shape {
def area: Double
}
object Shape{
private class Circle (radius: Double) extends Shape{
override val area = 3.14*radius*radius
}
private class Rectangle(height: Double, length: Double) extends Shape{
override val area = height * length
}
def apply(height: Double, length: Double): Shape = new Rectangle(height,length)
def apply(radius: Double): Shape = new Circle(radius)
}
A singleton object that does not share the same name with a companion class is called a standalone object.
(8) Packaging and Imports
1) The following statement imports the contents of the scala.xml package:
import scala.xml._
Import statements are made in the scope of prior imports. The following statement imports the scala.xml.transform._ package:
import transform._
You can import more than one class or object from a single package, for example:
import sclala.collection.immutable.{TreeMap, TreeSet}
You can import a class or object and rename it.
import scala.util.parsing.json.{JSON=> JsonParser}
Import can be used inside any code block, and the import will be active only in the scope of that code block.
scala> class Frog{
| import scala.xml._
| def n:NodeSeq = NodeSeq.Empty
| }
defined class Frog
Scala's import statement can also import the methods of an object so that those methods can be used without explicit reference to the object that owns them.This is much like Java's static import.
scala> object Moose{
| def bark = "woof"
| }
defined object Moose
scala> import Moose._
import Moose._
scala> bark
res2: String = woof
(9) Inheritance
1) Scala supports single inheritance.
scala> class Vehicle(speed: Int){
| val mph: Int = speed
| def race() = println("Racing")
| }
defined class Vehicle
The different extending class between Scala and Java are:
a. method overriding requires the override keyword, except trait
b. only the primary constructor can pass parameters to the base constructor.
scala> class Car(speed: Int) extends Vehicle(speed){
| override val mph = speed
| override def race() = println("Racing Car")
| }
defined class Car
scala> class Bike(speed: Int) extends Vehicle(speed){
| override val mph: Int = speed
| override def race() = println("Racing Bike")
| }
defined class Bike
scala> val vehicle1 = new Car()
vehicle1: Car = Car@1cfe29c
scala> vehicle1.race
Racing Car
scala> vehicle1.mph
res4: Int =
scala> val vehicle2 = new Bike()
vehicle2: Bike = Bike@
scala> vehicle2.mph
res5: Int =
scala> vehicle2.race
Racing Bike
(10) Traits
In Scala, when a class inherits from a trait, it implements the interface of the trait, and inherits all the code contained in the trait.
scala> trait flying{
| def fly() = println("flying")
| }
defined trait flying
scala> trait gliding{
| def glide() = println("gliding")
| }
defined trait gliding
Now you can create the Batmobile class extends Vehicle class along with the flying and gliding traits.
scala> class Batmobile(speed: Int) extends Vehicle(speed) with flying with gliding{
| override val mph: Int = speed
| override def race() = println("Racing Batmobile")
| override def fly() = println("Flying Batmobile")
| override def glide() = println("Gliding Batmobile")
| }
defined class Batmobile
n Scala, traits can inherit classes. The keyword extends is used: when a class inherits a trait, when the class mixes in other traits using the with keyword, when one trait is the child of another trait or class.
Now create a list of vehicles, then you can use the maxBy method provided by Scala collections library to find the fastest vehicle in the list.
scala> val vehicleList = List(vehicle1, vehicle2, batmobile)
vehicleList: List[Vehicle] = List(Car@1cfe29c, Bike@, Batmobile@8c2321)
scala> val fastestVehicle = vehicleList.maxBy(_.mph)
fastestVehicle: Vehicle = Batmobile@8c2321
(11) Case Classes
A case class provides the same facilities as a normal class, but the compiler generates toString, hasCode, and equals methods(which you can override). Case classes can be instantiated without the use of the new statement. By default, all the parameters in the case class's constructor become properties on the case class.
scala> case class Stuff(name: String, age: Int)
defined class Stuff
scala> val s = Stuff("David",) # create an instance of Stuff without the keywod new(you can use new if you want)
s: Stuff = Stuff(David,)
scala> s.toString # case class's toString method does the right thing
res0: String = Stuff(David,)
scala> s == Stuff("David",) # equal methods does a deep comparison
res1: Boolean = true
scala> s.name # the instance has properies
res2: String = David
scala> s.age
res3: Int =
class Stuff(val name: String, val age: Int){
override def toString = "Stuff("+name+","+age+")"
override def hashCode = name.hashCode + age
override def equals(other: AnyRef) = other match {
case s: Stuff => this.name == s.name && this.age == s.age
case _ => false
}
}
object Stuff {
def apply(name String, age: Int) = new Stuff(name,age)
def unapply(s: Stuff) = Some((s.name,s.age))
}
(12) Value Classes
With value classes, Scala allows user-defined value classes that extend AnyVal. Value classes are a new mechanism in Scala to avoid allocating runtime objects. Value classes allow you to add extension methods to a type without the runtime overhead of creating instances.
Class SomeClass(val underlying: Int) extends AnyVal
SomeClass has a single, public val parameter that is the underlying runtime representation. The type at compile time is SomeClass, but at runtime, the representation is an Int. A value class can defines defs, but no vals, vars, or nested traits classes or objects.
# wrap the Int parameter and encapsulates a twice method.
scala> class SomeClass(val i: Int) extends AnyVal{
| def twice() = i*
| }
defined class SomeClass
scala> val v = new SomeClass()
v: SomeClass = SomeClass@
scala> v.twiceres5: Int =
scala> :javap –c SomeClass
…...
SomeClass$.twice$extension # at runtime, the expression
a static object
…...
One use case for value classes is to combine them with implicit classes. Using an implicit class provides a more convenient syntax for defining extension methods, while value classes remove the runtime overhead.
(13) Scala verus Java
The uniformly of instances in Scala means that the developer does not have to perform special tests or to have different code paths to deal with primitive types.
scala> .hashCode
res6: Int =
scala> .toString
res7: String =
scala> def with42(in: Int => Int) = in() # define a method that takes a function that transforms an Int to an Int
with42: (in: Int => Int)Int
scala> with42(+)
res8: Int =
Beginning Scala study note(3) Object Orientation in Scala的更多相关文章
- Beginning Scala study note(4) Functional Programming in Scala
1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...
- Beginning Scala study note(1) Geting Started with Scala
1. Scala is a contraction of "scalable" and "language". It's a fusion of objecte ...
- Beginning Scala study note(9) Scala and Java Interoperability
1. Translating Java Classes to Scala Classes Example 1: # a class declaration in Java public class B ...
- Beginning Scala study note(2) Basics of Scala
1. Variables (1) Three ways to define variables: 1) val refers to define an immutable variable; scal ...
- Beginning Scala study note(8) Scala Type System
1. Unified Type System Scala has a unified type system, enclosed by the type Any at the top of the h ...
- Beginning Scala study note(6) Scala Collections
Scala's object-oriented collections support mutable and immutable type hierarchies. Also support fun ...
- Beginning Scala study note(5) Pattern Matching
The basic functional cornerstones of Scala: immutable data types, passing of functions as parameters ...
- Beginning Scala study note(7) Trait
A trait provides code reusability in Scala by encapsulating method and state and then offing possibi ...
- 面向对象(Object Orientation Programming)
Three characteristic of object orientation: Encapsulation: capturing data and keeping it safely and ...
随机推荐
- HttpContext.Cache属性
HttpContext基于HttpApplication的处理管道,由于HttpContext对象贯穿整个处理过程,所以,可以从HttpApplication处理管道的前端将状态数据传递到管道的后端, ...
- JavaScript 智能社 完美运动框架
简约版: function getStyle(obj, name) { if(obj.currentStyle) { return obj.currentStyle[name]; } else { r ...
- codevs1403 新三国争霸
题目描述 Description PP 特别喜欢玩即时战略类游戏,但他觉得那些游戏都有美中不足的地方.灾害总不降临道路,而只降临城市,而且道路不能被占领,没有保护粮草的真实性.于是他就研发了<新 ...
- 记一次Suse下的Django环境配置——第一弹
一.安装Python 由于原有Suse自带的Python版本只有2.4,因此首先需要安装Python的高版本,在这里我选择使用Python2.7.9.PS:之前选择使用2.7.11版本,由于没有zli ...
- js的click事件传递参数方法
参考链接:http://www.cnblogs.com/shytong/p/5005704.html 由于是回调函数,事先就需要先把数据储存在event上,否则只能用全局变量做为参数传递,建议用bin ...
- Qt &QSS
Today task:解决了qt中的一些控件无法使用qss的问题(如QProgressDialog 调节chunk的颜色,QMessageBox按钮的颜色问题)1,对于QMessageBox,可以单独 ...
- BZOJ 题目整理
bzoj 500题纪念 总结一发题目吧,挑几道题整理一下,(方便拖板子) 1039:每条线段与前一条线段之间的长度的比例和夹角不会因平移.旋转.放缩而改变,所以将每条轨迹改为比例和夹角的序列,复制一份 ...
- BZOJ 4184: shallot
Description 在某时刻加入或删除一个点,问每个时刻的集合中能异或出来的最大值是多少. Sol 线段树+按时间分治+线性基. 按时间分治可以用 \(logn\) 的时间来换取不进行删除的操作. ...
- vs2012 安装entity framework
1.安装vs2012 2.打开vs2012的工具下的扩展工具 3.搜索nuget,没安装的直接在线安装 4.安装好了NuGet,程序包管理器控制台 5.执行命令Install-Package Enti ...
- [Android Pro] 精确记录和恢复ListView滑动位置
reference to : http://blog.csdn.net/welovesunflower/article/details/7926512 工作中遇到一个需求,对ListView某一项操作 ...