Study Notes for swift Language 4 (class objects and functions)

Source: Internet
Author: User
Tags intel core i5

Fengsh998 original address: Sorry!


Swift extends many features and attributes, some of which are odd. I learned it slowly and summarized it through experience.

The following describes how to write a class.

The biggest hobby of a code engineer is reading code, not text. It's too boring.

/// Computer. swift // swiftdemo /// created by Apple on 14-6-8. // copyright (c) February 11, 2014 fensh. all rights reserved. /* The purpose of this example is to quickly learn the swift writing method, including knowledge points: 1. attribute settings 2. construction and Interpretation 3. interface implementation polymorphism 4. function overloading (especially for friends who do not need to pay attention to the overload keyword Delphi) and rewriting (override) 5. class Functions (static member functions) 6. declarations of various functions, including struct, default value, multiple responses, multiple output sequences, multiple undefined functions, and inner concatenation functions 7. function type variable. The function address is used as the callback parameter and the return function address (not completed yet, learning) 8. singleton 9. new swift functions: willset, didset @ lazy attribute 10. (learning more) */import foundationvar instance: comput Er? Let unk = "unknow" // display screen width and height struct monitorwh {var width = 0 var Height = 0 var resolution = 0.0 // resolution} // coroutine, interface, implement multiple inheritance protocols protocolcomputer {var price: Double {Get} // only the get method func runcomputer ()} // computer type Enum computertype: int {case none case book // notebook case superbook // super notebook case home // home computer} func callbackwhenstarting () // COMPUTER: Computer {}// computer class computer: nsobject, protocolcomputer {var CP U = unk // CPU var memory = unk // memory var harddisk = unk // hard disk var monitor = unk // display var cpname = unk // brand var computertype: computertype =. NONE // @ lazy // What is the function of this keyword declaration ???? // Inherit the interface property var price: Double = 0.0 // willset didset property var totalprice: Int = 0 {willset (newtotalprice) {// use the new + variable name for the number of dimensions and use the uppercase println ("to set the totalprice value (original value: \ (totalprice) to \ (newtotalprice)") for the first address of the variable name )") // To Do somthing before set .} didset {If totalprice> oldvalue {println ("after setting, add the new value to the old value? \ (Totalprice-oldvalue) ") }}// declare a set, get attribute var computerprice: Double {get {println (" You call computerprice. ") return price} set {price = newvalue println (" You set computerprice value is \ (price) ") }}// default structure Init () {println ("Default creatrustor is called. ")} // The default structure cannot coexist with Init () // convenience Init () {// self. init (computername: "unknow", price: 0) //} // you can define the constructor Init (computername: String, PR Ice: Double) {println ("Custom creatrustor is called.") self. cpname = computername self. Price = Price} // deinit {println ("this is destory? ")} Func description ()-> string {// I really don't know how to wrap the line to write code. I can use \ and now return" computer description: product \ (self. cpname), type is \ (self. computertype. toraw (), CPU is \ (self. CPU), memory is \ (self. memory), disk is \ (self. harddisk), monitor is \ (self. monitor), price is \ (self. price) "} // class function (+ number operation in OC, static function in C/C ++) Class func compute instance ()-> Computer {return computer ()} // start and shut down (without the return value function) f UNC operationcomputer (onoroff: bool) {If onoroff {println ("computer is starting")} else {println ("computer is stopping") }}// no operation, no return value function func computerrunning () {println ("computer is running")} // multiple return values (that is, number of output records) func getcomputerconfig ()-> (CPU: string, HD: String, Mem: String, mon: string) {return (self. CPU, self. harddisk, self. memory, self. monitor)} // use the inout worker count as the output worker count func getcomputerconfig (inout CPU: S Tring, inout HD: String, inout mem: String, inout Mon: string) {CPU = self. CPU Hd = self. harddisk mem = self. memory MON = self. monitor} // a number of external functions (the purpose is to make the caller more aware of the detailed meanings of each number of workers) // computercpu, withcomputerharddisk, withcomputermemory, withcomputermonitor is an external worker. // func setcomputerconfig must be included in the call. (computercpu: String, withcomputerharddisk HD: String, withcomputermemory mem: String, withcomputermonitor Mon: string) {Self. CPU = CPU self. harddisk = HD self. memory = mem self. monitor = mon} // use # To promote the variable name that has the effect of multiple external variables, in this way, you do not need to write the number of external partitions (used when the number of external partitions is the same as the variable name) func setcomputerconfig (# CPU: String, Disk: String, Mem: String, mon: string) {self. CPU = CPU self. harddisk = Disk Self. memory = mem self. monitor = mon} // default func macbookpro (pname: String = "apple", CPU: String = "Intel Core I5", type: computertype, Mem: string = "2G", Disk: String, mon: String = "Intel HD graphics 4000") {self. CPU = CPU self. harddisk = Disk Self. memory = mem self. monitor = mon self. cpname = pname self. computertype = type} // variable number of workers func usbnumbers (usbs: String ...) -> string {var RET: String = "" for USB in usbs {println (USB) RET + = (USB + ",")} return ret} // The number of constants and the number of variables. // although the function has changed the version, the original value of func lookwindowsversion (VAR version: string)-> st is not affected. Ring {version = "default Windows" + version return version} // mutating func getresolution (pname: string)-> monitorwh {var Mt = monitorwh (width: 1364, height: 1280, resolution: 16/9) If pname = "phripse" {Mt = monitorwh (width: 5555, height: 3333, resolution: 29/10 )} return Mt} // function as Callback data transmission // var callbackwhenstarting: ()-> () = callbackwhenstarting // function as return value // function as variable definition // nested function func opentask () {Func openothertask () {println ("open other task")} println ("Open Task")} // function rewrite func lookcomputerbasichardinfo (COMPUTER: Computer) {} // implement func runcomputer () {println ("computer run. ")} class lenove: Computer {override func lookcomputerbasichardinfo (COMPUTER: Computer) {If computer is lenove // is as operation. {Println ("this is Lenovo ")}}}

