美文网首页
定义BookClass

定义BookClass

作者: 哈喽Mm | 来源:发表于2017-12-11 09:24 被阅读0次

    //定义图书属性类BookClass 属性类中添加:Bookcls(分类)、BookName(书名)、Price(价格)

    class BookClass {

    var Bookcls = ""

    var BookName = ""

    var Price = 0.0

    }

    //实例化五个BookClass类的对象

    let objBookA = BookClass()

    let objBookB = BookClass()

    let objBookC = BookClass()

    let objBookD = BookClass()

    let objBookE = BookClass()

    //创建数组

    var ArrayBook = [Any]()

    //初始化对象

    objBookA.Bookcls = "励志"

    objBookB.Bookcls = "文学"

    objBookC.Bookcls = "小说"

    objBookD.Bookcls = "少儿"

    objBookE.Bookcls = "科技"

    objBookA.BookName = "钢铁人"

    objBookB.BookName = "小王子"

    objBookC.BookName = "平凡世界"

    objBookD.BookName = "三毛儿"

    objBookE.BookName = "探索"

    objBookA.Price = 45.0

    objBookB.Price = 35.7

    objBookC.Price = 40

    objBookD.Price = 47.9

    objBookE.Price = 77.0

    //添加到数组里

    ArrayBook = [objBookA,objBookB,objBookC,objBookD,objBookE]

    //打印所有对象的内容

    print(ArrayBook)

    //打印价格最贵的图书的信息

    print("最贵的图书信息:分类:\(objBookE.Bookcls),书名:\(objBookE.BookName),价格:\(objBookE.Price)")

    //打印objBookC.Bookcls  == “小说”的图书的信息

    print("小说的图书信息:分类:\(objBookC.Bookcls),书名:\(objBookC.BookName),价格:\(objBookC.Price)")

    相关文章

      网友评论

          本文标题:定义BookClass

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