Swift Learning notes-the computed properties of a class

Source: Internet
Author: User

1,swift classes can use computed properties, that is, use get and set to indirectly get/change the values of other properties, as shown in the code

classcalcuator{varA:int =1; varB:int =1; varsum:int{Get{            returnA +B}Set(val) {b= Val-a}}}let cal=calcuator ();p rint (cal.sum)//2Cal.sum =5print (cal.b)//4

2, for the set has a shorthand method, shorthand, the newly assigned value defaults to NewValue

class calcuator{    var1;     var 1 ;      var sum:int{        get{            return A + b        }        set  {            = newvalue- a        }}    }

3, if only get, do not set the method can be simply written as follows code

class calcuator{    var1;     var 1 ;      var sum:int{        return A + b    }}

Swift Learning notes-the computed properties of a class

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.