码迷,mamicode.com
首页 > 其他好文 > 详细

SCALA常规练习C

时间:2015-04-26 13:48:05      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:

package com.hengheng.scala


abstract class Animal {
  def walk(speed : Int)
  
  def breathe() = {
    println("Aninamal breathes.")
  }
}
trait Flyable {
  def hasFeather = true
  def fly
}
trait Swimable {
  def swim
}
class FishEagle extends Animal with Flyable with Swimable {
  def walk(speed : Int) = print("fish eagle walk with speed" + speed)
  def swim() = println("fish eagle swim fast")
  def fly() = println("fish eagle fly fast")
}
object Application{
  def main(args : Array[String]) {
    
  var fishEagle = new FishEagle
  
  var flyable : Flyable = fishEagle
  flyable.fly
  
  var swimmer : Swimable = fishEagle
  swimmer.swim
  var myArray : Array[String] = new Array[String](10)
  for(i <- 0 until myArray.length) {
    myArray(i) = "value is :" + i
  }
  for(value : String <- myArray) {
    println(value)
  }
  var myVar = "theValue"
  myVar match {
    case "somevalue" => println(myVar + "1")
    case "thisvalue" => println(myVar + "2")
    case "theValue" => println(myVar + "3")
    case "doublevalue" => println(myVar + "4")
  }
  
  def throwsException() {
    throw new IllegalStateException("Exception thrown")
  }
  try {
    throwsException();
  } catch {
    case e : IllegalStateException => println("illegal state exception.")
  } finally {
    println("This code is always executed.")
  }
 }
}

  输出:

fish eagle fly fast
fish eagle swim fast
value is :0
value is :1
value is :2
value is :3
value is :4
value is :5
value is :6
value is :7
value is :8
value is :9
theValue3
illegal state exception.
This code is always executed.

SCALA常规练习C

标签:

原文地址:http://www.cnblogs.com/aguncn/p/4457640.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!