// 1.枚举
//enum 枚举命: 类型{
//
// case 分支1 = 赋值1
// case 分支2 = 赋值2
//
//}
enum PersonIndentity: String {
case Teacher = "Teacher_id"
case Student = "Student_id"
}
// 类
class Person {
var indentity: PersonIndentity?
var name: String?
var age: String
// 类的构造器
init (name:String , age: String, idd:PersonIndentity) {
self.name = name
self.age = age
self.indentity = idd
}
func hello() {
print("hello")
}
// 类型方法
class func hello2() {
}
}
// 枚举 枚举名.类型 eg:PersonIndentity.Student
// 枚举名可以省略
var person = Person(name: "林林", age: "90", idd: PersonIndentity.Student)
// 枚举值的获取
// 获取当前分支
person.indentity?.hashValue
// 获取分支的值
person.indentity?.rawValue
// 2.继承 类名: 父类名
class Student: Person {
var classNumber: Int = 23
init(name: String, age: String, idd: PersonIndentity, classNum:Int) {
// 如果属性仅指定类型,需要在super,init前进行赋值操作
self.classNumber = classNum
super.init(name: name, age: age, idd: idd)
}
// 重写父类方法 需要重写override关键字
override func hello() {
}
override class func hello2() {
}
}
// 3.协议
protocol OneProtocol {
func typeFunc ()
static func typeFunc2()
mutating func typeFunc3()
}
// 类和结构体实现协议方法的时候 需要根据
class StudentNew:OneProtocol {
func typeFunc() {
}
class func typeFunc2() {
}
func typeFunc3() {
}
}
struct VideoNew:OneProtocol {
func typeFunc() {
}
static func typeFunc2() {
}
mutating func typeFunc3() {
}
}
// 类同时继承父类和协议的时候 父类必须写在前面
@objc protocol TwoProtocol {
optional func byebye()
}
// 当协议中方法使用 optional 声明可选时 协议必须声明成object 此时协议为类的协议: class protocol
// 并且不能被结构体继承
//struct Struct2:TwoProtocol {
//
//
//}
// 4.扩展
var value :String = ""
extension Person {
func hello5 (){
}
// 如果想扩展属性 只能是计算属性
var stu2:String {
get{
return value
}
set {
value = newValue
}
}
// 扩展构造器的时候需要使用 convenience
convenience init (name:String , age: String, idd:PersonIndentity, stu2:String) {
self.init(name:name, age:age, idd:idd)
self.stu2 = stu2
}
}
var person4 = Person(name: "毛毛", age: "80", idd: .Student, stu2: "笨蛋")
person4.stu2
person4.stu2 = "sb"
person4.stu2