1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | //: Playground - noun: a place where people can play import UIKit //class object class Vehicle{ var currentSpeed = 0.0 //stored property //Computed property var description : String { return "traveling at \(currentSpeed) miles per hour" } //Calling Method in Class func makeNoise() { } } let someVehicle = Vehicle(); print("Vehicle : \(someVehicle.description)") class Bicycle : Vehicle { var hasBasket = false } let bicycle = Bicycle() bicycle.hasBasket = true; bicycle.currentSpeed = 15.0 print("Bicycle : \(bicycle.description)") class Tandem : Bicycle { var currentNumberOfPassengers = 0 } let tandom = Tandem() tandom.hasBasket = true tandom.currentNumberOfPassengers = 2 tandom.currentSpeed = 22.0 print("Tandem : \(tandom.description)") class Train:Vehicle { override func makeNoise() { print("Choo Choo") } } let train = Train() train.makeNoise() | cs |
You define properties and methods to add functionality to your classes and structures by using exactly the same syntax as for constants, variables, and functions.
class는 상속받은 부모 클래스의 함수를 재정의 (override) 할 수 있다.
class와 structures 의 차이
class는 상속이 가능하지만 structures 는 불가능하다.
Classes and structures in Swift have many things in common. Both can:
Define properties to store values
Define methods to provide functionality
Define subscripts to provide access to their values using subscript syntax
Define initializers to set up their initial state
Be extended to expand their functionality beyond a default implementation
Conform to protocols to provide standard functionality of a certain kind
For more information, see Properties, Methods, Subscripts, Initialization, Extensions, and Protocols.
Classes have additional capabilities that structures do not:
Inheritance enables one class to inherit the characteristics of another.
Type casting enables you to check and interpret the type of a class instance at runtime.
Deinitializers enable an instance of a class to free up any resources it has assigned.
Reference counting allows more than one reference to a class instance.”
'IOS > Swift 문법' 카테고리의 다른 글
9. Optional Value (0) | 2016.05.20 |
---|---|
8. struct (0) | 2016.05.20 |
6. Functions (0) | 2016.05.20 |
5. Conditional Statements (0) | 2016.05.20 |
4. Control Flow (0) | 2016.05.20 |