正在加载图片...
多操作符 instanceof AvA o在接收父类的一个引用时,可以通过使用 iNstanced 运算符判定该对象实际上是否为你所要的子类,并 可以用类型转换该引用的办法来恢复对象的全部功 能 public void method (Employee e)t if (e instanceof Manager // Get benefits and options along wiy如果不用 Else if(e instanceof Contractor)i d instanceoftsiN / Get hourly rates 试,就会有类 型转换失败的 felse t 危险。 / regular employee JAVA操作符instanceof 在接收父类的一个引用时,可以通过使用Instanceof 运算符判定该对象实际上是否为你所要的子类,并 可以用类型转换该引用的办法来恢复对象的全部功 能。 public void method(Employee e) { if (e instanceof Manager) { // Get benefits and options along with salary }else if (e instanceof Contractor) { // Get hourly rates }else { // regular employee } } 如果不用 instanceof做测 试,就会有类 型转换失败的 危险
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有