1 import Foundation 2 3 class Hi{ 4 func sayHi(){ 5 print("HI jinpanpang") 6 } 7 } 8 9 class Hello:Hi { 10 var _name:String 11 12 // 有参构造函数 13 init(name:String) { 14 self._name = name 15 print("init hello") 16 } 17 18 // 重写父类方法 19 override func sayHi() { 20 print("override \(self._name)") 21 } 22 } 23 24 var hi = Hi() 25 hi.sayHi(); 26 27 var h = Hello(name: "zhangsan") 28 h.sayHi(); 29 30 /* 输出结果 31 HI jinpanpang 32 init hello 33 override zhangsan 34 Program ended with exit code: 0 35 */
时间: 2024-10-06 19:13:51