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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值