//定义协议
protocol SomeprotocolOne{
func printSomething()
}
protocol SomeprotocolTwo{
}
class SomeClass{
}
//采纳协议
class SomeClassOne:SomeClass,SomeprotocolOne,SomeprotocolTwo{
func printSomething() {
print("Hello World")
}
}
//属性要求//get set 可读可写 get可读 set可写
protocol FullNamed{
var fullname: String {get}
}
class SpellName: FullNamed{
var prefix: String?
var name: String?
init(name:String,prefix:String?=nil){
self.name = name
self.prefix = prefix
}//自定义初始化方法
var fullname:String {
return(prefix != nil ? prefix! + "":"")+name!
}
}
let name = SpellName(name: "阳菜", prefix: "小嶋")
print(name.fullname)
//方法要求
protocol SomeProtocolThree{
static func someTypeClass()
func sum() -> Double
}
protocol Conters {
func sum() -> Double
}
class sumOfThreeNumber:Conters{
var numOne = 0.0
var numTwo = 0.0
var numThree = 0.0
func sum() -> Double {
return numOne * numTwo * numThree
}
}
var sum = sumOfThreeNumber()
print(sum.sum())
sum.numOne = 9.0
sum.numTwo = 23.32
sum.numThree = 534
print(sum.sum())
protocol Toggable{
mutating func toggable()
}
//mutating 在方法里去改变方法所属的实例
enum OnOFFSwitch:Toggable{
case on
case off
mutating func toggable() {
switch self{
case off:
self = on
case on:
self = off
}
}
}
var lightSwitch = OnOFFSwitch.on
lightSwitch.toggable()
//委托代理模式
protocol retentHose {
func Renting()
}
class LianJia:retentHose{
func Renting() {
print("我是家,我可以找房")
}
}
class Lisi:retentHose{
func Renting() {
print("我是李四,我可以找房")
}
}
class myHouse{
var houseDelegate:retentHose?
}
class WU8{
func renting() {
print("我是8,我可以找房")
}
}
var mine = myHouse()
var wu = WU8()
var lisi = Lisi()
var lianjia = LianJia()
mine.houseDelegate = lianjia
mine.houseDelegate?.Renting()
//扩展要求
protocol PrintText{
var textDescription:String{
get
}
}
protocol DrawPicture{
}
class Book : DrawPicture {
}
extension Book : PrintText{
var textDescription: String{
return "可以描述"
}
}
protocol SomeprotocolOne{
func printSomething()
}
protocol SomeprotocolTwo{
}
class SomeClass{
}
//采纳协议
class SomeClassOne:SomeClass,SomeprotocolOne,SomeprotocolTwo{
func printSomething() {
print("Hello World")
}
}
//属性要求//get set 可读可写 get可读 set可写
protocol FullNamed{
var fullname: String {get}
}
class SpellName: FullNamed{
var prefix: String?
var name: String?
init(name:String,prefix:String?=nil){
self.name = name
self.prefix = prefix
}//自定义初始化方法
var fullname:String {
return(prefix != nil ? prefix! + "":"")+name!
}
}
let name = SpellName(name: "阳菜", prefix: "小嶋")
print(name.fullname)
//方法要求
protocol SomeProtocolThree{
static func someTypeClass()
func sum() -> Double
}
protocol Conters {
func sum() -> Double
}
class sumOfThreeNumber:Conters{
var numOne = 0.0
var numTwo = 0.0
var numThree = 0.0
func sum() -> Double {
return numOne * numTwo * numThree
}
}
var sum = sumOfThreeNumber()
print(sum.sum())
sum.numOne = 9.0
sum.numTwo = 23.32
sum.numThree = 534
print(sum.sum())
protocol Toggable{
mutating func toggable()
}
//mutating 在方法里去改变方法所属的实例
enum OnOFFSwitch:Toggable{
case on
case off
mutating func toggable() {
switch self{
case off:
self = on
case on:
self = off
}
}
}
var lightSwitch = OnOFFSwitch.on
lightSwitch.toggable()
//委托代理模式
protocol retentHose {
func Renting()
}
class LianJia:retentHose{
func Renting() {
print("我是家,我可以找房")
}
}
class Lisi:retentHose{
func Renting() {
print("我是李四,我可以找房")
}
}
class myHouse{
var houseDelegate:retentHose?
}
class WU8{
func renting() {
print("我是8,我可以找房")
}
}
var mine = myHouse()
var wu = WU8()
var lisi = Lisi()
var lianjia = LianJia()
mine.houseDelegate = lianjia
mine.houseDelegate?.Renting()
//扩展要求
protocol PrintText{
var textDescription:String{
get
}
}
protocol DrawPicture{
}
class Book : DrawPicture {
}
extension Book : PrintText{
var textDescription: String{
return "可以描述"
}
}