iOS巅峰之Swift结构体和类, 枚举, 构造器, 继承, 协议, 扩展(完结篇)

来源:互联网 发布:分享赚钱的软件 编辑:程序博客网 时间:2024/05/17 07:04
// 结构体和类
// 在Swift中, 结构体被做了很多强化, 几乎所有的数据类型都是用结构体实现的
// 相同点:1.都可以定义变量  2.都可以定义方法  3.都可以定义构造器 init  4.都可以遵守协议 5.都有扩展(延展)
// 不同点: 1.结构体是值类型,类是引用类型 2.类可以被继承 3.类可以使用类型推断 4.类可以使用deinit(析构器) 5.一个类可以有多个引用


// 结构体
struct Resolution {
    // 定义变量 (属性)
    var height = 0
    var width = 0
    
}

// 结构体自动根据属性生成构造器 (init方法)
let resolution = Resolution(height: 10, width: 10)
resolution.height
resolution.width


// 类
class Video {
    var resolution_class = Resolution(height: 20, width: 20)
    var frameRate = 0.1
}
// 类不会自动生成构造器 需要手动实现
let video = Video()
video.frameRate = 0.2

// 值类型和引用类型的区别
var newResolution = resolution
newResolution.height = 50
newResolution.height
resolution.height

var newVideo = video
newVideo.frameRate = 0.5
newVideo.frameRate
video.frameRate


// 构造器
struct ResolutionA {
    var height = 0
    var width = 0
    
    // 自己写构造器,系统生成的self.属性名 用于赋值
    init(gao:Int, kuan:Int) {
        self.height = gao
        self.width = kuan
    }
}

let resolution2 = ResolutionA(gao: 100, kuan: 70)
resolution2.height


class VideoA {
    var frameRate = 0.2
    var resolution_VA = ResolutionA(gao: 20, kuan: 20)
    // 构造器会自动生成外部参数名,构造器内实现对属性的赋值操作
    init(frame:Double, resolu_VA: ResolutionA) {
        self.frameRate = frame
        self.resolution_VA = resolu_VA
    }
}
let videoA = VideoA(frame: 0.3, resolu_VA: resolution2)


// 属性分为两种: 计算属性和存储属性
// 存储属性:存储类和结构体里面的常量或变量,只起到存储作用
// 计算属性:不作为存储功能使用,计算属性本身提供get set方法, 间接的获取计算属性的值
struct Point {
    var x = 0
    var y = 0
}

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


var point = Point(x: 0, y:0)
// 代表正方形
struct Rect {
    
    var point_z = Point(x:0, y:0)
    var size = Size(width: 100, height: 100)
    // 计算属性
    var center:Point {
        set{
            // set方法中,自动生成newValue,代表赋给的新值
            let x = newValue.x - size.width / 4
            let y = newValue.y - size.height / 2
            point_z.x = x
            point_z.y = y
            
        }
        get {
            // 在get方法中,用于获取属性的值
            let centerX = point_z.x + size.width / 2
            let centerY = point_z.y + size.height / 2
            return Point(x: centerX, y: centerY)
        }
    }
}


var rect = Rect(point_z: Point(x: 0, y:0), size: Size(width: 100, height: 100))
rect.center.x
rect.center = Point(x: 500, y: 500)
rect.point_z.x
rect.center.x



// 定义方法
struct ResolutionB {
    var height = 0
    var width = 0
    
    // 结构体方法默认不能对结构体属性做更改,如果有更改需求,需要使用mutating关键字对方法进行修饰
    mutating func hello() {
        
        print("hello world")
        
        func hello2() {
            print("hello")
        }
        self.width = 20
    }
    
    // 类似于 + 方法 静态方法
    static func helloWorld() {
        print("hello world")
        //        self.with = 20
    }
}
var resolution4 = ResolutionB()
resolution4.hello()
ResolutionB.helloWorld()




class VideoB {
    var frameRate = 0.1
    // 类里面的普通方法可以对类的属性做更改
    func dj(){
        print("好的")
        frameRate = 0.2
    }
    
    
    // +方法 类型方法
    class func djj() {
        print("打双姐")
    }
    
    // 类型属性, 只能是计算属性,只实现get方法--就是只读
    class var name: String {
        get{
            return "毛毛"
        }
    }
}
var video3 = VideoB()
video3.dj()
VideoB.djj()
VideoB.name

//-----------------------------

// 枚举
//enum 枚举名: 类型 {
//    case 分支1 = 赋值1
//    case 分支2 = 赋值2
//}

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

// 类
class  Person {
    var indentity: PersonIndentity?
    var name : String?
    var age : String
    
    // 类的构造器
    init (name:String, age:String, idd:PersonIndentity) {
    self.name = name
        self.age = age
        self.indentity = idd
    }
    func hello() {
    print("hello")
    }
    
    // 类方法
    class func hello2() {
        
    }
}
// 枚举   枚举名.类型
// 枚举名可以省略
var person = Person(name: "毛毛", age: "88", idd: PersonIndentity.Student)
// 枚举值的获取

// hashValue 获取当前分支
person.indentity?.hashValue
// rawValue 获取分支的值
person.indentity?.rawValue


// 继承   类名 : 父类名
class Student: Person {
    var classNumber : Int = 23
    init (name: String, age: String, idd: PersonIndentity, classNum: Int) {
        // 如果属性仅指定类型, 需要在super.init前进行赋值操作
    self.classNumber = classNum
        super.init(name: name, age: age, idd: idd)
    }
    // 重写父类方法, 需要使用override关键字
    override func hello() {
        
    }
    override class func hello2() {
    
    }
}

// 协议
protocol OneProtocol {
    func typeFunc()
    static func typeFunc2()
    mutating func typeFunc3()
}

// 类和结构体实现协议方法的时候, 需要根据本身的语法规则做出调整
class StudentNew:OneProtocol {
    func typeFunc() {
        
    }
    class func typeFunc2() {
        
    }
    
    func typeFunc3() {
        
    }
}

struct VideoNew:OneProtocol {
    
    func typeFunc() {
        
    }
    static func typeFunc2() {
        
    }
    
    mutating func typeFunc3() {
        
    }

}
// 类同时继承父类和协议的时候, 父类必须写在前面
//class StudentNew2: Person, OneProtocol {
//
//}

@objc protocol TwoProtocol {
   optional func byebye()
}
// 当协议中方法使用 optional 声明可选时, 协议必须声明成@objc , 此时协议为类协议 :class protocol
// 并且不能被结构体继承
//struct Struct2:TwoProtocol {
//    
//}

// 扩展
var value :String = ""
extension Person {
    func hello6(){
    
    }
    // 如果想扩展属性, 只能是计算属性
    var stu2:String {
        get{
            return value
        }
        set{
         value = newValue
        }

    }
    // 扩展构造器的时候需要使用 convenience
  convenience  init (name: String, age: String, idd: PersonIndentity, stu2:String) {
    self.init(name:name, age:age, idd:idd)
    self.stu2 = stu2
    }
}

var person7 = Person(name: "maomao", age: "999", idd: .Student, stu2: "df")
person7.stu2
person7.stu2 = "笨蛋蛋"
person7.stu2


0 0
原创粉丝点击