Swift's struct struct type describes the use

Source: Internet
Author: User

<span style= "FONT-SIZE:24PX;" >struct David {var x = 0;//defines a struct, two fields x, y var y = 0;//Initial value//constructor init () defining struct () {//define an empty constructor, real constructor            , call time without parameter call here//The constructor is started with Init, the automatic call//[[OC Alloc]init] is not a constructor println ("in Init");        } init (x:int,y:int) {self.x = x;//defines a constructor with 2 parameters, which is called here with a double argument self.y = y;    println ("In Init (x:y:)")}//struct has a constructor, no destructor func getcenter ()->int{return (x+y)/2; }//Give the existing point x, y plus offset//obj.addoffset (100,deltay:) mutating func addoffset (deltax:int,deltay:int) {//struct is copy        Object, function inside cannot modify variable//need to modify, add keyword mutating,//mutating, can modify constructor internal variable x + = DeltaX;    Y + = DeltaY; }};func Testdavid () {///define a struct//struct is defined as the struct's name (parameter name: parameter value) var d = David ();//Call Constructor init () var s = David (x:100, Y:200);//Call Constructor init (x, y) println ("s.x = \ (s.x), s.y = \ (s.y)") Let C = S.getcenter ();//Call destructor Getcenter print        ln ("C is \ (c)"); S.addoffset (100, DELTAY:200);//Because the destructor does not return a value, it cannot be assigned to the variable println ("D is: \ (s.x)");} Testdavid ();//test function call </span>


Swift's struct struct type describes the use

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.