将对象转换为另一个类

时间:2013-12-28 05:01:53

标签: java object

我有三节课。

currentsplit

中将mid.java声明为静态并加入的位置

DecDriver.java

public class DecDriver extends Configured implements Tool {
    public static Split currentsplit=new Split();
    public static void main(String[] args) throws Exception {
    }
}

Split.java

import java.util.ArrayList;
import java.util.List;

public class Split{
    public  List attr_index;
    public  List attr_value;
    double entophy;
    String classLabel;
    Split()
    {
        this.attr_index= new ArrayList<Integer>();
        this.attr_value = new ArrayList<String>();
    }
    Split(List attr_index,List attr_value)
    {
        this.attr_index=attr_index;
        this.attr_value=attr_value;
    }
    void add(Split obj)
    {
        this.add(obj);  
    }
}

Mid.java

DecDriver id = new DecDriver();
for(int count=0;count<size_split;count++)
{
    index=(Integer) id.currentsplit.attr_index.get(count);
    System.out.println("index : "+index);
    attr_value=(String)id.currentsplit.attr_value.get(count);
    System.out.println("attr_value : "+attr_value);     
}

但我有一个场景,我应该将currentsplit对象写入DecDriver的文件并访问Mid.java中的该文件并继续。

我该怎么做? 我所做的是将currentsplit对象转换为string并使用bufferedwriter在file中写入。

DecDriver.java

public class DecDriver extends Configured implements Tool {
    public static Split currentsplit=new Split();
    public static void main(String[] args) throws Exception {
    String objtostring = currentsplit.toString();
    //Buffered writer
    sptbw.write(objtostring);
    .
    .
}

然后我尝试将readMid中的文件casted分割为对象。

Object s = null ;
String cursplitinfo;
//BufferedReader 
while ((cursplitinfo = splitpathbw.readLine()) != null)   {
    s = cursplitinfo;
}

Split currentsplitobj = (Split) s;

DecDriver id = new DecDriver();
for(int count=0;count<size_split;count++)
{
    index=(Integer) currentsplitobj.attr_index.get(count);
    System.out.println("index : "+index);
    attr_value=(String)currentsplitobj.attr_value.get(count);
    System.out.println("attr_value : "+attr_value);
}

但是当我试图运行我的程序时,它显示:

java.lang.Exception: java.lang.ClassCastException: java.lang.String cannot be cast to pck.Split

我做错了吗?

3 个答案:

答案 0 :(得分:1)

为什么不使用JAVA Serialization将对象写入文件 您可以参考如何将对象写入文件Here

示例:
将对象写入文件。

Split currentsplit=new Split();  

FileOutputStream fout=new FileOutputStream("f.txt");  
ObjectOutputStream out=new ObjectOutputStream(fout);  

out.writeObject(currentsplit);  
out.flush();  

从文件中读取对象。

ObjectInputStream in=new ObjectInputStream(new FileInputStream("f.txt"));  
Split  s=(Split)in.readObject();  

答案 1 :(得分:0)

错误很清楚。

您无法将Split对象强制转换为String Object。

当你这样做时

s = cursplitinfo;

仍然持有String类型。

Split currentsplitobj = (Split) s;

String现在如何表现为Split对象?否。

所有人类都是动物,但不是每个动物都是人类。

答案 2 :(得分:0)

Base b = new Derived(); //reference variable of Base class points object of Derived class
Derived d = b; //compile time error, requires casting
Derived d = (Derived) b; // type casting Base to Derived

OR:这是一个简单的例子。

//X is a supper class of Y and Z which are sibblings.
public class RunTimeCastDemo {

    public static void main(String args[]) {
        X x = new X();
        Y y = new Y();
        Z z = new Z();
        X xy = new Y(); // compiles ok (up the hierarchy)
        X xz = new Z(); // compiles ok (up the hierarchy)
        //      Y yz = new Z();   incompatible type (siblings)
        //      Y y1 = new X();   X is not a Y
        //      Z z1 = new X();   X is not a Z
        X x1 = y; // compiles ok (y is subclass of X)
        X x2 = z; // compiles ok (z is subclass of X)
        Y y1 = (Y) x; // compiles ok but produces runtime error
        Z z1 = (Z) x; // compiles ok but produces runtime error
        Y y2 = (Y) x1; // compiles and runs ok (x1 is type Y)
        Z z2 = (Z) x2; // compiles and runs ok (x2 is type Z)
        //      Y y3 = (Y) z;     inconvertible types (siblings)
        //      Z z3 = (Z) y;     inconvertible types (siblings)
        Object o = z;
        Object o1 = (Y) o; // compiles ok but produces runtime error
    }
}

只需访问this link即可看到使用OP进行投射。