使用用户定义的类

时间:2019-02-17 18:17:21

标签: swift

我正在尝试实例化一个名为Rock的用户定义对象。使用此代码,“ contains”是Item类型的变量,Rock和Bug都是Item的子类。当我尝试实例化Rock时,代码会生成错误,但Bug不会。这不是您实例化课程的方式吗?它说:“无法将Rock.Type类型的值分配为'item'。

我还想知道如何编写代码来询问对象“包含”它包含什么类的对象。有功能吗?

    for r in 0...map.MAXROWS{
        for c in 0...map.MAXCOLUMNS{
            if (r < 3) || (r > (MAXROWS - 3)){
                self.contains = Rock
            }else if (c < 3) || (c > (MAXCOLUMNS - 3)){
                self.contains = Rock
            }else if (r == 3) && (c == 100){
                self.contains = Bug(index: 1, map: map)
            }else if (arc4random() * 100 == 1){
                self.contains = Bug(index: current, map: map)
                current = current + 1
            }else if (arc4random() * 8) == 1{
                self.contains = Rock
            }
        }
    }


import Cocoa

class Bug: Item {
    var male = true
    var direction = 1
    var appearance = 1
    var positionRow = 0
    var positionColumn = 0

    // Direction:
    // 1 = Up
    // 2 = Right
    // 3 = Down
    // 4 = Left

    init(index: Int, map: Map){
        super.init()
        map.initializeBugs(index: index, current: self)
        if (index == 1){
            self.direction = 3
            self.positionRow = 3
            self.positionColumn = 100
        }else{
            self.direction = (Int(arc4random() * 4)) + 1
        }
        Birth(index: index)
    }

    func Birth(index: Int){
        if (arc4random() * 8) == 1{
            self.male = false
        }else{
            self.male = true
            if (arc4random() * 2) == 1{
                self.appearance = 1
            }else{
                self.appearance = 2
            }
        }
    }

    func DisplayBug() -> Int{
        if self.male == false{
            return self.direction
        }else{
            if self.appearance == 1{
                switch self.direction{
                case 1:
                    return 5
                case 2:
                    return 6
                case 3:
                    return 7
                case 4:
                    return 8
                default:
                    return 0
                }
            }else if self.appearance == 2{
                switch self.direction{
                case 1:
                    return 9
                case 2:
                    return 10
                case 3:
                    return 11
                case 4:
                    return 12
                default:
                    return 0
                }
            }
        }
    }

    func WhereAreYouRow() -> Int{
        return self.positionRow
    }

    func WhereAreYouColumn() -> Int{
        return self.positionColumn
    }

    func AreYouBug() -> Bool{
        return true
    }

    func InstantiateBug{

    }
}



import Cocoa

class Rock: Item {

    override init(){
        super.init()
    }

    func AreYouRock() -> Bool{
        return true
    }

1 个答案:

答案 0 :(得分:0)

您需要使用构造函数实例化诸如Rock()Bug()

之类的类

仅调用RockBug而不引用()时,您要引用的类是要创建一个对象,因此需要() < / p>


简而言之:

RockBug

对象Rock()Bug()

因此在您的代码中,您想分配self.contains = Object,这意味着:

self.contains = Rock()self.contains = Bug()