I. Description
Like OC, the swift focus method can be divided into 2 major categories:
(1) Example method
Minus method in OC (object method)
(2) Type method
Plus method in OC (class method)
Ii. Examples and methods
Methods that can only be called by an object
code example:
1 class Dog {2 func run () {3 println ("dog--->run" )4 }5 }6var d = Dog () 7 D.run (
Description
Line 2nd defines a run method
Line 7th calls the Run method: The calling method is much like the calling function, and the format is "object name. Method Name (parameter)"
Iii. parameters of the method
The parameters of the method and function are somewhat different, by default
The 1th parameter name of a method is just a local parameter name
The other parameter names of the method (except for the 1th parameter name) are both local and external parameter names
1 class Calculator {2 func sum (num1:int, num2:int), Int {3 return NUM1 + num24 }5 }6var c = Calculator ()7 c.sum (ten)
Description
NUM1 is only a local parameter name, NUM2 is both a local parameter name and an external parameter name
Equivalent to Func sum (num1:int, #num2: int), int
(2) You can add an underscore in front of the parameter name to remove the default external parameter name
1 class Calculator {2 func sum (num1:int, _ Num2:int), Int {3 return NUM1 + num24 }5 }6var c = Calculator ()7 c.sum (ten)
Description: NUM1, num2 are only local parameter names, not external parameter names
(3) You can also add an external parameter name to the 1th parameter
1 class Calculator {2 func sum (#num1: int, num2:int), int {3 return NUM1 + num24 }5 }6var c = Calculator ()7)
Description: NUM1, num2 is both a local parameter name and an external parameter name
Iv. Types of methods
1. What is a type method?
Methods that are modified by the keyword class, also known as "class methods"
1 class Calculator {2 class func sum (num1:int, num2:int), Int {3 return num1 + num2
4
}
5
}
6 calculator.sum (
ten
)
Description
Line 2nd defines a type method
The characteristics of a type method: Invoking a type method directly with a class, cannot invoke a type method with an object
Line 6th invokes the type method, in the form "class name. Method Name (parameter)"
Note: The method name of the type method and the instance method can be the same
1 classCalculator {2 classFunc sum (num1:int, Num2:int)Int {3 returnNUM1 +num24 } 5Func sum (num1:int, Num2:int)Int {6 returnNUM1 +num27 } 8 } 9Calculator.sum (Ten, num2: -)Ten varc =Calculator () OneC.sum (Ten, num2: -)
Description
Line 2nd defines the type method and the instance method defined by line 5th: The same as the method name
Line 9th uses the class invocation type method
Line 11th using an object to invoke an instance method
2. Code examples
1 classPerson {2 classfunc Run () {3println"class Func Run") 4 } 5func Run () {6println"Func Run") 7 } 8 classfunc Test () {9Self.run ();Ten } Onefunc Test () { ASelf.run (); - } - } the varp =Person () -p.test () -Person.test ()
Turn from: Text top top of the blog http://www.cnblogs.com/wendingding/p/3887060.html, thank the top great God!
Swift Learning notes--Methods