Scala学习笔记(5)类

1.简单类和无参方法

calss Counter{

  private var value = 0   //必须初始字段

  def increment(){value +=1}  //方法默认是公有的

  def current()=value

}

在Scala中,类并不声明为publuc。Scala源文件可以包含多个类,所以这些类具有公有可见性。使用该类需要做的就是构造对象并按照通常的方式来调用方法:

  val myCounter = new Counter  //或者new Counter()

  myCounter.increment()

  println(myCounter.current)

  

调用无参构造方法的时候,可以协商圆括号,也可以不写:myCounter.current<===>myCounter.current()  //这二者等价

对于改值器方法即setter increment 使用()比较好的;而对于取值器方法,去掉()是不错的风格

myCounter.increment()    //对改值器使用()
println(myCounter.current)    //对于取值器方法不使用()

OOP

-----------------------------

  scala>class Person{

    //定义变量,私有类型,变量必须进行初始化

    private var id = 0 ;

    //方法默认是public 的

    def incre(a:Int)={id+=a}

    //如果在定义方法的时候没有加括号,那么在进行调用的时候就一定不能写

    def current() =id 

}

  scala>var  p = new Person();

  scala>p.current() 

  scala>p.current

  scala>p.incre(100)  

2.带getter和setter属性

------------------------------------------

  编写java类的时候,我们并不喜欢使用公有字段

  public class Person{  //这是Java

    public int age ;    //java 中是不鼓励这样做的

}

  //因为在使用公有字段的时候会导致信息不安全,所以我们更倾向于使用getter和setter方法

    public class Person{  //这是java

      private int age ;

      public int getAge(){return age;}

      public void setAge(int age){this.age=age}

}

Scala对每个字段都提供getter和setter方法。在这里。我们定义一个公有字段:

   calss Person{

    var age = 0 

  }

这个地方Scala是面向JVM的,其中有一个私有的age字段以及相应的getter和setter方法。

class Person{
  private var id = 0 ;
  def incre(n:Int)={id+=n}      
  def current()=id      
}

D:\scala>javap Person.class
Compiled from "Person.scala"
public class Person {    //在编译过程中已经定义好了
public void incre(int);
public int current();
public Person();
}

 

private[this]的作用,控制成员变量只能在自己的对象中进行访问

--------------------------------------

  class Counter{

    private var value = 0;

    def incre(n:Int){value+=n}

    def isLess(other:Counter)=value<other.value;

}

5.Bean属性

当将scala字段的标注为@BeanProperty的时候,这样的方法会自动生成

import scala.reflect.BeanProperty

定义BeanProperty注解

----------------------------------------

  class Person{

    @BeanProperty va name:String=_

  }

将会生成四个方法:

  1.name:String

  2.name_=(newValue:String):Unit

  3.getName():String

  4.setName(newvalue:String):Unit

6.辅助构造器

----------------------------------

  Scala类有一个构造器比其他所有的构造器更为重要,他就是主构造器,除了主构造器之外,类还可以有任意多的辅助构造器

  辅助构造器:(1)辅助构造器的名称为this;(2)每一个辅助构造器都必须以先前已经定义的其他辅助构造器或者主构造器调用开始

class Person{
    var id =1 ;
    var name="tom";
    var age =12;
    //辅助构造
    def this(name:String){
        this();        //调用主构造
        this.name=name;
    }
    def this(name:String,age:Int){
        this(name);
        this.age=age;
    }
}

7.主构造

class Person(name:String,age:Int){
  var id =1;
  var name ="tom";
  var age =12//辅助构造
  def this(name:String){
  //调用辅助构造
  this(name);
  this.age=age;          
    }      
        
}
//主构造器 
//val ===>只读
//var ===>能读能写
class Person(val name:String,var age:Int,var id:Int){  //这个地方在参数列表里面声明了参数,就可以不进行任何的声明了,
                                  //val是常量,var是私有的,
}

猜你喜欢

转载自www.cnblogs.com/bigdata-stone/p/9652289.html