package a public open class Rectangle { public var width: Int64 protected var height: Int64 private var area: Int64 public init(width: Int64, height: Int64) { this.width = width this.height = height this.area = this.width * this.height } init(width: Int64, height: Int64, multiple: Int64) { this.width = width this.height = height this.area = width * height * multiple } }
func samePkgFunc() { var r = Rectangle(10, 20) // Ok: constructor 'Rectangle' can be accessed here r.width = 8 // Ok: public 'width' can be accessed here r.height = 24 // Ok: protected 'height' can be accessed here r.area = 30 // Error, private 'area' cannot be accessed here }
open class C1 { func f(): This { // its type is `() -> C1` return this }
func f2() { // its type is `() -> C1` return this }
public open func f3(): C1 { return this } } class C2 <: C1 { // member function f is inherited from C1, and its type is `() -> C2` now public override func f3(): This { // ok return this } }
var obj1: C2 = C2() var obj2: C1 = C2()
var x = obj1.f() // During compilation, the type of x is C2 var y = obj2.f() // During compilation, the type of y is C1
说明:
This 类型占位符,代指当前类的类型
它只能被作为实例成员函数的返回类型来使用
2.3 创建对象及调用
1 2 3 4
let r = Rectangle(10, 20) // r.width = 10, r.height = 20 let width = r.width // width = 10 let height = r.height // height = 20 let a = r.area() // a = 200
2.4 class 的继承
1 2 3 4 5 6 7 8 9 10 11
open class A { let a: Int64 = 10 }
class B <: A { // Ok: 'B' Inheritance 'A' let b: Int64 = 20 }
class C <: B { // Error, 'B' is not inheritable let c: Int64 = 30 }
说明:
类继承符号是<:,B继承A表示为class B <: A
class 仅支持单继承,因此下面这样一个类继承两个类的代码是不合法的
sealed 修饰符只能修饰抽象类,表示被修饰的类定义只能在本定义所在的包内被其他类继承
2.4.1 父类构造函数调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
open class A { A(let a: Int64) {} }
class B <: A { let b: Int64 init(b: Int64) { super(30) this.b = b }
init() { this(20) } }
说明:
super(args) 的形式调用父类构造函数
this(args) 的形式调用本类其它构造函数。
2.4.2 覆盖和重定义
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
open class A { public open func f(): Unit { println("I am superclass") } }
class B <: A { public override func f(): Unit { println("I am subclass") } }
main() { let a: A = A() let b: A = B() a.f() b.f() }
说明:
类覆盖使用关键字override
2.5 类与结构体的区别
class 是引用类型,struct 是值类型
class 之间可以继承,但 struct 之间不能继承
三 接口
3.1 概念
接口用来定义一个抽象类型,它不包含数据,但可以定义类型的行为
一个类型如果声明实现某接口,并且实现了该接口中所有的成员,就被称为实现了该接口
接口的成员可以包含:成员函数、操作符重载函数、成员属性
3.2 接口定义
1 2 3
interface I { // 'open' modifier is optional. func f(): Unit }
class MyInt <: Addable & Subtractable { var value = 0 public func add(other: Int64): Int64 { value + other } public func sub(other: Int64): Int64 { value - other } }
open class Base { var name: String = "Alice" } class Derived <: Base { var age: UInt8 = 18 }
main() { let a = 1 is Int64 println("Is the type of 1 'Int64'? ${a}") let b = 1 is String println("Is the type of 1 'String'? ${b}")
let b1: Base = Base() let b2: Base = Derived() var x = b1 is Base println("Is the type of b1 'Base'? ${x}") x = b1 is Derived println("Is the type of b1 'Derived'? ${x}") x = b2 is Base println("Is the type of b2 'Base'? ${x}") x = b2 is Derived println("Is the type of b2 'Derived'? ${x}") }