Swift_学习笔记_类
发布日期:2021-05-10 10:09:08 浏览次数:17 分类:精选文章

本文共 5739 字,大约阅读时间需要 19 分钟。

Swift������������������������

��������������������������������������������������������������������������������������������������� Swift ���������������������������������������������������������������������������������������������������������������������������������

���������������������������

������ Swift ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

������������

������������������������������������������������������������������������������������

  • ������������������������������������������������������������������������������������������������������ var ��� let ������������������
  • class Student {
    var name: String = "Anonymous"
    let id: Int
    init(id: Int) {
    self.id = id
    }
    }
    1. ������������������������������������ getter ��� setter ��������������������������������������������������������� dynamic ������������������������������������������������
    2. class Calculator {
      var total: Double {
      get {
      return value
      }
      set {
      self.value = $0
      }
      }
      private var value: Double
      }
      1. ��������������������������������� static ��� class ������������������������������������������������������ final ���������������������
      2. class MyClass {
        static vartu�� = 42
        }

        ������������������������������

        ������������������������������������������������������������Swift ������������������������������������������������������������������������������������

        class CityInfo {
        lazy var name: String = ""
        init() {
        print("������������������������
        }
        }
        class Metro {
        lazy var city: CityInfo? = nil
        }
        let metro = Metro()
        // ���������������������������������
        print(metro.city!.name)

        ���������������������������������

        ������������������������������������������������������������������������������������������������������������ UI���

        class LoginUser {
        var name: String {
        willSet(newValue) {
        print("���������\(oldValue)")
        }
        didSet(newValue) {
        print("���������\(newValue)")
        }
        }
        init(name: String) {
        self.name = name
        }
        }
        var user = LoginUser(name: "������")
        user.name = "������" // ������ "���������������������������������"

        ���������������������

        ��������������������������������������������� Swift ��������������������������� static ��� class ���������������������������������������������

        class Tool {
        class func sharedTool() -> Tool {
        struct ToolSingleton {
        static var instance: Tool? = nil
        init() {
        instance = Tool()
        }
        }
        if ToolSingleton.instance == nil {
        ToolSingleton.instance = Tool()
        }
        return ToolSingleton.instance!
        }
        }
        let tool = Tool.sharedTool()

        ������������������������������

        ������������ Swift ���������������������������������������������������������������������������������������������������������������

        class Student {
        var id: Int
        var name: String
        init(id: Int, name: String) {
        self.id = id
        self.name = name
        }
        }
        class ClassRoom {
        var className: String
        var students: Dictionary
        {
        get {
        return students
        }
        set {
        students = $0
        }
        }
        init(classname: String) {
        self.className = classname
        }
        }
        let classRoom = ClassRoom(classname: "������������
        var student = Student(id: 1, name: "������")
        classRoomudur(students: [1: student])

        ���������������������������

        ���������������������������������������������������������������������������������������������������

        class Square {
        var width: Int = 0
        var round: Int {
        get {
        return width * 4
        }
        set(value) {
        width = value / 4
        }
        }
        }
        let square = Square()
        square.round = 40
        print(square.width)

        ���������������������������

        ���������������������������������������������������������������������������������������������������������

        class City {
        var cityName: String?
        var cityId: Int?
        init(cityName: String, cityId: Int) {
        self.cityName = cityName
        self.cityId = cityId
        }
        }
        class Province {
        var provinceName: String?
        var cities: Dictionary
        {
        get {
        return cities
        }
        set {
        cities = $0
        }
        }
        init(provinceName: String) {
        self.provinceName = provinceName
        }
        }
        class Country {
        var countryName: String?
        var provinces: Dictionary
        {
        get {
        return provinces
        }
        set {
        provinces = $0
        }
        }
        subscript(provinceId: Int, cityId: Int) -> City {
        get {
        guard provinceId > 300 else { return nil }
        guard cityId > 40 else { return nil }
        let province = provinces[provinceId]!
        return province[cityId]!
        }
        set {
        guard provinceId > 300 else { return }
        guard cityId > 40 else { return }
        let province = provinces[provinceId]!
        province[cityId] = $0
        }
        }
        subscript(provinceId: Int) -> Province {
        get {
        return provinces[provinceId]!
        }
        set {
        provinces[provinceId] = $0
        }
        }
        }
        let country = Country()
        for provinceId in 1...300 {
        var province = Province(provinceName: "������\(provinceId)")
        for cityId in 1...40 {
        var city = City(cityName: "������\(provinceId)-������\(cityId)", cityId: cityId)
        province[cityId] = city
        }
        country[provinceId] = province
        }
        print(country[3, 306].cityName)
    上一篇:Swift_学习笔记_继承
    下一篇:Swift_学习笔记_枚举和结构体

    发表评论

    最新留言

    网站不错 人气很旺了 加油
    [***.192.178.218]2025年04月24日 00时48分56秒