美文网首页
Swift(类之间的关系)

Swift(类之间的关系)

作者: HotRay | 来源:发表于2017-07-19 15:37 被阅读0次

直接贴代码 不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简单理解就是这个句话下边的方法是继承父类的方法,对其覆盖

相关文章

  • Swift(类之间的关系)

    直接贴代码 不BB!import UIKit var str = "Hello, playground"//关于集...

  • Swift(类之间的关系2)

    使用Playground ,继承多态的使用! import UIKitimport Foundation clas...

  • 类之间关系

    类的关系: 介绍几种常见的类与类之间的关系:继承、实现、组合、聚合、依赖。 这里的关系都是语义上的,和语言没有关系...

  • 类之间的关系

    类之间的关系: 依赖、关联、聚合、组合、继承、实现。他们的耦合度依次增强。

  • 类之间的关系

    今天讲类之间的关系,关联关系不是那么好理解和掌握。概念重要,例子更重要,例子讲好了,讲透了。今天做的就不错。依赖关...

  • 类之间的关系

    泛化关系(generalization) 类的继承结构表现在UML中为:泛化(generalize)与实现(rea...

  • 类之间的关系

    父子 兄弟继承同一父类的子类 同事协作关系,处理不同业务,可能没有必然共性 组合被组合对象与主体的生命周期相同,主...

  • 类之间的关系

    http://www.cnblogs.com/kittybo/articles/CPLUSPLUS_02.html...

  • 类之间的关系

    sqlsession 持有 Configurationsqlsession 持有executor Configur...

  • 类之间的关系

    1、前言 类之间主要有两大关系(继承和实现就不讲了),依赖和关联。依赖一般只是使用了对象,他们之间的关系很微弱,几...

网友评论

      本文标题:Swift(类之间的关系)

      本文链接:https://www.haomeiwen.com/subject/aovlkxtx.html