1. 程式人生 > >scala入門練習二

scala入門練習二

在這裡插入圖片描述

作業一:

package day03

class Student(var name:String,var id:Long) {
}

反編譯之後的結果:

// Decompiled Using: FrontEnd Plus v2.03 and the JAD Engine
// Available From: http://www.reflections.ath.cx
// Decompiler options: packimports(3) 
// Source File Name:   Person.scala

package day03;

import scala.Int$;
import scala.Predef$;
import scala.collection.Seq;
import scala.collection.mutable.StringBuilder;

public class Person
{

    public int study()
    {
        Predef$.MODULE$.print("\u6B63\u5728\u5B66\u4E60");
        return 1;
    }

    public void study(String name, int age)
    {
        Predef$.MODULE$.print((new StringBuilder()).append(name).append("\u6B63\u5728\u5B66\u4E60").append(Int$.MODULE$).toString());
    }

    public void study(Seq x)
    {
        x.foreach(new  Object()     /* anonymous class not found */
    class .anonfun.study._anm1 {}

);
    }

    public int add(int x, int y)
    {
        return x + y;
    }

    public void add(int a, int b)
    {
        int _tmp = a + b;
    }

    public Person()
    {
    }
}

可以在Scala中呼叫JavaBean的getter和setter方法,在定義屬性前面加上註解 @BeanProperty 但是我們不應該這麼做。

作業二:(不太準確)

package day03

//maker製造商    model型號  year年份   number車牌號
class Car(val maker:String="123",val model:String="234",val year:Int= -1,var number:String="0000") {

  def this(maker: String,model:String,year:Int){
    this(maker,model,year,"")
    this.number=number
  }

  def this(number: Int){
    this("","",-2,"")
  }


  def this(year:Int,number: String){
    this("","",year,number)
  }

  override def toString: String = {
    maker+model+year+number
  }
}

object test4{
  def main(args: Array[String]): Unit = {
    val a = new Car() //程式入口還是主建構函式
    println(a.year)
  }

}