繁体   English   中英

将对象强制转换为另一个类

[英]Cast an object to another class

我有三节课。

mid.java currentsplit声明为static并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);
    .
    .
}

然后,我尝试在Mid read文件并将其casted为Split对象。

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

我做错什么了吗?

为什么不使用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();  

错误非常明显。

您不能将Split对象转换为String Object。

当你做

s = cursplitinfo;

仍持有String类型。

Split currentsplitobj = (Split) s;

字符串现在如何表现为拆分对象? 没有。

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

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
    }
}

只需访问此链接即可查看使用OP进行投射。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM