class Lithuania {}
class Latvia {}
class Estonia {}
class Europe {}
class Country {}
class Baltic {}
Если с оглядкой на ксоникс, то у меня получается нечто такое
class Countries {
List <Europe> countriesE;
List <Baltic> countriesB;
String s = "";
public Countries(){
countriesB = new ArrayList<Baltic>();
countriesE = new ArrayList<Europe>();
countriesB.add(new Latvia());
countriesB.add(new Estonia());
countriesB.add(new Lithuania());
countriesE.add(new Ukraine());
}
public String toString() {
for(Baltic country: countriesB){
s += country + "\n";
}
for(Europe country: countriesE){
s += country + "\n";
}
return s;
}
}
Не понравилось, переделала.
interface IEurope {
String place = " in Europe";
}
interface IBaltic {
String place = " of the Baltic States";
}
abstract class Country implements IEurope, IBaltic {
String s;
public Country(){
s = "It's a country";
}
public String toString() {
return s;
}
}
class Countries {
List <Country> countries;
String s = "";
public Countries(){
countries = new ArrayList<Country>();
countries.add(new Latvia());
countries.add(new Lithuania());
countries.add(new Estonia());
countries.add(new Ukraine());
}
public String toString() {
for(Country country: countries){
s += country + "\n";
}
return s;
}
}
class Lithuania extends Country{
public Lithuania() {
s += IBaltic.place + " called Lithuania.";
}
}
Возможен и такой вариант.
interface IEurope {
String place = " in Europe";
String detail = ". Not baltic country, called";
}
interface IBaltic extends IEurope{
String detail = " of the Baltic States, called";
}
class Estonia extends Country
{
public Estonia() {
s += place + IBaltic.detail + " Estonia.";
}
}
class Ukraine extends Country {
public Ukraine() {
s += place + IEurope.detail + " Ukraine.";
}
}
Интересно сработает ли такая магия в релизе?
interface ICountry{
String c = "It's a country";
}
interface IEurope extends ICountry{
String place = " in Europe";
String detail = ". Not baltic country, called";
default String ss(String s){
return c + place + detail + s;
};
}
interface IBaltic extends IEurope{
String detail = " of the Baltic States, called";
default String ss(String s){
return c + place + detail + s;
};
}
abstract class Country {
String s;
public Country() {
s = " " + getClass().getName() + ".";
}
public String toString() {
return s;
}
}
class Lithuania extends Country implements IBaltic {
public Lithuania() {
s = ss(s);
}
}
Украина це Европа!
интерфейсы тут не при чём.
нужно было три поля - три страны
Да, Европа, но не Прибалтика.