One-Way
爱一人,攀一山,追一梦

一、简单类和无参方法

  class Counter{
    private var value = 0
    def increment(){value += 1} //改变对象状态的方法 使用()
    def current = value //取值,不改变对象状态的方法, 不使用()
  }

  val c  = new Counter
  c.increment()
  println(c.current)  //直接获取值,不带()

结果

1

 改值函数使用()

取值函数不使用()

方法默认是共有的

 

二、带getter和setter属性

//  class Persion{
//    var age = 0 //非private,可以使用getter和setter
// def current = age //如果age为private,可以自定义一个getter方法
// } class Persion{ private var privateAge = 0 def age = privateAge //getter 方法 age def age_=(newAge:Int) { //setter 方法 age_= _和=不能分开,中间不能有空格, if(newAge>privateAge) privateAge = newAge } } val f = new Persion f.age = 30 f.age = 20 println(f.age)

结果

30

 

三、对象私有字段

 

 

 

四、Bean属性

  import scala.beans.BeanProperty
  class Persion{
    @BeanProperty var age = 0
  }

  val p = new Persion
  p.setAge(10)
  println(p.getAge)

结果

10

 

五、辅助构造器

  class Persion{
    private var age = 0
    private var name = ""

    def this(name:String){   //辅助构造器
      this()         //调用主构造器
      this.name = name
    }

    def this(name:String,age:Int){ //辅助构造器
      this(name)     //调用辅助构造器
      this.age = age
    }
    
  }
  val p = new Persion          //主构造器
  val p1 = new Persion("Tom")    //辅助构造器
  val p2 = new Persion("Tom",10) //辅助构造器

 

六、主构造器

 

  class Persion(val name:String, val age:Int){ //主构造器的参数直接放置在类名之后
    println("Hi") //主构造器会执行类定义中的所有语句
  }

  val p = new Persion("Tom",10)

 

七、嵌套类

 

1.

    class Counter{
      var value = 0L
      def increment() = value+=1
      def current = value
    }

    val c = new Counter
    c.value=Int.MaxValue
    c.increment()
    println(c.current)

2.

    class BackAccount{
      private var value = 0D
      def deposit(money:Double): Unit ={
        value += money
      }
      def withdraw(money:Double): Unit ={
        value -= money
      }
      def current = value
    }

    val acc = new BackAccount
    acc.deposit(100)
    acc.withdraw(20)
    println(acc.current)

3.

    class Time(val hours:Int,val minutes:Int){
      def before(other:Time): Boolean ={
        if (hours==other.hours) minutes<other.minutes else hours<other.hours
      }
    }
    val t1 = new Time(2,17)
    val t2 = new Time(3,18)
    println(t1.before(t1))

4.

    class Time(val hours:Int,val minutes:Int){
      def before(other:Time): Boolean ={
        if (hours==other.hours) minutes<other.minutes else hours<other.hours
      }
      def display(): Unit ={
        println(hours+":"+minutes)
      }
      def display1(): Unit ={
        println(hours*60+minutes)
      }
    }
    val t1 = new Time(2,17)
    val t2 = new Time(3,18)
    t1.display()
    t1.display1()
    println(t1.before(t1))
    println(t1.hours)

5.

    import scala.reflect.BeanProperty

    class Student{
      @BeanProperty val name:String =""
      @BeanProperty val id:Long = 0L
    }
D:\>scalac Student.scala

D:\>javap -private Student
Compiled from "Student.scala"
public class Student {
  private final java.lang.String name;
  private final long id;
  public java.lang.String name();
  public long id();
  public java.lang.String getName();
  public long getId();
  public Student();
}

6&7. val

    class Person(val fullname:String){
      val firstName:String = fullname.split(" ")(0)
      val lastName:String = fullname.split(" ")(1)
      private var privateAge = 0
      def age = privateAge
      def age_=(newAge:Int): Unit ={
        if (newAge<0) privateAge = 0
      }
    }

    val p = new Person("Fred Smith")
    println(p.firstName)
    println(p.lastName)

8.必填参数设置为主构造器

 

9.

10.

 

 

 

 

参考《快学Scala》

posted on 2016-08-23 16:44  单行道|  阅读(855)  评论(0编辑  收藏  举报