Get the App
SLTechnology News&Howtos  ›  Development  › 

How to extend existing classes, structures, enumerations

Shulou Source: shulou.com Published: 2022-06-03 06:08:23 11月13日 Update

This article mainly introduces "how to expand existing classes, structures, enumerations". In daily operations, I believe many people have doubts about how to expand existing classes, structures, and enumerations. I hope it will be helpful to answer the doubts of "how to expand existing classes, structures, and enumerations". Sort out simple and useful operation methods! Next, please follow the editor to study!

Extended calculation attribute / / extended Double calculation property extension Double {var km: Double {return self * 10000.0} var m: Double {return self} var cm: Double {return self / 100.0} var mm: Double {return self / 10.0000.0} var ft: Double {return self / 3.28084} let oneInch = 25.4.mmprint ("One inch is\ (oneInch) meters") / / One inch is 0.0254 meterslet threeFeet = 3.ftprint ("Three feet is\ (threeFeet) meters") / / Three feet is 0.914399970739201 meters extension initializer / / extension initializer struct Rect {var x = 0.0 Y = 0.0 var width = 0.0, height = 0.0} extension Rect {init (pos: [Double], size: [Double]) {self.init (x: pos [0], y: pos [1], width: size [0], height: size [1])}} let r = Rect (pos: [0.0,0.0], size: [4.0] 4]) extension method / / extension method extension Int {func repeatTalk (talk: ()-> Void) {for _ in 0. Int {var base = 1 for _ in 0.. 0: return. Positive default: return. Negative}} print (4.kind) / / positive, the study on "how to extend existing classes, structures, enumerations" is over. I hope you can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Structure learning method subscript example attribute more type help practical next article theory knowledge article website material follow question easy to use Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei macOS Docker Shulou Tech Info MariaDB