Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Initialization methods of classes and structures in Swift

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

本篇内容主要讲解"Swift中类与结构的初始化方法",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Swift中类与结构的初始化方法"吧!

一,结构的初始化struct FirstStruct{ let b:Int let c:Int init() { b = 2 c = 3 } } let fs = FirstStruct()

解析:这个结构中b,c这两个存储属性并未赋初值,而是在init初始化器(相当于Java中的构造函数)中赋初值。

二,结构的初始化 struct SecondStruct{ let b:Int let c:Int } let ss = SecondStruct(b:2,c:3)

解析:结构中,编译器会自动生成memberwise的初始化器,因此在调用结构时必须传入初始化值。

三,结构中初始化错误示例//struct ThirdStruct{// let b:Int// let c:Int// init() {// b = 0// }//}//let ts = ThirdStruct(c:3)

注意:结构中声明了初始化器,编译器将不会给我们添加memberwise的初始化器,(原理和Java的空构造函数一致)上面这个例子是不能通过编译的。

四,结构中init可以通过self关键字调用其他的initstruct FourthStruct{ let b:Int let c:Int init() { self.init(b:2) } init(b:Int){ c = 2 self.b = b } } let fos = FourthStruct()五,类初始化错误示例 //错误示例 //class SecondClass{ // let b:Int // let c:Int //} //let sc = SecondClass(b:2,c:3)

解析:类里面也可以通过声明init初始化器,若不提供初始化器,编译器不会提供默认的初始化器

六,类的(Designated init)初始化器class Parent2{ var name:String var height:Int init(name:String,height:Int){ self.name = name self.height = height } } let p2 = Parent2(name:"zs",height:168) p2.height p2.name

解析:没有特殊的修饰符的init就是专门的(Designated init)初始化器,主要的作用就是确保所有的存储属性得到初始化

七,Designated init

一般只有一个,可以有多个,根据你调用时赋值的不用而调用不同的初始化器

class Parent3{ var name:String var sex:String init(name:String,sex:String){ self.name = name self.sex = sex } init(name:String){ self.name = name sex = "男" } } let p3 = Parent3(name:"zs",sex:"女") let p4 = Parent3(name:"ls") p3.sex p4.sex八,类的便利(convenience)初始化器class Parent3{ var name:String var sex:String init(name:String,sex:String){ self.name = name self.sex = sex } convenience init(name:String){ self.init(name:name,sex:"男") } convenience init(){ self.init(name:"zs") }}let p3 = Parent3(name:"zs")let p4 = Parent3()p3.sexp4.sex

解析:通过在初始化器前声明convenience关键字可以声明一个便利初始化器,它的作用是方便创建对象时使用,但它必须直接或间接调用专门初始化器

九,子类的专门初始化器

子类的专门初始化器必须调用父类的专门初始化器,子类不能调用父类的便利初始化器

class Parent4{ var name:String var sex:String init(name:String,sex:String){ self.name = name self.sex = sex } convenience init(name:String){ self.init(name:name,sex:"男") } } //----------------------------------------------- class Child4:Parent4{ var age:Int init(age:Int) { self.age = age //super.init(name: "ls")错误示例 super.init(name: "zs",sex:"妖") } convenience init(){ self.init(age:12) } } let c4 = Child4() c4.name到此,相信大家对"Swift中类与结构的初始化方法"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report