加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift - The Prototype Pattern

发布时间:2020-12-14 01:56:15 所属栏目:百科 来源:网络整理
导读:import Foundation class Sum : NSObject , NSCopying { var resultsCache: [[ Int ]]; var firstValue: Int ; var secondValue: Int ; init (first: Int ,second: Int ) { resultsCache = [[ Int ]](count: 10 ,repeatedValue: [ Int ](count: 10 ,repeated

import Foundation


class Sum : NSObject,NSCopying {

var resultsCache: [[Int]];

var firstValue:Int;

var secondValue:Int;

init(first:Int,second:Int) {

resultsCache = [[Int]](count: 10,repeatedValue:

[Int](count:10,repeatedValue: 0));

for i in 0..<10 {

for j in 0..<10 {

resultsCache[i][j] = i + j;

}

}

self.firstValue = first;

self.secondValue = second;

}

private init(first:Int,second:Int,cache:[[Int]]) {

self.firstValue = first;

self.secondValue = second;

resultsCache = cache;

}

var Result:Int {

get {

return firstValue < resultsCache.count

&& secondValue < resultsCache[firstValue].count

? resultsCache[firstValue][secondValue]

: firstValue + secondValue;

}

}

func copyWithZone(zone: NSZone) -> AnyObject {

return Sum(first:self.firstValue,

second: self.secondValue,

cache: self.resultsCache);

}

}


var prototype = Sum(first:0,second:9);

var calc1 = prototype.Result;

var clone = prototype.copy() as! Sum;

clone.firstValue = 3; clone.secondValue = 8;

var calc2 = clone.Result;


println("Calc1: (calc1) Calc2: (calc2)");

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读