麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Java > 正文

java中instanceof和getClass()的區(qū)別分析

2019-11-26 16:10:16
字體:
供稿:網(wǎng)友

class A { } 

class B extends A { } 

Object o1 = new A(); 
Object o2 = new B(); 

o1 instanceof A => true 
o1 instanceof B => false 
o2 instanceof A => true // <================ HERE 
o2 instanceof B => true 

o1.getClass().equals(A.class) => true 
o1.getClass().equals(B.class) => false 
o2.getClass().equals(A.class) => false // <===============HERE 
o2.getClass().equals(B.class) => true 


getClass() will be useful when you want to make sure your instance is NOT a subclass of the class you are comparing with.

一個非常完美的equals方法的寫法:

復(fù)制代碼 代碼如下:

   public boolean equals(Object otherObject)
   {
      // a quick test to see if the objects are identical
      if (this == otherObject) return true;

      // must return false if the explicit parameter is null
      if (otherObject == null) return false;

      // if the classes don't match, they can't be equal
      if (getClass() != otherObject.getClass()) return false;

      // now we know otherObject is a non-null Employee
      Employee other = (Employee) otherObject;

      // test whether the fields have identical values
      return name.equals(other.name) && salary == other.salary && hireDay.equals(other.hireDay);
   }

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 日韩av电影免费在线观看 | 成人毛片免费 | 国产精品久久久久无码av | 欧美国产一区二区三区激情无套 | 久草久视频 | 免费国产自久久久久三四区久久 | 免费看欧美一级特黄a毛片 九色com | 美国黄色毛片女人性生活片 | 成片免费观看大全 | 国产精品欧美久久久久一区二区 | 性欧美视频在线观看 | 蜜桃视频在线播放 | 青青草最新网址 | 成年人在线视频观看 | xxxx18韩国护士hd老师 | 欧美巨乳在线观看 | 久草在线新时代视觉 | 国产一级毛片高清视频完整版 | 成人毛片免费 | 艹男人的日日夜夜 | 国产精品探花在线观看 | 国产乱淫av| 国产一区二区视频网站 | 国产一区二区三区四区五区在线 | 亚洲一级电影在线观看 | 欧美日韩1区2区 | 久久福利在线 | 久久久三区| 一级片免费在线 | 黄色片免费在线 | 免费网站看v片在线a | 久久国产精 | 亚久久 | 中国fx性欧美xxxx | av在线电影网站 | 成人精品| sm高h视频 | 亚洲特黄 | 色播一区 | av成人在线免费观看 | 一级黄色影片在线观看 |