Java類和成員上的一些方法實例代碼
isInstance和isAssignableFrom
obj instanceof Class
判斷obj是不是Class或者Class的子類的實例
clazz.isInstance(obj)
判斷obj能不能強(qiáng)制轉(zhuǎn)換成clazz類型,亦即obj是不是clazz或者clazz的子類的實例
clazz1.isAssignableFrom(clazz2)
如果clazz2和clazz1相同,或者clazz1是clazz2的父類則返回True,否則返回Flase
static class Parent{ } static class Son extends Parent{ } public static void main(String[] args) { Parent parent=new Parent(); Son son=new Son(); Assert.assertTrue(son instanceof Son); Assert.assertTrue(son instanceof Parent); Assert.assertFalse(parent instanceof Son); Assert.assertTrue(Son.class.isInstance(son)); Assert.assertFalse(Son.class.isInstance(parent)); Assert.assertTrue(Parent.class.isInstance(son)); Assert.assertTrue(Son.class.isAssignableFrom(Son.class)); Assert.assertFalse(Son.class.isAssignableFrom(Parent.class)); Assert.assertTrue(Parent.class.isAssignableFrom(Son.class)); }
Modifier.isTransient(field.getModifiers())
在使用Java自帶的方式對對象進(jìn)行序列化時,transient成員變量不會被序列化,比如銀行密碼這樣的敏感信息不允許序列化到磁盤或者在網(wǎng)絡(luò)上傳輸。
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class Account implements Serializable{ private static final long serialVersionUID = 2103161633120805900L; private String name; private transient String password; public Account(String n,String p){ this.name=n; this.password=p; } @Override public String toString(){ return "["+this.name+"]\t["+this.password+"]"; } //序列化 public static byte[] serialize(Object object) { ObjectOutputStream oos = null; ByteArrayOutputStream baos = null; try { baos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(baos); oos.writeObject(object); oos.close(); byte[] bytes = baos.toByteArray(); return bytes; } catch (Exception e) { e.printStackTrace(); } return null; } // 反序列化 public static Object deserialize(byte[] bytes) { ByteArrayInputStream bais = null; try { bais = new ByteArrayInputStream(bytes); ObjectInputStream ois = new ObjectInputStream(bais); Object rect=ois.readObject(); ois.close(); return rect; } catch (Exception e) { e.printStackTrace(); } return null; } public static void main(String[] args) throws IOException { Account inst=new Account("orisun","123456"); System.out.println("序列化前"+inst); byte[] datas=serialize(inst); Account inst2=(Account)deserialize(datas); System.out.println("序列化后"+inst2); } }
總結(jié)
以上就是本文關(guān)于Java類和成員上的一些方法實例代碼的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關(guān)文章
Hystrix?Dashboard斷路監(jiān)控儀表盤的實現(xiàn)詳細(xì)介紹
這篇文章主要介紹了Hystrix?Dashboard斷路監(jiān)控儀表盤的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-09-09在MyBatis中使用 # 和 $ 書寫占位符的區(qū)別說明
這篇文章主要介紹了在MyBatis中使用 # 和 $ 書寫占位符的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10Springboot安全框架整合SpringSecurity實現(xiàn)方式
這篇文章主要介紹了Spring全家桶中Springboot安全框架整合SpringSecurity的實現(xiàn)方式,有需要的朋友可以借鑒參考下,希望可以有所幫助2021-09-09JAVA中excel導(dǎo)出一對多合并具體實現(xiàn)
項目中經(jīng)常會使用到導(dǎo)出功能,有導(dǎo)出Word,有導(dǎo)出Excel的,下面這篇文章主要給大家介紹了關(guān)于JAVA中excel導(dǎo)出一對多合并具體實現(xiàn)的相關(guān)資料,需要的朋友可以參考下2023-09-09Java線程池的優(yōu)點及池化技術(shù)的應(yīng)用
這篇文章主要介紹了Java線程池的優(yōu)點及池化技術(shù)的應(yīng)用,Java種提高程序的執(zhí)行效率有兩種實現(xiàn)方法,一個是使用線程、另一個是使用線程池,下文我們就來具體介紹該詳細(xì)內(nèi)容吧,需要的小伙伴可以參考一下2022-05-05