Swift 学习 --- 基础<二>

// Playground - noun: a place where people can play

import UIKit

var str = "Hello, playground"

//------------------------- 类和结构体的相同与不同 -------------------------

//*************************

// 结构体和类的相同点, 在swift中,struct被强化

// 1. 都可以定义属性

// 2. 定义方法

// 3. 定义init (构造器)

// 4. 可以延展

// 5. 可以遵守协议

//*************************

// 不同点

// 1.结构体是值类型,类是引用类型

// 2.类是可以被继承的

// 3.类可以使用类型推断 isKindOfClass --- is

// 4.类可以使用deinit(析构器)

// 5.一个类可以有多个引用

//------------------------- 结构体 -------------------------

struct Resolution {

    

    // 定义属性

    var height = 0

    var width = 0

}

// 输入完名称后,打半括号,就会有提示(结构体)

var resolution = Resolution(height: 20, 20)

class Video {

    // 类的一个属性 (此处将一个结构体定义为该类的属性)

    var resolution2 = Resolution(height: 20, 20)

    var frameRate = 0.1

}

// 引用类型和值类型的区别

// 在类中,定义一个指针指向之前的实例,是指向同一块内存, 对原值进行修改

// 但是在结构体中,是开辟了一块新的空间,这样做,不会对原值造成任何影响

var newResolution = resolution

newResolution.height = 100

resolution

newResolution

// 类 指针指向原值,是同一块内存,所以,原值被修改

var video = Video()

video.frameRate = 0.2

var video1 = video

video1.frameRate = 0.5

video.frameRate

struct ResolutionA {

    var height: Int

    var Int

    

    // 构造器

    init (Height: Int, Width: Int) {

        

        // 自动生成 self.

        self.height = Height

        self.width = Width

    }

}

class VideoA {

    var resolutionA = ResolutionA(Height: 100, Width: 100)

    var frameRateA : Double

    

    // 构造器

    init(frameRate : Double){

        self.frameRateA = frameRate

    }

}

// 构造器,结构体会自动生成构造器方法,类不会

var videoA = VideoA(frameRate: 0.2)

videoA.resolutionA.height = 120

videoA.resolutionA.height

/**

*  属性 : 属性分为两种, 计算属性 / 存储属性

*/

// 存储属性是在类和结构体重

// 存储属性是存储在类和结构体重的常量或者变量 计算熟悉纪念馆,就是本身不会进行存储, 它是提供setter 和 getter 方法,简洁设置属性的值

struct Point {

    var x = 0

    var y = 0

}

struct Size {

    var height = 20

    var width = 20

}

struct Rect {

    // point 和 size 是存储属性

    var point = Point(x: 0, y: 0)

    var size = Size(height: 20, 20)

    

    // 计算属性, 通过point size 的值来计算center

    var center: Point {

        get {

            // 计算属性的get方法

            let centerX = point.x + size.width/2

            let centerY = point.y + size.height/2

            return Point(x: centerX, y: centerY)

        }

        

        set {

            // 默认newValue, 如果我们不给他名字, 就会叫 newValue

            let x = newValue.x - size.width/2

            let y = newValue.y - size.height/2

            point.x = x

            point.y = y

        

        }

    }

    

    

    // 写一个方法 在常规方法中,是不能实现体内部改变本身属性的值的,需要在方法前加"mutating"

    mutating func hello () {

        println("hello")

        self.point = Point(x: 100, y: 100)

    }

    

    // 静态方法 类似类方法

    static func hello2() {

        println("hello2")

    }

}

var rect = Rect()

rect.center

rect.point

rect.center = Point(x: 20, y: 20)

rect.center

rect.point

rect.size = Size(height: 40, 40)

rect.center

rect.point

// 调用结构体方法

rect.hello()

// 静态方法调用

Rect.hello2()

// -------------------------------- 枚 举 ----------------------------------

enum PersonIndentity: String {

    case Teacher = "Teacher"

    case Student = "Student"

}

// ------------------------------- 类 ----------------------------------

class Person {

    var indentify: PersonIndentity?

    let someValue: Int

    var name: String

    var sex: String

    

    // 类的构造器

    init(someValue: Int, name: String, sex: String, idd: PersonIndentity){

        

        self.someValue = someValue

        self.sex = sex

        self.name = name

        self.indentify = idd

        

    }

    

    deinit {

        

    }

    

    func hello(){

        println("hello")

        self.name = "world"

    }

    

    // 类方法, 类比于 + 方法 加"class"关键字

    class func hello2(){

        println("hello2")

    }

    

    // 类型属性 只能是计算属性 如果只有getter方法, 那么他就是只读的

    class var home: String {

        

        get{

            return "地球"

        }

        

    }

}

// 枚举赋值 PersonIndentity.Student PersonIndentity 可以省略 (因为具备类型推断功能) 写成

// .Student

var student = Person(someValue: 20, name: "Ammy", sex: "f", idd: PersonIndentity.Student)

student.hello()

Person.hello2()

// 类型属性页只能用类调用

Person.home

student.indentify?.hashValue

student.indentify?.rawValue

