對像操作符 ?
如果使用父類定義對像 "person",并 new 子類實例 "Student()" 賦值,該對像不能訪問子類定義的方法,為什么使用對像操作符(if(xxx is xxxx)){} 就可以訪問 ? 這個不太理解.
if(person?is?Student)?{ ??person.study(); }
如果使用父類定義對像 "person",并 new 子類實例 "Student()" 賦值,該對像不能訪問子類定義的方法,為什么使用對像操作符(if(xxx is xxxx)){} 就可以訪問 ? 這個不太理解.
if(person?is?Student)?{ ??person.study(); }
2020-09-28
舉報
2021-09-13
相當于做了判斷,只有是studet實例才會走到if內部,反過來走到if內部的一定是student,所以可以訪問student方法
2020-09-29
可以假設訪問但實際沒有執行也不會執行 因為person is Student 是false。