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