码迷,mamicode.com
首页 > 其他好文 > 详细

Scala中的类、接口及协变和逆变

时间:2014-10-16 14:56:28      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:scala的类   scala接口   scala协变和逆变   trait   scala反射   


4.   OOP

4.1.     class

4.1.1.  定义

例子1

class User {

  var name = "anonymous"

  var age:Int = _

  val country = "china"

  def email = name + "@mail"

}

使用:

val u = new User

// var定义的属性可读可写

u.name = "qh"; u.age = 30

println(u.name + ", " + u.age) // "qh, 30"

// val 定义的属性只读不可写

u.country = "usa" // 报错

println(u.country) // "china"

// def 定义的是方法每次调用时重新计算

u.email // "qh@mail"

 

例子2

// 定义

class Person(ln : String, fn : String, s : Person = null) {

  def lastName = ln;   // def定义后才是属性,lnfns不可见

  def firstName = fn;

  def spouse = s;

  def introduction() : String =

    return (

("Hi, " + firstName + " " + lastName) +

(if (spouse != null) " and spouse, " + spouse.firstName + " " + spouse.lastName + "."

 else ".")

   );

}

// 调用

new Person("aa","bb", new Person("cc","dd")).introduction();

 

4.1.2.  构造方法

class c1(x:String) // 等同于:class c1(private var x:String)

val o1 = new c1("aaa")

o1.x // 报错,因为是private的,定义成 class c1(var x:String) 或者 class c1(val x:String) 才能这样用

 

例子1

object construct1 {

  class c1(name:String, age:Int) { // (1)直接在类定义处

    def this() { this("anonymous", 20) } // (2)this定义

    def m1() = {  printf("%s=%d\n", name, age) }

  }

  def main(args:Array[String]) = {

    new c1().m1()

    new c1("qh", 30).m1()

  }

}

编译:fsc construct1.scala

运行:java construct1

 

例子2:继承中的构造方法:

class c2(name:String, age:Int, female:Boolean=false) extends c1(name,age) {

    override def toString = { name + "," +  age + "," + female }

}

 

4.1.3.  override

不同于Java的使用 @Override,或者直接使用相同名字覆盖父类方法。

    override def toString = { name + "," +  age + "," + female }

如果是覆盖抽象方法,可以不用override关键字。

4.1.4.  object单例对象 天然的Singleton

classes in Scala cannot have static members. Instead, Scala has singleton objects.
A singleton object definition looks like a class definition, except instead of the keyword class you use the keyword object.
Defining a singleton object doesn‘t define a type.


When a singleton object shares the same name with a class, it is called that class’s companion object. You must define both the class and its companion object in the same source file. The class is called the companion class of the singleton object. A class and its companion object can access each other‘s private members.


A singleton object is more than a holder of static methods, however. It is a first-class object. A singleton object is initialized the first time some code accesses it.

如:

Java

Scala

  1. public class User {  
  2.   private String name;  
  3.   private User(String name) { this.name=name; }  
  4.   private static theUser = new User("Nick");  
  5.   public static User instance() {  
  6.     return theUser;  
  7.   }  
  8. }  

  1. object User {      
  2.   var name:String = _  
  3.   def apply(name:String){this.name=name; this}  
  4.   override def toString = "name: " + name  
  5. }  

Scala中,则是:

sealed abstract class Fruits // sealed类似于javafinal

case object Apple extends Fruits

case object Banana extends Fruits

case object Cherry extends Fruits

也可以是 case class

 

4.1.9.  属性和Bean

例子1(直接定义和使用属性):

class c {

var name = "anonymous" // var定义的是r/w的属性

val age = 20  // val定义的是只r属性

}

 

val o = new c

o.name = "qh"

o.name // "qh"

o.age = 10 // 错误

o.age // 20

o.

 

例子2(定义get/set方法):

class c2 {

@reflect.BeanProperty var name = "anonymous"

}

val o2 = new c2

o2.name = "qh" // 也可以直接存取

o2.name // "qh"

o2.setName("james") // 增加了set/get方法

o2.getName() // "james"

4.1.10.      反射

Scala没有太特别的反射机制,使用java的即可,不过Scalamatch..case中可以匹配类型:

    case o:FooClass1 => ...

相关还有isInstanceOf[T], asInstanceOf[T]

 

1(利用javareflect):

"hello".getClass.getMethods.map(_.getName).toList.sortWith(_<_).mkString(", ")

 

