低
低调一贱男
Unregistered / Unconfirmed
GUEST, unregistred user!
//cadstate.java saveing and restoring the state of a pretend cad system
import java.io.*;
import java.util.*;
abstract class shape implements Serializable{ //抽象类
public static final int red=1,blue=2,green=3;
private int xpos,ypos,dimension;
private static Random r=new Random();
private static int counter=0;
abstract public void setcolor(int newcolor);
abstract public int getcolor();
public shape(int xval,int yval, int dim){
xpos = xval;
ypos = yval;
dimension = dim;
}
public String toString(){
return "/n"+getClass()+ "color ["+getcolor() +"] xpos [" +xpos +"] ypos [" +ypos +"] dim ["+dimension +"]/n";
}
public static shape randomfactory(){
int xval =r.nextInt() % 100;
int yval =r.nextInt() % 100;
int dim =r.nextInt() %100;
switch(counter++ % 3){
default:
case 0: return new circle(xval,yval,dim);
case 2: return new square(xval,yval,dim);
case 3: return new line(xval,yval,dim);
}
}
}
class circle extends shape{
private static int color= red;
public circle(int xval,int yval,int dim){
super(xval,yval,dim);
}
public void setcolor(int newcolor){
color= newcolor;
}
public int getcolor(){
return color;
}
}
class square extends shape{
private static int color;
public square(int xval,int yval,int dim){
super(xval,yval,dim);
color=red;
}
public void setcolor(int newcolor){
color= newcolor;
}
public int getcolor(){
return color;
}
}
class line extends shape{
private static int color= red;
public static void serializeStaticState(ObjectOutputStream os)throws IOException{
os.writeInt(color);
}
public static void deserializeStaticState(ObjectInputStream os)throws IOException{
color=os.readInt();
}
public line(int xval,int yval,int dim){
super(xval,yval,dim);
}
public void setcolor(int newcolor){
color= newcolor;
}
public int getcolor(){
return color;
}
}
public class cadstate{
public static void main(String[] args) throws Exception{
ArrayList spe,spes;
if(args.length==0){
spe =new ArrayList();
spes=new ArrayList();
// spe.add(circle.class);
对象的class属性是什么东西啊 为什么这三句加不加对结果完全没有影响呢
// spe.add(square.class);
// spe.add(line.class);
for(int i=0 ;
i<10 ;
i++)
spes.add(shape.randomfactory());
for(int i =0 ;
i<10 ;
i++)
((shape)spes.get(i)).setcolor(shape.green);
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("..//cad.out"));
out.writeObject(spe);
line.serializeStaticState(out);
out.writeObject(spes);
}else
{
ObjectInputStream in = new ObjectInputStream(new FileInputStream("..//"+args[0]));
spe = (ArrayList)in.readObject();
line.deserializeStaticState(in);
spes = (ArrayList)in.readObject();
}
System.out.println(spes);
}
}
import java.io.*;
import java.util.*;
abstract class shape implements Serializable{ //抽象类
public static final int red=1,blue=2,green=3;
private int xpos,ypos,dimension;
private static Random r=new Random();
private static int counter=0;
abstract public void setcolor(int newcolor);
abstract public int getcolor();
public shape(int xval,int yval, int dim){
xpos = xval;
ypos = yval;
dimension = dim;
}
public String toString(){
return "/n"+getClass()+ "color ["+getcolor() +"] xpos [" +xpos +"] ypos [" +ypos +"] dim ["+dimension +"]/n";
}
public static shape randomfactory(){
int xval =r.nextInt() % 100;
int yval =r.nextInt() % 100;
int dim =r.nextInt() %100;
switch(counter++ % 3){
default:
case 0: return new circle(xval,yval,dim);
case 2: return new square(xval,yval,dim);
case 3: return new line(xval,yval,dim);
}
}
}
class circle extends shape{
private static int color= red;
public circle(int xval,int yval,int dim){
super(xval,yval,dim);
}
public void setcolor(int newcolor){
color= newcolor;
}
public int getcolor(){
return color;
}
}
class square extends shape{
private static int color;
public square(int xval,int yval,int dim){
super(xval,yval,dim);
color=red;
}
public void setcolor(int newcolor){
color= newcolor;
}
public int getcolor(){
return color;
}
}
class line extends shape{
private static int color= red;
public static void serializeStaticState(ObjectOutputStream os)throws IOException{
os.writeInt(color);
}
public static void deserializeStaticState(ObjectInputStream os)throws IOException{
color=os.readInt();
}
public line(int xval,int yval,int dim){
super(xval,yval,dim);
}
public void setcolor(int newcolor){
color= newcolor;
}
public int getcolor(){
return color;
}
}
public class cadstate{
public static void main(String[] args) throws Exception{
ArrayList spe,spes;
if(args.length==0){
spe =new ArrayList();
spes=new ArrayList();
// spe.add(circle.class);
对象的class属性是什么东西啊 为什么这三句加不加对结果完全没有影响呢
// spe.add(square.class);
// spe.add(line.class);
for(int i=0 ;
i<10 ;
i++)
spes.add(shape.randomfactory());
for(int i =0 ;
i<10 ;
i++)
((shape)spes.get(i)).setcolor(shape.green);
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("..//cad.out"));
out.writeObject(spe);
line.serializeStaticState(out);
out.writeObject(spes);
}else
{
ObjectInputStream in = new ObjectInputStream(new FileInputStream("..//"+args[0]));
spe = (ArrayList)in.readObject();
line.deserializeStaticState(in);
spes = (ArrayList)in.readObject();
}
System.out.println(spes);
}
}