class Person {
var firstName: String
var lastName: String
init(firstName:String, lastName:String){
self.firstName = firstName
self.lastName = lastName
}
init?(fullName:String) {
guard let spaceIndex = fullName.rangeOfString(" ")?.startIndex
else{
return nil
}
self.firstName = fullName.substringToIndex(spaceIndex)
self.lastName = fullName.substringFromIndex(spaceIndex.successor())
}
func fullName() ->String{
return firstName + " " + lastName
}
}
let person1 = Person(firstName: "aaaa", lastName: "bbbb")
let person2 = Person(fullName: "full name")
person1.fullName()
類是引用類型
class Person {
var firstName: String
var lastName: String
var career: String?
init(firstName:String, lastName:String, career:String){
self.firstName = firstName
self.lastName = lastName
self.career = career
}
init(firstName:String, lastName:String){
self.firstName = firstName
self.lastName = lastName
}
func fullName() ->String{
return firstName + " " + lastName
}
}
let person1 = Person(firstName: "aaa", lastName: "sss", career: "doctor")
let person2 = person1
person2.firstName = "www"
person2.lastName = "rrr"
person2.career = "ceo"
//person2改變之后墨叛,person1也改變了---他們指向的是同一塊內(nèi)存空間(結(jié)構(gòu)體是值類型,指向不同的內(nèi)存空間滓技,改變一個不會影響另一個)
person2
person1
class Person {
let firstName: String
let lastName: String
var career: String?
init(firstName:String, lastName:String, career:String){
self.firstName = firstName
self.lastName = lastName
self.career = career
}
init(firstName:String, lastName:String){
self.firstName = firstName
self.lastName = lastName
}
func fullName() ->String{
return firstName + " " + lastName
}
}
let person1 = Person(firstName: "aaa", lastName: "sss", career: "doctor")
//person1.firstName = "sss"不可修改
person1.career = "ceo"http://person1的let類型不會限制內(nèi)存空間中career的類型令漂,但是person1已經(jīng)指向了一塊內(nèi)存空間叠必,而且是let類型纬朝,不可以再指向另一塊
class Person {
let firstName: String
let lastName: String
var career: String?
init(firstName:String, lastName:String, career:String){
self.firstName = firstName
self.lastName = lastName
self.career = career
}
init(firstName:String, lastName:String){
self.firstName = firstName
self.lastName = lastName
}
func fullName() ->String{
return firstName + " " + lastName
}
func changeCareer(newCareer: String) {
self.career = newCareer
}
}
let person1 = Person(firstName: "aaa", lastName: "sss", career: "doctor")
person1.changeCareer("CEO")
結(jié)構(gòu)體和枚舉是值類型共苛,改變自身要加mutating
struct Location {
var x = 0
var y = 0
mutating func goEast() {
self.x += 1//本來不可改變隅茎,加上mutating
}
}
var location = Location()
location.goEast()
enum Switch {
case On
case Off
mutating func click(){
switch self {
case On:
self = .Off
case Off:
self = .On
}
}
}
var button = Switch.Off
button.click()
類的等價
class Person {
var firstName: String
var lastName: String
var career: String?
init(firstName:String, lastName:String, career:String){
self.firstName = firstName
self.lastName = lastName
self.career = career
}
init(firstName:String, lastName:String){
self.firstName = firstName
self.lastName = lastName
}
func fullName() ->String{
return firstName + " " + lastName
}
}
let person1 = Person(firstName: "aaa", lastName: "sss", career: "doctor")
let person2 = person1
person1 === person2
let person3 = Person(firstName: "aaa", lastName: "sss", career: "doctor")
person1 === person3
person1 !== person3
//什么時候使用類,什么時候使用結(jié)構(gòu)體
/*
1.一般位置堂竟,坐標(biāo)出嘹,溫度等等用結(jié)構(gòu)體。人疚漆,動物娶聘,車丸升,商店等等用類
2.類是引用類型狡耻,可以被繼承。結(jié)構(gòu)體是值類型
3.結(jié)構(gòu)體比類更“輕量級”
*/
類的繼承
class Avatar{
var name: String
var life = 100{
didSet{
if self.life <= 0{
self.isAlive = false
}
if self.life > 100{
self.life = 100
}
}
}
var isAlive: Bool = true
var description: String{
return "I'm avatar \(name)"
}
//指定構(gòu)造函數(shù)
init(name : String){
self.name = name
}
convenience init(firstName: String, lastName: String){
self.init(name: firstName + " " + lastName)
}
func beAttacked(attack: Int) {
life -= attack
if life <= 0 {
isAlive = false
}
}
}
class User:Avatar{
var score = 0
var level = 0
//重載---重寫父類的方法/屬性
override var description: String{
return "I'm user \(name)"
}
//兩段式構(gòu)造
var group: String
init(name: String,group: String = "" ) {
//1.構(gòu)造初值
self.group = group
super.init(name: name)
//2.進(jìn)一步完善
if group == ""{
self.getScore(-10)
}
}
//convenience便利構(gòu)造函數(shù)---沒有最終把整個對象構(gòu)造出來岭皂,而是執(zhí)行了一些邏輯之后爷绘,把構(gòu)造的任務(wù)交給了它本身的另外一個構(gòu)造函數(shù)
//與之相對應(yīng)的就是指定構(gòu)造函數(shù)designated
convenience init(group: String = ""){
let name = User.generateUserName()
self.init(name: name, group: group)
}
/*
子類構(gòu)造函數(shù)
1.如果子類沒有實現(xiàn)任何父類的指定構(gòu)造函數(shù)进倍,則自動繼承父類的所有指定構(gòu)造函數(shù)
2.如果子類實現(xiàn)了父類的所有的指定構(gòu)造函數(shù),則自動繼承父類的所有便利構(gòu)造函數(shù)
*/
override convenience init(name: String) {
self.init(name: name, group: "")
}
func getScore(score: Int) {
self.score += score
if score > level*100 {
level += 1
}
}
static func generateUserName() -> String{
return "Player" + String(rand()%1_000_000)
}
}
final class Magician: User {
var magic = 0
override var description: String{
return "I'm magician \(name)"
}
override init(name: String, group: String) {
let defaultGroups = ["Gryffindor","Hufflrpuff","Ravenclaw","Slytherin"]
for theGroup in defaultGroups {
if group == theGroup {
super.init(name: name, group: group)
return
}
}
let group = defaultGroups[random()%4]
super.init(name: name, group: group)
}
func heal(user: User) {
user.life += 10
}
}
final class Warrior: User {
var weapon: String
init(name: String, group: String, weapon: String ) {
self.weapon = weapon
super.init(name: name, group: group)
}
override var description: String{
return "I'm Warrior \(name)"
}
override func beAttacked(attack: Int) {
self.life -= attack/2
}
}
class Monster: Avatar {
override var description: String{
return "I'm monster \(name)"
}
func attack(user: User, amount: Int) {
user.beAttacked(amount)
}
}
final class Zombie: Monster{
var type = "Default"
override var description: String{
return "I'm zombie \(name)"
}
}
let player = User(name: "aaa")
player.name
player.life
player.isAlive
player.score
player.beAttacked(10)
player.life
player.getScore(30)
player.level
//不希望再有子類垂蜗,在前面加final
//類的多態(tài)
let player1 = Magician(name: "Harry Potter")
let player2 = Warrior(name: "liuyubobobo")
let zombie = Zombie(name: "default zombie1")
let monster = Monster(name: "monster")
func printBasicInfo(avatar:Avatar) {
print("The avatar's name is \(avatar.name)")
print("The life is \(avatar.life). He is \( (avatar.isAlive) ? "still alive" : "dead")" )
print("======")
}
printBasicInfo(player1)
printBasicInfo(player2)
printBasicInfo(zombie)
printBasicInfo(monster)
let avatarArr:[Avatar] = [player1, player2, zombie, monster]
for avatar in avatarArr{
//avatar.life -= 10
print(avatar.description)
}
player1.heal(player2)
player1.heal(player1)
//print(player1.description)
//print(player2.description)
//print(zombie.description)
//print(monster.description)
monster.attack(player1, amount: 20)
player1.life
monster.attack(player2, amount: 20)
player2.life
兩段式構(gòu)造
let user = User(name: "aaa", group: "ddd")