Swift Object Methods

Source: Internet
Author: User

The Swift class methods are also divided into object methods and class methods, so let's take a quick look at the object methods.

We analyze it in three categories.

Let's take a look at an example, create a student class Student, he has four attribute names name, age, sex sex, knowledge knowledge

We define three methods of learning

1, without parameters, as long as the students call the learning method knowledge +1

2, with two parameters, the first parameter subject represents the learning course, the second parameter indicates the learning lesson

3, parameter same as 2 but we specify an external name for each parameter



Class student{    var name= ""    var age:int=18    var sex:string = "Male"    var knowledge:int=10//student knowledge, default is ten        / /No parameter method students learn after knowledge increase    func study () {        self.knowledge++    }        //Two parameters for the method, the first parameter for the learning course, the second parameter for the learning lesson, Suppose that learning a lesson mathematics knowledge increases by 2, otherwise knowledge increases by 1.    Func study1 (subject:string, withtime:int) {        if (subject== "math") {            knowledge+=2*withtime        }else{            Knowledge+=1*withtime        }    }        //Specify method for parameter external name    func study2 (subjectname subject:string,studytime Withtime:int) {                if (subject== "math") {            knowledge+=2*withtime        }else{            knowledge+=1*withtime        }    }        }


The calls to the above three functions are as follows


var s=student ();        Call the parameterless function, directly using        s.study ()        println (S.knowledge)                //Call a function of multiple parameters, you need to note the following two points        //1, the parameter name of the first argument is not required to write out the default,        / /2, starting with the second argument, the parameter name needs to be written out (for example, the first parameter name Withtime to be written out)        s.study1 ("math", Withtime:5)        println (S.knowledge)                // Calls a function that specifies the name of an external parameter, which must be called with the specified external argument name        s.study2 (subjectname: "Sport", Studytime:3)        println (S.knowledge)



We're going to see the three function calls that correspond to what we're talking about.



// Call the parameterless function directly using


// function that calls multiple parameters, note the following two points

//1, the parameter name of the first argument does not need to be written out by default,

//2, starting with the second argument, the parameter name needs to be written out (e.g. the name of the first parameter in the example withtime needs to be written out)


// call a function that specifies the name of an external parameter, which must be called using the specified external argument name


You can try it and have a problem welcome communication


Apple Development Group: 414319235 Welcome to join the Welcome discussion question

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Swift Object Methods

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.