아미(아름다운미소)

[문법요약] Structure 본문

랭귀지/SWIFT

[문법요약] Structure

유키공 2017. 12. 14. 13:12
Structure
import UIKit
struct UserInfo {
    var name: String
    var weight: Int
    var email: String

    func showInfo() {
        print("이름: \(name), 몸무게: \(weight), 이메일: \(email)")
    }
}

struct UserInfo2 {

}

var userInfo = UserInfo(name: "김남준", weight: 67, email: "seungrry@daum.net")
var hyunjinInfo = UserInfo(name: "제이홉", weight: 100, email: "hyunjin@daum.net")
// ...
var userInfo2 = UserInfo2.init()
print(userInfo)
print(userInfo2)

userInfo.email
userInfo.showInfo()

var userInfo3 = userInfo
print(userInfo3)

userInfo3.name = "지민"
print(userInfo3)
print(userInfo)

class CompanyInfo {
    var name: String = ""
    var address: String = ""

    init() {

    }

    init(name: String) {
        self.name = name
    }

    init(name: String, address: String) {
        self.name = name
        self.address = address
    }

    func showInfo() -> Void {
       print("이름: \(self.name), 주소: \(address)")
    }
}

var companyInfo = CompanyInfo(name: "빅히트엔터테인먼트", address: "서울시 금천구 가산동")
companyInfo.showInfo()

var companyInfo2 = CompanyInfo()
companyInfo2.showInfo()

var companyInfo3 = CompanyInfo(name: "빅히트엔터테인먼트")
companyInfo3.showInfo()

var companyInfo4 = companyInfo
print(companyInfo4.name)

companyInfo4.name = "해피해피"
print("##########################\n" + companyInfo.name)

class SmileCompanyInfo: CompanyInfo {
    private var ceo: String = "김남준"
    func printCEO() {
        print(ceo)
    }

    override func showInfo() -> Void {
        print("CEO: \(ceo), 주소: \(address)")
    }
}

var smileInfo = SmileCompanyInfo(name: "빅히트엔터테인먼트", address: "미국 실리콘밸리")
print("########### CEO: " + smileInfo.ceo)
smileInfo.printCEO()
smileInfo.showInfo()
Comments