Swift 2.0 Study notes (day 46)--Subscript rewrite

Source: Internet
Author: User

Original articles, welcome reprint. Reprint Please specify: Dongsheng's Blog

Subscript is a special property. A subclass property override is a getter and setter accessor that overrides a property, and an override of subscript is also an overriding getter and setter accessor.

Let's look at an example:

classDoubledimensionalarray {Let rows:int, Columns:intvargrid: [Int] Init (rows:int, columns:int) {self.rows=rows Self.columns=Columns Grid= Array (count:rows * columns, Repeatedvalue:0)} subscript (Row:int, Col:int)-Int {//Define Subscript               Get {            returngrid[(Row * columns) +Col]} Set{grid[(row* columns) + col] =NewValue}} //Define Subscript  }classSquarematrix:doubledimensionalarray {OverrideSubscript (Row:int, col:int), Int {//overriding the parent class subscript              Get {                 returnsuper.grid[(Row * columns) +Col]
} Set{super.grid[(row* columns) + col] = newvalue *NewValue}}
} varAry2 = Squarematrix (rows:5, Columns:5) for vari =0; I <5; i++ { for varj =0; J <5; J + +{Ary2[i,j]= i +J}} for vari =0; I <5; i++ { for varj =0; J <5; J + +{print ("\t\t \ (ary2[i,j])")} print ("\ n")}

where super.grid[(row * columns) + COL] statement uses Super to call the parent class's Grid property.

where super.grid[(row * columns) + col] = newvalue * NewValue statement is assigned a value to the parent class's Grid property.

Welcome to follow Dongsheng Sina Weibo @tony_ Dongsheng.
Learn about the latest technical articles, books, tutorials and information on the public platform of the smart Jie classroom
?
More Products iOS, Cocos, mobile design courses please pay attention to the official website of Chi Jie Classroom: http://www.zhijieketang.com
Luxgen Classroom Forum Website: http://51work6.com/forum.php

Swift 2.0 Study notes (day 46)--Subscript rewrite

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.