zoukankan      html  css  js  c++  java
  • Scala 学习笔记(四)

    object A
    {  
        def main(args:Array[String]):Unit=
      {
            val b = new B();
            b(0)="scala"; 
            print(b(0)); // scala 索引器的实现
        }
    }
    
    class B
    {
        private val content:Array[String] = Array("hello","world");//Array[String]("hello","world"); 或者 Array.apply[String]("hello","world"); Array.apply("hello","world");
        def apply(index:Int):String =
        {
            return this.content(index);//return this.content.apply(index);
        }
    
        def update(index:Int,item:String):Unit =
        {
            this.content(index)=item;//this.content.update(index,item);
        }
    }
     
    
    class R(n:Int,d:Int )
    {
    private val g = gcd(n,d);
    val number = n/g;
    val denom = d/g;
    
    def this(n:Int)=this(n,1);
    
    private def gcd(a:Int,b:Int):Int = if(b==0) a else gcd(b,a%b);//recursive method gcd needs result type
    override def toString() = if(this.denom==1) this.number.toString() else this.number+"/"+this.denom;
    
    def +(that:R)= new R(this.number*that.denom+that.number*this.denom*that.number,this.denom*that.denom);
    def *(that:R)= new R(this.number*that.number,this.denom*that.denom);
    def /(that:R)= new R(this.number*that.denom,this.denom*that.number);
    }
    
    object R
    {
    def apply(n:Int,d:Int)= new R(n,d);
    def apply(n:Int)=new R(n);
    }
    
    
    object A
    {  
    def main(args:Array[String]):Unit=
    {
    print(R(2,3)/R(1,3));
    }
    } 
    

      

  • 相关阅读:
    Nginx服务器中配置非80端口的端口转发方法详解
    索引的优缺点
    redis cluster搭建
    linux 无需主机密码传输文件
    linux buffer/cache手动释放
    常用的Percona-Toolkit工具
    mysql 存储过程批量删除表
    redis(方法通用)开机自启动
    mysql 查看表的分区信息
    redis 内存分析
  • 原文地址:https://www.cnblogs.com/scala/p/2650025.html
Copyright © 2011-2022 走看看