Call Demo:

// Var CPT = computer () // call the default build var CPT = computer (computername: "apple", price: 12000) // call the custom build println (CPT. description) println (CPT. getcomputerconfig () // property of the benchmark test println ("Price: \ (CPT. computerprice) ") CPT. computerprice = 2000.0; println ("the configured price is \ (CPT. computerprice) ") // retry willset didset CPT. totalprice = 100; CPT. totalprice = 400; CPT. totalprice = 900; var A = "", B = "", c = "", D = "" CPT. getcomputerconfig (& A, HD: & B, Mem: & C, mon: & D) println ("A = \ (a), B = \ (B ), C = \ (C), D = \ (d) ") CPT. setcomputerconfig (computercpu: "Inter I5", withcomputerharddisk: "WD 500", withcomputermemory: "4G", withcomputermonitor: "phripse") println ("latest configuration: \ (CPT. description) ") CPT. setcomputerconfig (CPU: "amd", Disk: "HD 1 t", Mem: "8g", mon: "Samsung") println ("latest configuration: \ (CPT. description) ") // use the default value to call the CPT function. macbookpro (type: computertype. book, Disk: "5") println ("Pingguo configuration: \ (CPT. description) ") Let usbsupporttype = CPT. usbnumbers ("2.0", "3.0") println ("supported USB interfaces: \ (usbsupporttype)") Let extentusbtype = CPT. usbnumbers ("5.0") println ("extended USB interface: \ (extentusbtype)") var version = "XP 3"; let newversion = CPT. lookwindowsversion (version); println (Version) println (newversion)

Output:

Custom creatrustor is called. computer Description: Product apple, type is 0, CPU is unknow, memory is unknow, disk is unknow, monitor is unknow, price is 12000.0 (unknow, unknow) you call computerprice. price: 120000000you set computerprice value is 20000000you call computerprice. the set price is: 2000.0. Are you sure you want to set the totalprice value (original value: 0) to: 100. After the set value is greater than the old value? 100 are you going to set the totalprice value (original value: 100) to: 400, and then add the new value to the old value? 300 are you going to set the totalprice value (original value: 400) to: 900, and then add the new value to the old value? Up to 500a = unknow, B = unknow, c = unknow, D = unknow latest configuration: computer description: Product apple, type is 0, CPU is inter I5, memory is 4G, disk is WD 500, monitor is phripse, price is 2000.0 latest configuration: computer description: Product apple, type is 0, CPU is AMD, memory is 8g, disk is HD 1 t, monitor is Samsung, price is 2000.0 Pingguo configuration: computer description: Product apple, type is 1, CPU is Intel Core I5, memory is 2g, disk I S 5, monitor is Intel HD graphics 4000, price is 200020.2.03.0 support USB interface: 2.0, 3.0,) 5.0 extended USB interface: 5.0, XP 3 default Windows XP 3 this is destory?

It is better to write and learn by yourself. You may not know what it is.

Thank you. Because it is an English document, my head is also more painful. I have to guess and execute it to understand it, and some are not in good condition. Some do not understand, so they are not organized.

Everyone learns and makes progress together.


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.