iOS讲解迷惑--Swift中类和结构体

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

import UIKit





// 结构体和类
// 在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 // 10
resolution.width // 10

let eee = Resolution(height: 10, width: 10)




// 类
class Video {
    // 定义结构体
    var relolution_class = Resolution(height: 20, width: 20)
    var frameRate = 0.1
}

// 类不会自动生成构造器(init 方法), 需要我们手动实现
let video = Video() // 创建对象
video.frameRate = 0.2
video.relolution_class  // Resolution
video.relolution_class.height // 20


// 只传递了值, 指向的不是 同一块内存空间 叫做值类型

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

resolution.height  // 10
newResolution.height = 50 // 这里把结构体的 高度属性改为50
resolution.height // 结果还是原来定义时候的10,  没有被改变成50, 这就是值类型的特点




// 引用类型 , 指向的是同一块内存空间,可以被更改属性的值
var newVideo = video
video.frameRate  // 0.2
newVideo.frameRate = 0.5
newVideo.frameRate // 0.5
video.frameRate // 0.5,  已经被改变(原来已经被改变)






// 构造器
struct ResolutionA {
    var height = 0
    var width = 0
    
    // 构造器(初始化方法), 系统生成self.属性名 用于赋值
    init(gao:Int, kuan:Int) {
        self.height = gao
        self.width = kuan
    }
}


// gao 外部参数名自动生成
let resolution2 = ResolutionA(gao: 10, kuan: 10)


resolution2.height // 10
resolution2.width // 10




///写一个类,属性为frameRate relolution_VA
/// 里面有构造器(初始化方法)
class  ViedoA {
    var frameRate = 0.2
    var relolution_VA = ResolutionA(gao: 20, kuan: 20)
    
    
    // 构造器会自动生成外部参数名, 构造器内部实现对属性的赋值操作
    init(frame:Double, resolu_VA:ResolutionA ){
        
        self.frameRate = frame
        self.relolution_VA = resolu_VA
        }
}

let videoA = ViedoA(frame: 0.3, resolu_VA: resolution2)
videoA.frameRate // 0.3
videoA.relolution_VA // ResolutionA结构体



// 属性分为两种: 计算属性和存储属性
// 存储属性: 存储类和结构体里面的常量或变量, 只起到存储作用
// 计算属性: 不作为存储功能使用, 计算属性本身提供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 {
    // point_z 是已知的点 (存储属性, )
    // size    是已知的大小(存储属性)
    var point_z = Point(x: 0, y: 0)
    var size = Size(width: 100, height: 100)
    
    
    
    // center 是一个计算属性(获取中心点)
    var center: Point {
        set{ // set 方法中, 自动生成newValue, 代表赋给的新值
            
          // 平移
            let x = newValue.x - size.width / 2 // size.width / 2 是获取x轴的中心点
            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 // 50
rect.center.y // 50

rect.center = Point(x: 500, y: 500)
rect.center.x // 500
rect.point_z.x


// 定义方法 (结构体方法)
struct ResolutionB {
    var height = 0
    var width = 0
    
    // 结构体定义方法
    //  结构体方法默认不能对结构体属性做更改, 如果有更改需求需要使用mutating关键字 对方法进行修饰
    mutating func hello() {
     print("你好") // "你好\n"
        
        /*
        func hello2(){
            print("hello2")
        }*/
        
        self.width = 20 // 加了mutating关键字就可以修改了
    }
    
    // 类似于 + 方法 静态方法
    // 完全从结构体中独立出来了, 结构体中的成员不能访问
    static func helloWorld() {
        print("hello world")
        
        //self.width // 报错
    }
}

var resolution4 = ResolutionB() // ResolutionB
resolution4.hello() // 调用hello()方法
ResolutionB.helloWorld() // ResolutionB







// 类
class VideoB {
    var frameRate = 0.1
    
    // 类里面的普通方法可以对类的属性做更改
    func dj() {
        print("hahaha")
        
        frameRate = 0.2
        // 可以加self 也可以不加
//       self.frameRate = 0.2
    }

    // +方法, 类型方法 (用VideoB这个类名调用)
    class func djj() {
        print("hello")
    }
    
    // 类型属性, 只能是计算属性, 也就是只实现get方法----只读
    class var name: String {
        
        get {
           return "毛毛军军"
        }
    }
}

// 创建video3对象
var video3 = VideoB()

video3.dj()
VideoB.djj() // 类方法 用 类名调用
VideoB.name // "毛毛军军"









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值