Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中的转换接口_Java_Interface_Casting - Fatal编程技术网

java中的转换接口

java中的转换接口,java,interface,casting,Java,Interface,Casting,我有两个独立的接口,一个是“多语言”接口,用于选择要返回的文本语言,另一个是“对齐”接口,用于对齐返回的文本。现在我需要加入他们,但我遇到了“java.lang.ClassCastException”错误。课本在这里不重要。Data.length和FormattedInt.width对应于行的宽度。守则: public interface MultiLingual { static int ENG = 0; static int PL = 1; String get(in

我有两个独立的接口,一个是“多语言”接口,用于选择要返回的文本语言,另一个是“对齐”接口,用于对齐返回的文本。现在我需要加入他们,但我遇到了“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;
    }
}
显示此casting Justificationgatecrasher[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");

Data或book implement Justification和Multilanguage都不会继承Justification,因此类似Justificationgatecrasher[1]的强制转换将失败。

Data或book implement Justification和Multilanguage都不会继承Justification,因此类似Justificationgatecrasher[1]的强制转换将失败将失败。

您应该通过添加新方法更改对正类

String justify(Multilingual m);

然后,您可以通过调用带有book或data实例的justify方法来对数据或book进行对正。

您应该通过添加新方法来更改对正类

String justify(Multilingual m);

你可以用书本或数据的实例来调用证明方法来证明数据或书的正确性。

< P>如果你不能改变你所写的东西,那么你可能想考虑< /P> 使用API。它使用反射,因此您可能需要编写更多代码

如果你知道如何映射到某个算法的正确性,那么用多语言进行证明,然后考虑使用模式。它与DynamicProxy类似,但更易于使用


如果你不能改变你所写的东西,那么你可能要考虑

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

如果你知道如何映射到某个算法的正确性,那么用多语言进行证明,然后考虑使用模式。它与DynamicProxy类似,但更易于使用