我想让开发人员必须定义要在视图中显示的“模型”类的属性。
我曾想过使用枚举作为解决方案,但我认为不可能在超类(接口)中定义枚举。
如果我的描述不清楚,我很抱歉。有点难以解释。我会尝试用一个具体的案例来解释。
动物.java
public interface Animal {
public void eat();
}
鸟.java
public class Bird implements Animal
{
private String name;
private int age;
private Beak beak;
private Wings wings;
public Bird(String name, int age, Beak beak, Wings wings)
{
this.name = name;
this.age = age;
this.beak = beak;
this.wings = organ;
}
//getter setter..
@Override
public void eat() {
//eating
}
}
Snake.java
public class Snake implements Animal
{
private String name;
private int age;
private Fang fangs;
public Snake(String name, int age, Fang fangs)
{
this.name = name;
this.age = age;
this.fangs = fangs;
}
//getter setter..
@Override
public void eat() {
//eating
}
}
动物园.java
public class Zoo
{
private ArrayList<Bird> birds = new ArrayList<Bird>();
private ArrayList<Snake> snakes = new ArrayList<Snake>();
private ArrayList<Object?> birdsShownFeatures = new ArrayList<Object?>();
private ArrayList<Object?> snakesShownFeatures = new ArrayList<Object?>();
public Zoo()
{
birds.add(new Bird("Vulture", 2, new CrookedBeak(), new WideWing()));
birds.add(new Bird("Whatever", 3, new WhateverBeak(), new WhateverWing()));
birds.add(new Bird("Wut", 4, new WutBeak(), new WutWing()));
snakes.add(new Snake("Cobra", 5, new TwinFang()));
snakes.add(new Snake("Whatever", 5, new WhateverFang()));
snakes.add(new Snake("Wut", 5, new WutFang()));
birdsShownFeatures.add(new Object?("name"));
birdsShownFeatures.add(new Object?("beak"));
birdsShownFeatures.add(new Object?("wings"));
snakesShownFeatures.add(new Object?("name"));
snakesShownFeatures.add(new Object?("fangs"));
}
public void showOff()
{
for(Bird bird:birds)
{
for(Object? object:birdsShownFeatures)
{
System.out.println("Bird: "+bird.unknownFunction(object));
}
}
for(Snake snake:snakes)
{
for(Object? object:snakesShownFeatures)
{
System.out.println("Snake: "+snake.unknownFunction(object));
}
}
}
}
我必须概括Animal(Object?)的子类的属性。而且我必须能够定义一个函数来检索该属性(unknownFunction)。
换句话说,我希望能够轻松定义动物子类的某些属性,并能够进行相应的处理。
完美(不真实?)示例:
public class Zoo {
private ArrayList<Bird> birds = new ArrayList<Bird>();
private ArrayList<Snake> snakes = new ArrayList<Snake>();
private ArrayList<Object> birdsShownFeatures = new ArrayList<Object>();
private ArrayList<Object> snakesShownFeatures = new ArrayList<Object>();
public Zoo()
{
birds.add(new Bird("Vulture", 2, new CrookedBeak(), new WideWing()));
birds.add(new Bird("Whatever", 3, new WhateverBeak(), new WhateverWing()));
birds.add(new Bird("Wut", 4, new WutBeak(), new WutWing()));
snakes.add(new Snake("Cobra", 5, new TwinFang()));
snakes.add(new Snake("Whatever", 5, new WhateverFang()));
snakes.add(new Snake("Wut", 5, new WutFang()));
birdsShownFeatures.add(Bird.NAME);
birdsShownFeatures.add(Bird.BEAK);
birdsShownFeatures.add(Bird.WINGS);
snakesShownFeatures.add(Snake.NAME);
snakesShownFeatures.add(Snake.FANGS);
}
public void showOff()
{
for(Bird bird:birds)
{
for(Object object:birdsShownFeatures)
{
System.out.println("Bird: "+bird.get(object));
}
}
for(Snake snake:snakes)
{
for(Object object:snakesShownFeatures)
{
System.out.println("Snake: "+snake.get(object));
}
}
}
}
枚举无法工作,因为我无法强制开发人员在每次创建实现 Animal 的类时进行实现接口的特定枚举。
蛇和鸟的属性都必须能够泛化。但还是定义了。让每个属性都实现一个特定的接口不是一种选择,因为让所有需要的类(包括本机)实现一个接口将是一个痛苦。
对不起,如果它太混乱了。我不知道如何正确解释这个案例。