• 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)
    }
    }

      

  • 相关阅读:
    JS 面向对象
    堆 栈
    考试题
    HTML Meta标签
    Nodejs 安装
    CSS3 背景图片的大小位置
    JS Math函数
    CSS3 巧用before after选择器
    计算机网络原理_数据链路层
    Asp.net_验证控件
  • 原文地址:https://www.cnblogs.com/youlangta/p/10207494.html
Copyright © 2020-2023  润新知