1. 程式人生 > >Scala程式語言中方法的定義以及assert函式的應用

Scala程式語言中方法的定義以及assert函式的應用

package Demo

/**
  * Created by Administrator on 2016/12/22.
  */
object MultiplyByTwo {
  def main(args: Array[String]): Unit = {
    //region mutiplyByTwo
    /* def multiplyByTwo(x: Int): Int = {
       println("Inside multiplyByTwo")
       x * 2
     }
     val r = multiplyByTwo(5)
     println(r)*/
    //endregion
    //region testAddMultiplyByTwo
    /* def
addMultiplyBy(x: Int, y: Double, s: String):
Double = { println(s) (x + y) * 2.1 } val r: Double = addMultiplyBy(7, 9, "Inside addMultiply") println(r) def test(x: Int, y: Double, s: String, expected: Double): Unit = { val result = addMultiplyBy(x, y, s) assert
(result == expected, "Expected " + expected + "Got " + result) println("result :=" + result) } test(7, 9, "Inside addMultiply", 40)*/ //endregion //region excise1 /*def getSquare(x: Int): Int = { println("The square is ") x * x } def test(x: Int, expected: Int)
:
Unit = { val r1 = getSquare(x) assert(r1 == expected, "Expected: " + expected + "Got " + r1) println(r1) } //test(3, 7) // test(6,36) test(5,25)*/ //endregion // //region excise2 /* def getSquareDouble(x: Double, s: String): Double = { println(s) x * x } val sd1 = getSquareDouble(1.2, "The square double is") assert(sd1 == 1.44, "Don't is same with the expected") println(sd1) val sd2 = getSquareDouble(5.7, "The square double is") assert(sd2 == 32.49, "Don't is same with the expected") println(sd2)*/ //endregion //assert()函式就是為了檢查自己所定義的函式的返回結果是否如自己所期望的一樣,若不一樣,則就會丟擲一個異常,異常的資訊在第二個引數中可以自己進行設定 //region excise3 /* def isArg1GreaterThanArg2(x: Double, y: Double): Boolean = { if (x > y) { true } else { false } } val t1 = isArg1GreaterThanArg2(4.1, 4.12) assert(true, "Arg1 is less than Arg2") println(t1) val t2 = isArg1GreaterThanArg2(2.1, 1.2) assert(true, "Arg1 is less than Arg2") println(t2) val t3 = isArg1GreaterThanArg2(1.3, 1.3)*/ //endregion //region excise4 /*def getMeToLower(s: String): String = { s.toLowerCase } val g1 = getMeToLower("ILoveYOU") assert("iloveyou" == g1, "converse to lower is failed") println(g1) val g2 = getMeToLower("asdfASDF") assert("asdfasdf" == g2, "converse to lower is failed") println(g2)*/ //endregion //region excise5 /* def addStrings(s1: String, s2: String): String = { s1 + s2 } val s = addStrings("abc", "def") assert("abcdef" == s, "The add strings is failed") println(s) val st = addStrings("XYZ", "abc") assert("XYZabc" == st, "The add strings is failed") println(st)*/ //endregion //臥槽,長見識了,字串和整形的乘積竟然是字串連線起來的次數。。。。。此刻博主的知識觀快要崩潰了,不過這門語言是真的強大,長知識了 //region excise6 字串 * 整數 /* def manyTimesString(s: String, x: Int): String = { s * x } val m1 = manyTimesString("abc", 3) assert("abcabcabc" == m1, "many times string is failed") println(m1) val m2 = manyTimesString("123", 2) assert("123123" == m2, "many times string is failed") println(m2) val m3 = manyTimesString("I love you", 5) println(m3)*/ //endregion } }

博主在Scala語言程式設計中第一個感覺就是方便,Scala語言太方便了,簡直就是利器,本來是準備將其充當Spark-hadoop的機器學習語言,方便閱讀一些機器學習原始碼,可是今天剛一接觸到Scala語言就被其簡單的語法,強大的功能所吸引,特別是,今天博主學到在Scala語言中字串是可以和整數相乘的,這完全顛覆了博主的知識觀,學習java的時候字串是無法與整數相乘的。這意味著什麼?
這就意味著在java中想要實現的功能如果用Scala語言僅需要很少的程式碼量就能完成同樣的功能,比如定義一個方法,這個方法接受兩個引數,第一個是整形,第二個是字串型別,方法的返回結果是將字串重複第一個引數的次數。

java版

package JavaDemo;

/**
 * Created by Administrator on 2016/12/20.
 */
public class Test {
    public static void main(String[] args) {
        String s = manyTimesString("abc", 3);
        System.out.println(s);
    }
    public static String manyTimesString(String s, int n) {
        String result = "";
        while (n > 0) {
            result += s;
            n--;
        }
        return result;
    }
}

Scala版:

package Demo

/**
  * Created by Administrator on 2016/12/20.
  */
object Test {
  def main(args: Array[String]): Unit = {
    def manyTimesString(s: String, n: Int): String = {
      s * n
    }
    val s1 = manyTimesString("abc", 3)
    assert("abcabcabc" == s1, "many times string is failed")
    println(s1)
  }
 }

不多說了,我得重新構建知識觀了T_T