前端之家收集整理的这篇文章主要介绍了
Swift学习笔记-面向对象,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
/** * enum 枚举名 { //使用case关键字列出所有的枚举值 //枚举的其他成员 } */
enum People
{
case Man
case Female
}
enum StrongPeople
{
case Man,Female
}
/** * 面向对象编程的五大点(牢记) 1,声明变量 2.创建实例 3,访问类型属性 4调用类型方法 5,派生子类,但是Swift只有类支持继承,枚举和结构体不支持继承 */
var manPeople :StrongPeople
manPeople = StrongPeople.Man
println(manPeople)
var choosePeople = StrongPeople.Man
switch (choosePeople)
{
case StrongPeople.Man:
println("是个男的")
case StrongPeople.Female:
println("是个女的")
}
/** * enum 枚举名 : 原始类型 { case 枚举值 = 原始值 } */
enum Weekday : Int
{
case Monday,Tuesday = 1,Wednesday = 5,Thursday,Friday
}
enum Season : Character
{
case Spring = "S"
case Summer = "U"
}
var day = Weekday.Monday
println("\(day.rawValue)")
day = .Thursday
println("\(day.rawValue)")
var mySea = Season(rawValue: "S")
if mySea != nil
{
switch mySea!
{
case .Spring:
println("is U")
case .Summer:
println("is S")
}
}
/** * 语法格式 case 枚举值(元组语法) */
enum Plant
{
case Mercury(weight:Double,density:Double,name:String)
case valus(Double,Double)
case Jupiter
case Saturn
}
class Dog1
{
func jump()
{
println("正在jump")
}
func run()
{
jump()
println("正在跑")
}
}
class Wolf
{
var name : String = ""
var age : Int = 2
init (name :String,age : Int)
{
self.name = name
self.age = age
}
func info()
{
println("\(name),\(age)")
}
}
var wolf = Wolf(name:"JiangJum",age: 24)
wolf.info()
class ReturnSelf
{
var age : Int = 0
func grow() -> ReturnSelf
{
age++
return self
}
}
var rt = ReturnSelf()
rt.grow()
.grow()
.grow()
println("\(rt.age)")
struct FixedLengthRange {
var strat :Int
let length : Int
}
var rg = FixedLengthRange(strat: 2,length: 3)
println("\(rg.strat),\(rg.length)")
rg.strat = 5
struct FkRange {
var start : Int
var length : Int
}
let rg1 = FkRange(start: 3,length: 4)
println("\(rg1.start),\(rg1.length)")
class Dept3
{
var id : Int
var info : String
init (id : Int)
{
self.id = id
NSThread.sleepForTimeInterval(2)
self.info = "模拟读取数据"
}
}
class User {
var id : Int = 0
lazy var dept = Dept3(id: 3)
var nicks = [String]()
}
var user = User()
user.nicks.append("JiangJun")
user.nicks.append("jiangjum")
println(user.nicks)