jeudi 22 décembre 2016

Reflection to automatically initiate implementations

I'm trying to use reflection to make it easier for me to add impl classes.

This is how I'm trying to use it:

This is in my main class (Person.java)

public MyHouse myHouse;

Then this is what initiates it:

myHouse = new MyHouse();
myHouse.setPerson(this);

So what I'm trying to do is use reflection and an abstract class to automatically execute the 2 both codes.

The problem is, I can execute: myHouse.setPerson(this);

by using setPerson as an abstract method, but how can I execute myHouse = new MyHouse(); ? Obviously the class I am trying to initiate will be called MyHouse.java, so perhaps something to do with class.getSimpleName() and format it with a lowercase first letter to find the field?

For example, after formatting the class's simple name, it will be equal to "myHouse" which is what the field is called in the Person class, but is still just a String, so how would I do that?

I know the literature is quite confusing any questions please ask below.





Aucun commentaire:

Enregistrer un commentaire