Swift2.2 NO.3 Class

本文通过具体的Swift语言代码示例介绍了函数定义、类的继承与重写等概念,并演示了如何使用这些特性来创建实用的类结构。

//

func sumOf(numbers:Int...)->Int{

    var sum=0

    for number in numbers {

        sum+=number

    }

    return sum

}

sumOf()

sumOf(4,5,12)

 

//

func returnFifteen()-> Int{

    var y=0

    func add(){

        y+=15

    }

    add()

    return y

}

returnFifteen()

 

///// 

 

////

class Shape{

    var  numberOfSides=0

    func simpleDescription() -> String{

        return "A shape with \(numberOfSides)"

    }

}

 

var shape = Shape()

shape.numberOfSides = 7

var shapedescripton=shape.simpleDescription();

 

class NameShape{

    var numberOfSides:Int=0

    

    var name:String

    init (name:String)

    {

        self.name=name

        

    }

 

    func simpleDescription ()->String

    {

                           return  "A shape with \(numberOfSides) sides."

    }

}

 

class Square:NameShape{

    

    var sideLength:Double

    

    init(sideLength:Double ,name :String){

        self.sideLength=sideLength

    

        super.init(name: name)

        numberOfSides=8

        

    

        }

        func area()->Double{

                return sideLength * sideLength

        }

    

    override func simpleDescription() -> String {

            return "A square with sides of length \(sideLength)"

        }

 

}

let test = Square(sideLength: 5, name: "my test square")

test.area()

test.simpleDescription()

 

转载于:https://www.cnblogs.com/yshgxm/p/5236968.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值