Skip to content

Instantly share code, notes, and snippets.

@hossshy
Created January 12, 2016 08:47
Show Gist options
  • Save hossshy/208cc2e28223ef844028 to your computer and use it in GitHub Desktop.
Save hossshy/208cc2e28223ef844028 to your computer and use it in GitHub Desktop.
Chapter3 ごちゃまぜ。
package chapter3
/**
* Created by hoshi on 1/12/16.
*/
class Shape {
def area:Double = 0.0
}
class Rectangle(val width:Double, val height:Double) extends Shape {
override def area:Double = width*height
}
class Circle(val radius:Double) extends Shape {
override def area:Double = math.Pi * radius * radius
}
object Chapter3 {
def draw(s:Shape): Unit = {
println(s.getClass + ":" + s.area)
}
def main(args: Array[String]): Unit = {
println("Chapter3")
draw(new Circle(3))
draw(new Rectangle(2,3))
}
}
----
package chapter3
/**
* Created by hoshi on 1/12/16.
*/
class Book(val title:String, var author:String, private var code:String = "0000", hidden:String = "default") {
def printCode {println(code)}
def printValues(b: Book) {
println(b.code) // 'private var' can access in the same class
//println(b.hidden) // Parameter without val/var cannot access
}
def printHidden {println(hidden)}
def printAll {
println(s"$title, $author, $code, $hidden")
}
}
object Main {
def main(args: Array[String]) {
val book = new Book("Scala", "hoge", "0001", "aaa")
// book.title = "piyo" // no setter
book.author = "piyo"
// book.code //private
book.printCode
book.printValues(book)
book.printValues(new Book("test", "test2", "0002", "bbb"))
val book2 = new Book("hoge", "0002", "aaa")
book2.printAll
val book3 = new Book("hoge", "0003")
book3.printAll
val book4 = new Book("hoge", "0004", hidden="ABC") // you can choose to provide named parameters
book4.printAll
}
}
----
package chapter3
/**
* Created by hoshi on 1/12/16.
*/
class AuxiliaryBook(var title:String, var ISBN: Int) {
def this(title:String) {
this(title, 2222)
}
def this() {
this("Begging Erlang")
}
override def toString = s"$title ISBN- $ISBN"
}
object AuxiliaryMain {
def main(args: Array[String]): Unit = {
println(new AuxiliaryBook)
println(new AuxiliaryBook("Clojure"))
println(new AuxiliaryBook("Scala", 3333))
}
}
----
package chapter3
import java.io.BufferedReader
/**
* Created by hoshi on 1/12/16.
*/
class Methods {
def mymethod() = "Moof"
def foo(a: Int):String = a.toString
def f2(a: Int, b:Boolean):String = if (b) a.toString else "false"
}
object MethodsMain {
def list[T](p:T):List[T] = p :: Nil
def largest(as: Int*): Int = as.max
def mkString[T](as: T*):String = as.foldLeft("")(_ + _.toString)
// The type must be Number or a subclass of Number
def sum[T <:Number](as:T*):Double = as.foldLeft(0d)(_ + _.doubleValue())
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
}
def main(args: Array[String]) {
val m = new Methods
println(m.mymethod())
println(m.foo(5))
println(m.f2(1,true))
println(m.f2(2,false))
println(list(1))
println(list("test"))
println(largest(1, 2, 3, 4, 5, 4, 3))
println(mkString("aa", "b", "ccc"))
}
}
----
package chapter3
/**
* Created by hoshi on 1/12/16.
*/
abstract class Base {
def thing: String
}
----
package chapter3
/**
* Created by hoshi on 1/12/16.
*/
class One extends Base {
def thing = "Moof"
}
class Two extends One {
override val thing = (new java.util.Date).toString
}
class Three extends One {
override lazy val thing = super.thing + (new java.util.Date).toString
}
object OneMain {
def main(args: Array[String]) {
val one = new One
println(one.thing)
val two = new Two
println(two.thing)
val three = new Three
println(three.thing)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment