前端之家收集整理的这篇文章主要介绍了
swift学习笔记之构造方法和析构方法,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
import Foundation
struct Fahrenheit {
var temperature:Double
init() {
temperature = 32.0
}
}
var f = Fahrenheit()
print("the default temperature=\(f.temperature)")
struct Size {
var width = 0.0,height = 0.0
}
let twoByTwo = Size(width: 2.0,height: 2.0)
struct Point {
var x = 0.0,y = 0.0
}
class ShoppingListItem {
var name: String?
var quantity = 1
var purchased = false
}
var item = ShoppingListItem()
struct Celsius {
var temperatureInCelsius: Double = 0.0
init(fromFahrenheit fahrenheit: Double) {
temperatureInCelsius = (fahrenheit - 32.0)
/ 1.8
}
init(fromKelvin kelvin: Double) {
temperatureInCelsius = kelvin - 273.15
}
}
let boilingPointOfWater = Celsius(fromFahrenheit:212.0)
let freezingPointOfWater = Celsius(fromKelvin:273.15)
class SurveyQuestion {
let text: String
var response: String?
init(text: String) {
self.text = text
}
func ask() {
print(text)
}
}
var a = SurveyQuestion(text: "dddd")
a.ask();
class SurveyQuestion1 {
var text: String
var response: String?
init(text: String) {
self.text = text
}
func ask() {
print(text)
}
}
let cheeseQuestion = SurveyQuestion(text: "Do you like cheese?")
cheeseQuestion.ask()
cheeseQuestion.response = "Yes,I do like cheese."
struct Rect {
var origin = Point()
var size = Size()
init() {}
init(origin: Point,size: Size) {
self.origin = origin
self.size = size
}
init(center: Point,size: Size) {
let originX = center.x - (size.width / 2)
let originY = center.y - (size.height / 2)
self.init(origin: Point(x: originX,y: originY),size: size)
}
}
class Food {
var name: String
init(name: String) {
self.name = name
}
convenience init() {
self.init(name: "[Unnamed]")
}
}
let namedMeat = Food(name: "Bacon")
let mysteryMeat = Food()
class RecipeIngredient: Food {
var quantity: Int
init(name: String,quantity: Int) {
self.quantity = quantity
super.init(name: name)
}
convenience override init(name: String) {
self.init(name: name,quantity: 1)
}
}
let oneMysteryItem = RecipeIngredient()
let oneBacon = RecipeIngredient(name: "Bacon")
let sixEggs = RecipeIngredient(name: "Eggs",quantity: 6)
class ShpingListItem: RecipeIngredient {
var purchased = false
var description: String {
var output = "\(quantity) x \(name.lowercaseString)"
output += purchased ? " ✔" : " ✘"
return output
}
}
var breakfastList = [
ShoppingListItem(),ShpingListItem(name: "Bacon"),ShpingListItem(name: "Eggs",quantity: 6),]
for item in breakfastList {
print(item.description)
}
struct Checkerboard {
let boardColors: [Bool] = {
var temporaryBoard = [Bool]()
var isBlack = false
for i in 1...10 {
for j in 1...10 {
temporaryBoard.append(isBlack)
isBlack = !isBlack
}
isBlack = !isBlack
}
return temporaryBoard
}()
func squareIsBlackAtRow(row: Int,column: Int) -> Bool {
return boardColors[(row * 10) + column]
}
}
class Player {
var coinsInPurse:Int
init(coins: Int) {
print("call init")
coinsInPurse = coins
}
func winCoins(coins: Int) {
coinsInPurse += 10
}
deinit {
coinsInPurse = 0
}
}
var playerOne: Player? = Player(coins: 100)
print("coinsInPurse : \(playerOne!.coinsInPurse) coins")
playerOne = nil
print("PlayerOne has left the game")
原文链接:https://www.f2er.com/swift/323650.html