在swift中使用继承对象的方法

时间:2015-12-04 13:04:41

标签: ios swift oop inheritance

我有一个可能看起来像这样的对象......

class User {
    var username: String?
} 

我希望拥有可以是学生或教师的用户。目前,我已将属性添加到User类中,如此...

class User {
    var username: String?
    // student properties
    var year: Int?
    // teacher properties
    var department: String?
} 

我确定我应该在这里使用继承,但我担心这会使控制流程变得有点复杂。例如,作为登录功能的一部分,我这样做......

 let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
 appDelegate.user = User(delegate: self)
 appDelegate.user!.load_from_user_defaults()

或者从当前用户那里得到一些东西,我会这样做......

 let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
 var username = appDelegate.user.username

在这种情况下,如果我不知道是教师还是学生登录,我将如何使用继承?是否更容易坚持我的方式?

2 个答案:

答案 0 :(得分:1)

一些选项:

  • 超级/子类模式(继承),您可以使用downcast来检查它是学生还是教师。

  • 协议的工作方式与超级/子模式相同,但type可以符合许多protocols,但只能从一个super继承

  • 如果学生和教师都具有相同的属性,您还可以添加属性(boolenum)以确定哪个属性。

  • 创建一个enum,而不是protocolsuper class rawValue User,并且有教师和学生的案例。 (这很复杂)

  • enum与关联值一起使用。

每种方法都有自己的优点和缺点。 如果您希望能够将User对象传递给应用程序中的不同函数,则需要具有一致性/继承性。

如果您有一些一致性/继承,您可以使用您选择的方法加载用户,然后向下转发:

if let student = user as? Student {
    // do student stuffs
} else if let teacher = user as? Teacher {
    // do teacher stuffs
}

选项1,常规继承:

class User {
    var username: String?
}

class Student : User {
    // student properties
    var year: Int?
}

class Teacher : User {
    // teacher properties
    var department: String?
}

选项2,符合而不是继承,AKA Protcols:

protocol User : class {
    var username: String? { get set }
}

class Student : User {

    var username: String?
    // student properties
    var year: Int?
}

class Teacher : User {

    var username: String?
    // teacher properties
    var department: String?
}

选项3,UserType属性:

enum UserType {
    case Student
    case Teacher
}

class User {
    var username: String?
    // student properties, if it is a teacher we leave this blank
    var year: Int?
    // teacher properties,, if it is a student we leave this blank
    var department: String?

    var type : UserType?
}

选项4,enum UserrawValue

class User: Equatable,StringLiteralConvertible {

    var username: String?
    // student properties
    var year: Int?
    // teacher properties
    var department: String?

    var type : String?

    init(withType type:String) {
        self.type = type
    }

    required convenience init(stringLiteral value: String) {

        self.init(withType: value)
    }

    required convenience init(extendedGraphemeClusterLiteral value: String) {
        self.init(withType: value)
    }

    required convenience init(unicodeScalarLiteral value: String) {
        self.init(withType: value)
    }
}

func ==(lhs:User,rhs:User) -> Bool {
    if lhs.username == rhs.username && lhs.department == rhs.department && lhs.year == rhs.year && lhs.type == rhs.type {
        return true
    } else {
        return false
    }
}


enum UserType : User {

    case Student = "Student"
    case Teacher = "Teacher"

}

let user = UserType.Teacher

选项5,enum及其相关值:

class User {

    var username: String?

}

class Student {
    // student properties
    var year: Int?
}

class Teacher {
    // teacher properties
    var department: String?
}


enum UserType {
    case student(Student)
    case teacher(Teacher)
}

答案 1 :(得分:0)

“可能是这个或那个”:在Swift中,您使用枚举。一个班级为学生,一个班级为教师,一个为“学生或教师”。