Let's say I have two classes Animal and Food:
public class Animal {
String animalName = "";
Food animalFood;
public Animal(String animalName, Food food) {
this.animalFood = animalFood;
this.animalName = animalName;
}
}
public class Food {
String foodType;
String foodName;
public Food(String foodType, String foodName) {
this.foodName = foodName;
this.foodType = foodType;
}
}
public static void main(String[] args) {
Food dogFood = new Food("meat", "beef");
Animal animal = new Animal("Max", dogFood);
//problem: get animal from dogfood
}
Is there anyway, through Reflection or otherwise to get an instance of a class from its fields (assuming that you have access to the fields)?
Aucun commentaire:
Enregistrer un commentaire