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.
时间: 2024-10-01 21:44:27