【Swift 60秒】73 - Creating your own classes

0x00 Lesson

Classes are similar to structs in that they allow you to create new types with properties and methods, but they have five important differences and I’m going to walk you through each of those differences one at a time.

The first difference between classes and structs is that classes never come with a memberwise initializer. This means if you have properties in your class, you must always create your own initializer.

class Dog {
	var name: String
	var breed: String
	init(name: String, breed: String) {
		self.name = name
		self.breed = breed
	}
}

Creating instances of that class looks just the same as if it were a struct:

let poppy = Dog(name: "Poppy", breed: "Poodle")

0x01 我的小作品

欢迎体验我的作品之一:小五笔 86 版
五笔学习好帮手
App Store 搜索即可~


猜你喜欢

转载自blog.csdn.net/xjh093/article/details/128492413