Scala实现的Rational类

/**
 * Created by ly on 13-12-21.
 */
// class parameters; primary constructor
class Rational(n: Int, d: Int) {
  //包含在primary constructor中,在new Rational的时候会调用
  println("Created " + n + "/" + d)

  // precondition
  require(d != 0)
  /*
  The Scala compiler will place the code for the initializers of Rational ’s
three fields into the primary constructor in the order in which they appear
in the source code. 必须放在之前,否则g一开始是0, numer = n / 0会报异常
   */
  private val g = gcd(n.abs, d.abs)
  /*
  不能直接访问that.n或者that.d
  Even though n and d are used in the body of the class, given they are only used inside constructors, the Scala compiler will not emit fields for them. Thus, given this code the Scala
  compiler will generate a class with two Int fields, one for numer and one for denom .
   */
  val numer: Int = n / g
  val denom: Int = d / g

  /* Auxiliary constructors
  In Scala, every auxiliary constructor must invoke another constructor of the same class as its first action.
  In other words, the first statement in every auxiliary constructor in every Scala class will have the form “ this( . . . ) ”.
   */
  def this(n: Int) = this(n, 1)

  // Reimplementing the toString method
  override def toString: String = numer + "/" + denom

  def add(that: Rational): Rational = new Rational(numer * that.denom + that.numer * denom, denom * that.denom)

  // Defining operators
  def +(that: Rational): Rational = add(that)

  def *(that: Rational): Rational = new Rational(numer * that.numer, denom * that.denom)

  // Method overloading
  def +(i: Int): Rational = this + new Rational(i)

  def *(i: Int): Rational = this * new Rational(i)

  // Self references
  def lessThan(that: Rational) =
    this.numer * that.denom < that.numer * this.denom // this.numer == numer

  def max(that: Rational) =
    if (this.lessThan(that)) that else this

  // Private fields and methods

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


  /*
  Identifiers in Scala
  1. An alphanumeric identifier starts with a letter or underscore, which can be followed by further letters, digits, or underscores.
  _和$是合法的字符,但是最好不要用,$是Scala编译器使用的,容易导致冲突
  Scala中常量用XOffset,跟Java中X_OFFSET不太一样,其实明显Scala的表示要好,全是大写字母看上去很不直观,经常需要思考一下
  2. An operator identifier consists of one or more operator characters.
  例如+, ++, :::, <?>, :->,编译器内部表示$colon$minus$greater,如果在java中调用,需要这样写
  3. A mixed identifier consists of an alphanumeric identifier, which is followed by an underscore and an operator identifier.
  unary_+, myvar_=
  4. A literal identifier is an arbitrary string enclosed in back ticks ( ` . . . ` ).
  The idea is that you can put any string that’s accepted by the runtime as an identifier between back ticks.
  `x`, `<clinit>`, `yield`(是Scala的reserved words,所以只能这样访问)
   */
}

object Rational {
  /* Implicit conversions
  Note that for an implicit conversion to work, it needs to be in scope.
  放在Rational class里面不行!

  Scala gives you a great deal of power to design such easy-to-use libraries, but please bear in mind that with power comes responsibility.

  concise but readable, understandable client code!
   */
  implicit def intToRational(x: Int) = new Rational(x)

}



转载于:https://my.oschina.net/magicly007/blog/186576

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值