Declaring
class PersonBaseClass {
var firstName: String = ""
var lastName: String = ""
func getFullName () -> String {
return "\(firstName) \(lastName)";
}
}
Subclassing
Terminology in Swift
- Subclassing = Inheriting "I need to subclass ObjectClass." (You need to inherit ObjectClass.)
- Subclass = Derived Class, Child Class
- Superclass = Base Class, Parent Class
In this example we are "subclassing" the PersonBaseClass:
class Mother : PersonBaseClass {
var maidenName: String = ""
func setIdentity (firstName: String, lastName: String) {
// Find members of the superclass by using the word "super".
super.firstName = firstName
super.lastName = lastName
// Find members in this class by using the word "self".
self.maidenName = lastName
}
override func getFullName() -> String {
return super.getFullName() + " (\(maidenName))"
}
}
Instantiating and Using
var me = PersonBaseClass()
me.firstName = "Mark"
me.lastName = "Moeykens"
me.getFullName()
var mom = Mother()
mom.firstName = "Beth"
mom.lastName = "Moeykens"
mom.maidenName = "Marr"
mom.getFullName()
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.