java 中的序列化概念,卡在了概念中
Serialization concept in java, got stuck in concept
我不明白序列化的确切概念,请帮助我,也请帮助我理解 d.getCollar().getCollarSize()
的这个概念,这是如何工作的?我们可以直接调用 getColarSize();直接创建那个 class 的对象后的方法?
喜欢,
Collar ab=new Collar();
ab.getCollarSize();
代码如下:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class SerializeDog {
public static void main(String args[])
{
Collar c=new Collar(2);
Dog d=new Dog(c, 5);
System.out.println("before collar size is :"+d.getCollar().getCollarSize());
try{
FileOutputStream fs=new FileOutputStream("serializeDog.ser");
ObjectOutputStream os=new ObjectOutputStream(fs);
os.writeObject(d);
os.close();
}catch(Exception e){e.printStackTrace();}
try{
FileInputStream fis=new FileInputStream("serializeDog.ser");
ObjectInputStream ois=new ObjectInputStream(fis);
d=(Dog)ois.readObject();
ois.close();
}catch(Exception e){e.printStackTrace();}
System.out.println("after collar size is: "+d.getCollar().getCollarSize());
}
}
class Dog implements Serializable{
private Collar theCollar;
private int dogSize;
public Dog(Collar collar,int size)
{
theCollar=collar;
dogSize=size;
}
public Collar getCollar()
{
return theCollar;
}
}
class Collar implements Serializable
{
private int collarSize;
public Collar(int size)
{
collarSize=size;
}
public int getCollarSize()
{
return collarSize;
}
}
看看下面的代码
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class SerializeDog {
public static void main(String args[])
{
Collar c=new Collar(2);
Dog d=new Dog(c, 5);
System.out.println("before collar size is :"+d.getCollar().getCollarSize());
try{
FileOutputStream fs=new FileOutputStream("serializeDog.ser");
ObjectOutputStream os=new ObjectOutputStream(fs);
os.writeObject(d);
os.close();
}catch(Exception e){e.printStackTrace();}
try{
FileInputStream fis=new FileInputStream("serializeDog.ser");
ObjectInputStream ois=new ObjectInputStream(fis);
d=(Dog)ois.readObject();
ois.close();
}catch(Exception e){e.printStackTrace();}
System.out.println("after collar size is: "+d.getCollar().getCollarSize());
}
}
class Dog implements Serializable{
private Collar theCollar;
private int dogSize;
public Dog(Collar collar,int size)
{
theCollar=collar;
dogSize=size;
}
public Collar getCollar()
{
return theCollar;
}
}
我不明白序列化的确切概念,请帮助我,也请帮助我理解 d.getCollar().getCollarSize()
的这个概念,这是如何工作的?我们可以直接调用 getColarSize();直接创建那个 class 的对象后的方法?
喜欢,
Collar ab=new Collar();
ab.getCollarSize();
代码如下:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class SerializeDog {
public static void main(String args[])
{
Collar c=new Collar(2);
Dog d=new Dog(c, 5);
System.out.println("before collar size is :"+d.getCollar().getCollarSize());
try{
FileOutputStream fs=new FileOutputStream("serializeDog.ser");
ObjectOutputStream os=new ObjectOutputStream(fs);
os.writeObject(d);
os.close();
}catch(Exception e){e.printStackTrace();}
try{
FileInputStream fis=new FileInputStream("serializeDog.ser");
ObjectInputStream ois=new ObjectInputStream(fis);
d=(Dog)ois.readObject();
ois.close();
}catch(Exception e){e.printStackTrace();}
System.out.println("after collar size is: "+d.getCollar().getCollarSize());
}
}
class Dog implements Serializable{
private Collar theCollar;
private int dogSize;
public Dog(Collar collar,int size)
{
theCollar=collar;
dogSize=size;
}
public Collar getCollar()
{
return theCollar;
}
}
class Collar implements Serializable
{
private int collarSize;
public Collar(int size)
{
collarSize=size;
}
public int getCollarSize()
{
return collarSize;
}
}
看看下面的代码
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class SerializeDog {
public static void main(String args[])
{
Collar c=new Collar(2);
Dog d=new Dog(c, 5);
System.out.println("before collar size is :"+d.getCollar().getCollarSize());
try{
FileOutputStream fs=new FileOutputStream("serializeDog.ser");
ObjectOutputStream os=new ObjectOutputStream(fs);
os.writeObject(d);
os.close();
}catch(Exception e){e.printStackTrace();}
try{
FileInputStream fis=new FileInputStream("serializeDog.ser");
ObjectInputStream ois=new ObjectInputStream(fis);
d=(Dog)ois.readObject();
ois.close();
}catch(Exception e){e.printStackTrace();}
System.out.println("after collar size is: "+d.getCollar().getCollarSize());
}
}
class Dog implements Serializable{
private Collar theCollar;
private int dogSize;
public Dog(Collar collar,int size)
{
theCollar=collar;
dogSize=size;
}
public Collar getCollar()
{
return theCollar;
}
}