直接贴代码 不BB!
import UIKit
var str = "Hello, playground"
//关于集成的方法
class person{
//这是一个字段
var Name:String
//重写init方法,构造方法
init(name:String) {
self.Name = name;
// print(name)
// print("personname = (self.Name)")
}
}
class woman:person{
//字段
var female:String
//构造方法
init(name:String,male:String) {
self.female = male
print("name == \(name)")
//调用父类方法,这个方法调用了之后我的子类的那么就被设置上了。(这里好奇怪为什么会被设置上呢?)
super.init(name: name)
}
}
class Man:person{
var Weight:String
init(name:String,weigth:String) {
self.Weight = weigth
print("name == (name)")
super.init(name: name)
}
}
let ArrayList = [woman(name:"gzz",male:"she"),
woman(name:"gle",male:"she"),
Man(name: "ty", weigth: "180"),
Man(name: "az", weigth: "101"),
Man(name: "az", weigth: "102")
]
print("--------------")
//这里为啥不能加加了我日
var womanCount = 0
var manCount = 0
for itme in ArrayList {
print(itme.Name)
if itme is woman {
womanCount+=1
}
if itme is Man {
manCount+=1
}
}
print("womanCount = (womanCount)")
print("manCount = (manCount)")
for items in ArrayList {
if let _ = items as? woman{
print("woman")
}
if let _ = items as? Man {
print("man")
}
}
//这里要加括号
let objcets:[AnyObject] = [
Man.init(name: "tl", weigth: "180"),
Man.init(name: "WX", weigth: "150"),
Man.init(name: "RZ", weigth: "160"),
]
for objc in objcets {
let man = objc as! Man
print("man:(man.Name)")
}
//var string01:String = obj as! String
//装任何类型
var thisngs = Any
thisngs.append(0)
thisngs.append(3.14159)
thisngs.append(0.111)
thisngs.append("hello")
thisngs.append("3.0,5.0")
thisngs.append(Man.init(name: "wukong", weigth: "130"))
thisngs.append(woman.init(name: "zixia", male: "120"))
//这个是完全看不明白,主要是不明白as的意思,
for this in thisngs {
switch this {
case 0 as Int:
print("Zero as an Int")
case let someDouble as Double where someDouble > 0:
print("a positive double value of(someDouble)")
case let someString as String:
print("a string value of (someString)")
case let (x, y) as (Double, Double):
print("an (x, y) point at (x), (y)")
case let man as Man:
print("Man: (man.Name)")
case let woma as woman:
print("woman: (woma.Name)")
default:
print("something else")
}
}
//还有一个问题是 他的字段 根本呼不出来!
woman.init(name: "wudi", male: "colss")
Man.init(name: "mais", weigth: "hepp")
//as 使用场合,派生类往上转
let man = Man.init(name: "wudi", weigth: "hehe")
let woma = woman.init(name: "女人", male: "160")
let wo = man as person
print(wo)
//(2)消除二义性,数值类型转换
let num1 = 42 as CGFloat
let num2 = 42 as Int
let num3 = 42.5 as Double
//(3)switch 语句中进行模式匹配
var Arraylist:Array = [Man.init(name: "Nanren", weigth: "wudi"),woman.init(name: "Nvren", male: "170")
]
let count = ArrayList.count
//这个外层要加个循环
for itme in ArrayList {
switch itme {
case let m as Man:
print("有man (m.Name)")
case let w as woman:
print("有woman\(w.Name)")
default:
print("something else")
}
}
//3,as?使用场合,向下转型(Downcasting)时使用。这个只能使用!
//父类 用子类的方法
let per:person = Man.init(name: "renlei", weigth: "180")
let pzm = per as! Man
print("基类转化派生类(pzm.Name)");
//as? 和 as! 操作符的转换规则完全一样。
let pers:person = woman.init(name: "人类转女人", male: "170")
if let Pzw = pers as? woman{
print("转化成功")
}else{
print("不成功等于null")
}
//Override简单理解就是这个句话下边的方法是继承父类的方法,对其覆盖