Some method instance code on the Java class and members

  • 2020-12-16 05:56:58
  • OfStack

isInstance and isAssignableFrom

obj instanceof Class

Determines whether obj is an instance of Class or a subclass of Class

clazz.isInstance(obj)

Determines whether obj casts to clazz, i.e. whether obj is an instance of a subclass of clazz or clazz

clazz1.isAssignableFrom(clazz2)

Returns True if clazz2 is the same as clazz1, or clazz1 is the parent of clazz2, otherwise returns 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())

transient member variables are not serialized when serializing objects using Java's built-in method, and sensitive information such as bank passwords is not allowed to be serialized to disk or transferred over the network.


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+"]";
	}
	// serialization 
	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;
	}
	//  deserialization 
	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(" Before serialization "+inst);
		byte[] datas=serialize(inst);
		Account inst2=(Account)deserialize(datas);
		System.out.println(" serialized "+inst2);
	}
}

conclusion

That's the end of this article on the Java class and members of 1 method instance code, I hope to help you. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: