我有兩個單獨的接口,一個用於選擇要返回的文本語言的MultiLingual接口,另一個接口用於證明返回的文本。現在我需要加入他們,但我被卡在'java.lang.ClassCastException'錯誤。課本在這裏並不重要。 Data.length和FormattedInt.width對應於行的寬度。代碼:java中的接口接口
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;
}
}
並測試代碼,顯示該鑄造「(理由)不速之客[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");