zoukankan      html  css  js  c++  java
  • scala 标识符案例

    package com.youlangta.scalaTest

    class Rational(n: Int, d: Int) {
    require(d != 0, "分母不能为0")
    private val g = gcd(n.abs, d.abs)
    val number: Int = n / g
    val denom: Int = d / g

    override def toString: String = number + "/" + denom

    def this(n: Int) = this(n, 1)

    def +(that: Rational): Rational = {
    new Rational(
    number * that.denom + that.number * denom,
    denom * that.denom
    )
    }

    def +(i: Int): Rational = {
    new Rational(
    number + i * denom, denom
    )
    }


    def -(that: Rational): Rational = {
    new Rational(
    number * that.denom - that.number * denom,
    denom * that.denom
    )
    }

    def -(i: Int): Rational = {
    new Rational(
    number - i * denom, denom
    )
    }

    def *(that: Rational): Rational = {
    new Rational(
    number * that.number,
    denom * that.denom
    )
    }

    def *(i: Int): Rational = {
    new Rational(
    number * i, denom
    )
    }

    def /(that: Rational): Rational = {
    new Rational(
    number * that.denom,
    denom * that.number
    )
    }

    def /(i: Int): Rational = {
    new Rational(
    number, denom * i
    )
    }

    private def gcd(a: Int, b: Int): Int = {
    if (b == 0) a else gcd(b, a % b)
    }
    }

      

  • 相关阅读:
    Java——快速排序
    Java——归并排序
    Java——递归
    Java——希尔排序
    Java——插入排序
    Java——选择排序
    ES6快速入门
    ECharts is not Loaded
    scoped的规则
    css哪些样式属性可以继承
  • 原文地址:https://www.cnblogs.com/youlangta/p/10207494.html
Copyright © 2011-2022 走看看