import UIKit

var str = "Hello, playground"
//------------------------- 类和结构体的相同与不同 -------------------------
//*************************
// 结构体和类的相同点, 在swift中,struct被强化
// 1. 都可以定义属性
// 2. 定义方法
// 3. 定义init (构造器)
// 4. 可以延展
// 5. 可以遵守协议

//*************************
// 不同点
// 1.结构体是值类型,类是引用类型
// 2.类是可以被继承的
// 3.类可以使用类型推断 isKindOfClass --- is
// 4.类可以使用deinit(析构器)
// 5.一个类可以有多个引用

//------------------------- 结构体 -------------------------

struct Resolution {
    
    // 定义属性
    var height = 0
    var width = 0
}

// 输入完名称后,打半括号,就会有提示(结构体)
var resolution = Resolution(height: 20,  20)

class Video {
    // 类的一个属性 (此处将一个结构体定义为该类的属性)
    var resolution2 = Resolution(height: 20,  20)
    var frameRate = 0.1
}


// 引用类型和值类型的区别
// 在类中,定义一个指针指向之前的实例,是指向同一块内存, 对原值进行修改
// 但是在结构体中,是开辟了一块新的空间,这样做,不会对原值造成任何影响

var newResolution = resolution
newResolution.height = 100
resolution
newResolution

// 类 指针指向原值,是同一块内存,所以,原值被修改
var video = Video()
video.frameRate = 0.2
var video1 = video
video1.frameRate = 0.5
video.frameRate

struct ResolutionA {
    var height: Int
    var  Int
    
    // 构造器
    init (Height: Int, Width: Int) {
        
        // 自动生成 self.
        self.height = Height
        self.width = Width
    }
}

class VideoA {
    var resolutionA = ResolutionA(Height: 100, Width: 100)
    var frameRateA : Double
    
    // 构造器
    init(frameRate : Double){
        self.frameRateA = frameRate
    }
}

// 构造器,结构体会自动生成构造器方法,类不会
var videoA = VideoA(frameRate: 0.2)
videoA.resolutionA.height = 120
videoA.resolutionA.height

/**
*  属性 : 属性分为两种, 计算属性 / 存储属性
*/

// 存储属性是在类和结构体重
// 存储属性是存储在类和结构体重的常量或者变量 计算熟悉纪念馆,就是本身不会进行存储, 它是提供setter 和 getter 方法,简洁设置属性的值

struct Point {
    var x = 0
    var y = 0
}

struct Size {
    var height = 20
    var width = 20
}

struct Rect {
    // point 和 size 是存储属性
    var point = Point(x: 0, y: 0)
    var size = Size(height: 20,  20)
    
    // 计算属性, 通过point size 的值来计算center
    var center: Point {
        get {
            // 计算属性的get方法
            let centerX = point.x + size.width/2
            let centerY = point.y + size.height/2
            return Point(x: centerX, y: centerY)
        }
        
        set {
            // 默认newValue, 如果我们不给他名字, 就会叫 newValue
            let x = newValue.x - size.width/2
            let y = newValue.y - size.height/2
            point.x = x
            point.y = y
        
        }
    }
    
    
    // 写一个方法 在常规方法中,是不能实现体内部改变本身属性的值的,需要在方法前加"mutating"
    mutating func hello () {
        println("hello")
        self.point = Point(x: 100, y: 100)
    }
    
    // 静态方法 类似类方法
    static func hello2() {
        println("hello2")
    }
}

var rect = Rect()
rect.center
rect.point

rect.center = Point(x: 20, y: 20)
rect.center
rect.point

rect.size = Size(height: 40,  40)
rect.center
rect.point

// 调用结构体方法
rect.hello()
// 静态方法调用
Rect.hello2()


// -------------------------------- 枚 举 ----------------------------------

enum PersonIndentity: String {
    case Teacher = "Teacher"
    case Student = "Student"
}

// ------------------------------- 类 ----------------------------------

class Person {
    var indentify: PersonIndentity?
    let someValue: Int
    var name: String
    var sex: String
    
    // 类的构造器
    init(someValue: Int, name: String, sex: String, idd: PersonIndentity){
        
        self.someValue = someValue
        self.sex = sex
        self.name = name
        self.indentify = idd
        
    }
    
    deinit {
        
    }
    
    func hello(){
        println("hello")
        self.name = "world"
    }
    
    // 类方法, 类比于 + 方法 加"class"关键字
    class func hello2(){
        println("hello2")
    }
    
    // 类型属性 只能是计算属性 如果只有getter方法, 那么他就是只读的
    class var home: String {
        
        get{
            return "地球"
        }
        
    }
}

// 枚举赋值 PersonIndentity.Student PersonIndentity 可以省略 (因为具备类型推断功能) 写成
// .Student
var student = Person(someValue: 20, name: "Ammy", sex: "f", idd: PersonIndentity.Student)

student.hello()
Person.hello2()

// 类型属性页只能用类调用
Person.home
student.indentify?.hashValue
student.indentify?.rawValue
原文地址:https://www.cnblogs.com/aaalice/p/4249234.html