Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to extend existing classes, structures, enumerations

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report