Swift Object-oriented enumeration

Source: Internet
Author: User
Tags inheritance
Main.swift//Swift Object-oriented enumeration////Created by Goddog on 15/7/17. COPYRIGHT©2015 year Goddog.
All rights reserved. Import Foundation print ("Swift is not only process-oriented, but also object-orientated, Swift can define enumerations, structs, Class 3 object-oriented types, variables defined with enumerations and structs are value types, variables defined by classes are reference types, 3 Object-oriented supports defining storage properties, computed properties, methods, subscripts, constructors, and nested types ") print (" Swift5 Object-oriented program units: classes, structs, enumerations, extensions, Protocols ") print (" Steps for Defining a type: 1. declaring variables; 2. Creating an instance; 3. Access attributes; 4. Calling method; 5. Derived subclasses (enumerations and structs do not support inheritance)//mark:-Define enum enum Season {case Spring case Summer case Fall case Win ter}//or enum Weekday {case Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}//use enum to declare variable var day:wee KDAY//Using an existing enumeration assignment day = Weekday.saturday print ("Result: \ (day)")//mark:-enumeration value and switch statement var chooseday = weekday.monday Switch C Hooseday {case. Monday:print ("Monday") case. Tuesday:print ("Tuesday") case. Wednesday:print ("Wednesday") case. Thursday:print ("Thursday") case. Friday:print ("Friday") case. Saturday:print ("Saturday") default:print ("Sunday")}//mark: -Original value//define enumeration, specify original value Int enum Weekdays:int {case Monday, Tuesday = 1, Wednessday = 5, Thursday, Friday, Saturday, Sun Nday} var days = Weekdays.monday Print ("Monday are: \ (days.rawvalue)")//0 is automatically determined by tuesday=1 days =. Friday print ("Friday is: \ (days.rawvalue)")//7 is an enum based on wednessday=5 auto-judgment Seasons:character {//Specify the original value for each enumeration CAs E Spring = "s" Case Summer = "U" Case Fall = "F" Case Winter = "W"}//Get enumeration based on original value var Getseason = Seasons (RAWV
    Alue: "s") if Getseason! = nil {switch (getseason!) {case. Spring:print ("Spring") case. Summer:print ("Xia") case. 
    Fall:print ("Autumn") Default:print ("Winter")}}//mark:-associative value enum Planet {//different enumerations can carry an unequal number of associated values of unequal type Case Mercury (weight:double, density:double, name:string) Case Venus (double, double) case Earth (double, Str ing) case Mars (density:double, name:string) Case Jupiter Case Saturn case Uranus case Nepturn} var P1 = Planet.mercurY (weight:0.05, density:5.43, Name: "Mercury") var P2 = Planet.venus (0.815, 5.25) var p3 = Planet.earth (1.0, "Earth") var P4 = Pl Anet. Mars (density:3.95, Name: "Mars") switch (p3) {case Planet.earth (var weight,var name): Print ("\ (name), \ (weigh T) ") default:break} switch (p1) {case-let. Venus (weight, density): Print ("\ (weight), \ (density)") case-let.









 Mercury (Weight:w, Density:d, name:n): Print ("\ (w), \ (d), \ (n)") Default:break}
Swift is not only process oriented or object oriented, Swift can define enumerations, structs, Class 3 object-oriented types, variables defined with enumerations and structs are value types, variables defined by classes are reference types, and 3 object-oriented objects support the definition of storage properties, computed properties, methods, subscripts, Constructors and nested types
SWIFT5 Object-oriented program units: classes, structs, enumerations, extensions, Protocol
definition types steps: 1. Declaring variables, 2. Creating instances, 3. accessing properties; 4. Calling methods; 5. Derived subclasses (enumerations and structs do not support inheritance)
The result: One of Swift's object-oriented objects. Weekday.saturday
Monday
Monday is:0
Friday is:7
spring
earth,  1.0
0.05,  5.43,  Mercury program
ended with exit code:0


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.