Swift中的类,结构体和枚举可以进行嵌套,即在某一类型的内部定义类,这种类型嵌套在JAVA中称为内部类,在C#中称为嵌套类,它们的形式和定义是相似的,类型嵌套的有点是能够访问它外部的成员,包括方法,属性和其他的嵌套类型,嵌套还可以有多个层次
示例:
class Employee{ var no: Int = 0 var name : String ="" var job : String ="" var salary : Double = 0 var dept:Department=Department() var day: WeekDays = WeekDays.Friday } struct Department{ var no : Int = 100 var name:String ="sales" } enum WeekDays{ case Monday case Tuesday case Wednesday
case Thursday case Friday struct Day{ static var message : String = "Today is ...." } } } var emp = Employee() println(emp.dept.name) println(emp.day)
时间: 2024-10-22 18:46:22