在java中强制转换接口

时间:2010-04-12 02:25:22

标签: java interface casting

我有两个独立的界面,一个'MultiLingual'用于选择要返回的文本语言,第二个'Justification'用于证明返回的文本。现在我需要加入它们,但我遇到了'java.lang.ClassCastException'错误。课堂书在这里并不重要。 Data.length和FormattedInt.width对应于行的宽度。代码:

public interface MultiLingual {
    static int ENG = 0;
    static int PL = 1;
    String get(int lang);
    int setLength(int length);
}

class Book implements MultiLingual {

    private String title;
    private String publisher;
    private String author;
    private int jezyk;

    public Book(String t, String a, String p, int y){

        this(t, a, p, y, 1);
    }

    public Book(String t, String a, String p, int y, int lang){
        title = t;
        author = a;
        publisher = p;
        jezyk = lang;
    }

    public String get(int lang){

        jezyk = lang;
        return this.toString();
    }

    public int setLength(int i){     
        return 0;
    }

    @Override
    public String toString(){

        String dane;
        if (jezyk == ENG){
            dane = "Author: "+this.author+"\n"+
                    "Title: "+this.title+"\n"+
                    "Publisher: "+this.publisher+"\n";
        }
        else {
            dane = "Autor: "+this.author+"\n"+
                    "Tytul: "+this.title+"\n"+
                    "Wydawca: "+this.publisher+"\n";
        }
        return dane;
    }
}


class Data implements MultiLingual {
    private int day;
    private int month;
    private int year;
    private int jezyk;
    private int length;

    public Data(int d, int m, int y){
        this(d, m, y, 1);
    }

    public Data(int d, int m, int y, int lang){
        day = d;
        month = m;
        year = y;
        jezyk = lang;
    }

    public String get(int lang){
        jezyk = lang;
        return this.toString();
    }

    public int setLength(int i){
        length = i;
        return length;
    }

    @Override
    public String toString(){
        String dane="";
        String miesiac="";
        String dzien="";

        switch(month){
            case 1: miesiac="January";
            case 2: miesiac="February";
            case 3: miesiac="March";
            case 4: miesiac="April";
            case 5: miesiac="May";
            case 6: miesiac="June";
            case 7: miesiac="July";
            case 8: miesiac="August";
            case 9: miesiac="September";
            case 10: miesiac="October";
            case 11: miesiac="November";
            case 12: miesiac="December";
        }
        if(day==1){
            dzien="st";
        }
        if(day==2 || day==22){
            dzien="nd";
        }
        if(day==3 || day==23){
            dzien="rd";
        }
        else{
            dzien="th";
        }

        if(jezyk==ENG){
            dane =this.day+dzien+" of "+miesiac+" "+this.year;
        }
        else{
            dane = this.day+"."+this.month+"."+this.year;
        }
        return dane;
    }

}

interface Justification {
    static int RIGHT=1;
    static int LEFT=2;
    static int CENTER=3;

    String justify(int just);

}

class FormattedInt implements Justification {
    private int liczba;
    private int width;
    private int wyrownanie;

    public FormattedInt(int s, int i){
        liczba = s;
        width = i;
        wyrownanie = 2;
    }

    public String justify(int just){

        wyrownanie = just;
        String wynik="";
        String tekst = Integer.toString(liczba);
        int len = tekst.length();
        int space_left=width - len;
        int space_right = space_left;
        int space_center_left = (width - len)/2;
        int space_center_right = width - len - space_center_left -1;
        String puste="";

        if(wyrownanie == LEFT){

            for(int i=0; i<space_right; i++){
                puste = puste + " ";
            }
            wynik = tekst+puste;
        }
        else if(wyrownanie == RIGHT){
            for(int i=0; i<space_left; i++){
                puste = puste + " ";
            }
            wynik = puste+tekst;
        }
        else if(wyrownanie == CENTER){
            for(int i=0; i<space_center_left; i++){
                puste = puste + " ";
            }
            wynik = puste + tekst;
            puste = " ";
            for(int i=0; i< space_center_right; i++){
                puste = puste + " ";
            }
            wynik = wynik + puste;

        }
        return wynik;
    }
}

显示此投射的测试代码“(Justification)gatecrasher [1]”给出了错误:

  MultiLingual gatecrasher[]={ new Data(3,12,1998),
                               new Data(10,6,1924,MultiLingual.ENG),
                               new Book("Sekret","Rhonda Byrne", "Nowa proza",2007),
                               new Book("Tuesdays with Morrie",
                                        "Mitch Albom", "Time Warner Books",2003,
                                        MultiLingual.ENG),
                             };

  gatecrasher[0].setLength(25);
  gatecrasher[1].setLength(25);

  Justification[] t={ new FormattedInt(345,25),
                      (Justification)gatecrasher[1],
                      (Justification)gatecrasher[0],
                      new FormattedInt(-7,25)
                    };

  System.out.println("         10        20        30");
  System.out.println("123456789 123456789 123456789");
  for(int i=0;i < t.length;i++)
    System.out.println(t[i].justify(Justification.RIGHT)+"<----\n");

3 个答案:

答案 0 :(得分:3)

数据或书籍工具Justification and Multilingual都没有继承Justiifcation,因此预期像(Justification)gatecrasher [1]这样的强制转换会失败。

答案 1 :(得分:3)

您应该通过添加新方法

来更改您的Justification类
String justify(Multilingual m);

然后,您可以通过调用带有书籍或数据实例的justify方法来证明数据或书籍。

答案 2 :(得分:0)

如果您无法更改自己所写的内容,那么您可能需要考虑

  1. 使用DynamicProxy API。它使用反射,因此您可能需要编写更多代码。

  2. 如果您知道如何将justify()映射到某些算法,请使用多语言执行对齐,然后再考虑使用Facade模式。它与DynamicProxy类似,但更易于使用。

相关问题