开发者

using Java interfaces

开发者 https://www.devze.com 2022-12-25 02:26 出处:网络
I need to create interface MultiLingual, that allows to display object\'s data in 开发者_如何学JAVAdifferent languages (not data itself, but introduction like \"Author\", \"Title\" etc.).

I need to create interface MultiLingual, that allows to display object's data in 开发者_如何学JAVAdifferent languages (not data itself, but introduction like "Author", "Title" etc.).

Printed data looks like this :

3 grudnia 1998

10th of June 1924

Autor: Tolkien
Tytul: LoTR
Wydawnictwo: Amazon 2010

Author: Mitch Albom
Title: Tuesdays with Morrie
Publishing House: Time Warner Books 2003   

37 360,45 PLN

5,850.70 GBP

3rd of December 1998

10th of June 1924

Author: Tolkien
Title: LoTR
Publishing House: Amazon 2010

Author: Mitch Albom
Title: Tuesdays with Morrie
Publishing House: Time Warner Books 2003

37,360.45 GBP

5,850.70 GBP

Test code looks like this :

public class Main {

public static void main(String[] args){

  MultiLingual gatecrasher[]={ new Data(3,12,1998),
                               new Data(10,6,1924,MultiLingual.ENG),
                               new Book("LoTR", "Tolkien", "Amazon", 2010),
                               new Book("Tuesdays with Morrie",
                                        "Mitch Albom", "Time Warner Books",2003,
                                        MultiLingual.ENG),
                               new Money(1232895/33.0,MultiLingual.PL),
                               new Money(134566/23.0,MultiLingual.ENG),
                             };

  for(int i=0;i < gatecrasher.length;i++)
    System.out.println(gatecrasher[i]+"\n");

  for(int i=0;i < gatecrasher.length;i++)
    System.out.println(gatecrasher[i].get(MultiLingual.ENG)+"\n");

}
}

So i need to introduce constants ENG, PL in MultiLingual interface, as well as method get(int language) :

public interface MultiLingual {

int ENG = 0;
int PL= 1;

String get(int lang);

}

And then I have class Book. Problem starts with the constructors. One of them needs to take MultiLingual.ENG as argument, but how to achieve that ? Is this the proper way? :

class Book implements MultiLingual {

private String title;
private String publisher;
private String author;

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

Or should I treat this MultiLingual.ENG as int variable , that will just change automatically constants in interface?

Second constructor for book doesn't take MultLingual as argument, but following implementation is somehow wrong :

public Book(String t, String a, String p, int y){
    Book someBook = new Book(t, a, p, y, MultiLingual m);
}

I could just send int m in place of MultiLingual m but then I will have no control if language is set to PL or ENG.

And finally get() method for Boook but I think at least this should be working fine:

public String get(int lang){

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

    return data;
}

@Override
public String toString(){
    return "";
}

}


You'd probably be better using an enum to represent the different languages

public interface MultiLingual {

    enum Language { ENG, PL }

    String get(Language lang);

}

The constructor of your Book class can then take an instance of MultiLingual.Language

public Book(String t, String a, String p, int y, MultiLingual.Language lang) {      
}


Inside your interface, the ints representing the languages should be statics, you don't want to accidentally start reassinging them while your code is running. Then have your book class hold an int for which language it is.

Book someBook = new Book(t, a, p, y, MultiLingual m); won't work as MultiLingual is an interface, it isn't anything there can be an instance of to pass in.

It needs to be an int. Which will be something like MultiLingual.ENG or .PL


Did you consider using ResourceBundle? That means, you could use property files for defining locale specific Data, making it very easy to add more locales at a later point. Your data object would return some key for the labels to translate, depending on your application, the guy could then resolve the right labels via the ResourceBundle.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号