В JavaScript интерфейсы могут иметь атрибуты, а интерфейс может расширять несколько интерфейсов:
Код: Выделить всё
interface Interface1 {
name?: string
age?: number
}
interface Interface2 {
id?: string
}
interface InterfaceCombination extends Interface1, Interface2 {
position?: string
}
Первая идея: классы и делегирование
Поскольку интерфейсы Java имеют только окончательные статические атрибуты, я выбираю классы для поддержки этих атрибутов:Код: Выделить всё
public class Class1 {
private String name;
private Number age;
public String getName();
public void setName(String name);
... // getter & setter for age
}
public class Class2 {
private String id;
... // getter & setter for id
}
Код: Выделить всё
public class ClassCombination {
private Class1 class1;
private Class2 class2;
private String position;
public String getName() {
return class1.getName();
}
... // getters & setters
}
Вторая идея: интерфейс, геттеры и сеттеры
Хотя класс Java может расширять только один класс, интерфейс Java может расширять несколько интерфейсов.Код: Выделить всё
public interface Interface1 {
public String getName();
public void setName();
... // getter & setter for age
}
public interface Interface2 {
... // getter & setter for id
}
public interface InterfaceCombination extends Interface1, Interface2 {
... // getter & setter for position
}
Есть ли более элегантное и простое в реализации решение?
Подробнее здесь: https://stackoverflow.com/questions/701 ... s-into-one
Мобильная версия