例子2

classOf[String] // 相当于java中的String.class

"aaa".isInstanceOf[String] // true

"aaa".asInstanceOf[String]

疑问:为什么是中括号而不是小括号呢?泛型?

4.2.     trait超级接口

注:trait  [treit] n.特征,特点,特性

JavaInterface类似,但可以定义实体方法,而非仅仅方法定义

trait可以看作有方法实现和字段的interface;代表一类事物的特性;

比如

Tom,可能是Engine Son两个trait的混合;

Sunny可能SalesSonFather三个trait的混合;

当在运行时往Son里面增加方法或者字段的时候,TomSunny都得到增加的特性。

4.2.1.  trait使用

trait Runnable {

  def run(): Unit;

}

 

只是用一个接口,就用extends:

class c1 extends Runnable {...}

 

2个接口(或一个继承一个接口),用with而不是implements如下:

class c1 extends c0 with Runnable {

    def run(): Unit = {...}

}

 

一个类可以组合多个trait

class c1 extends t1 with t2 with t3 {...}

 

4.2.2.  mixin

class Human

class Child

 

trait Dad {

    private var children:List[Child] = Nil

    def add(child:Child) = child :: children

}

 

class Man1(name:String) extends Human with Dad // 静态mixin

class Man2(name:String) extends Human // 先不具备Dad trait

 

val m1 = new Man1("qh")

m1.add(new Child)

     

val m2 = new Man2("小孩")

//    m2.add(new Child) // 报错   

val m2$ = new Man2("james"with Dad // 动态mixin

m2$.add(new Child)

 

 

4.3.     协变和逆变(co-|contra-)variance

(找到应用,然后理解。仅仅是为了看懂定义中的符号?)

4.3.1.  概念

使用“+”“-”差异标记

Function[A, B]Function[-A, +B]的区别图示:

bubuko.com,布布扣

bubuko.com,布布扣

Function[A,B]

Function[-A,+B]

 

trait Queue[T] {}

非变

trait Queue[+T] {}

协变

如果S extends A (S为子类型,A为父类型)

Queue[S]为子类型,Queue[A]为父类型

S <: A => Queue[S] <: Queue[A]            

trait Queue[-T] {}

逆变

如果S extends A (S为子类型,A为父类型)

Queue[S]为父类型,Queue[A]为子类型,和协变互逆

S <: A => Queue[S] >: Queue[A]

 

-AA的子集,叫逆变

+BB的超集,叫协变

 

4.3.2.  类型上下界

 

 

<%

foo[T <% Ordered[T]](...)

关系较弱:T能够隐式转换为Ordered[T]

<:

foo[T <: Ordered[T]](...)

关系较强:T必须是Ordered[T]的子类型,即T的类型范围小于Ordered[T]Ordered[T]为上界

>:

foo[T >: A](...)

关系较强:T必须是A的父类型,即Tde类型范围大于AA为下界

 

4.3.3.  协变、逆变结合上下界

例子1

trait c1[+T] {

def m[K >: T](x:K) = x }

trait c1[-T] {

def m[K <: T](x:K) = x }

object c2 extends c1[Int]

c2.m(3) // 3

c2.m(3.0) // 3.0

c2.m("abc") // "abc"

object c2 extends c1[Int]

c2.m(3) // 3

c2.m(3.0) // 报错

c2.m("abc") // 报错

 

 

 

例子2

// 非变

case class T1[T](e:T)

val v1:T1[java.lang.Integer] = new T1(100)

val v2:T1[java.lang.Integer] = v1

v2.e // 100

val v3:T1[java.lang.Number] = v1 // 报错

 

// 协变

case class T1[+T](e:T)

val v1:T1[java.lang.Integer] = new T1(100)

val v2:T1[java.lang.Integer] = v1

v2.e // 100

val v3:T1[java.lang.Number] = v1 // 合法

v3.e // 100

val v4:T1[java.lang.Integer] = v3 //非法

 

// 逆变

class T1[-T](e:T)

val v1:T1[java.lang.Number] = new T1(100)

val v2:T1[java.lang.Number] = v1

val v3:T1[java.lang.Integer] = v1 // 合法

val v4:T1[java.lang.Number] = v3 // 非法

Scala中的类、接口及协变和逆变

标签:scala的类   scala接口   scala协变和逆变   trait   scala反射   

原文地址:http://blog.csdn.net/rocky_wangjialin/article/details/40146005

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!