java - how to flatten an inherited class? -
is there way flatten inherited classes discover functions belong class?
if have class like:
public class person { public string name; public void setname(string name){ this.name = name; } public string getname(){ return this.name; } } and inherited class like
public class worker extends person { public string job; public void setjob(string job){ this.job = job; } public string getjob(){ return this.job; } } when instantiate worker, there way find out set/getname belongs person class?
i've seen suggestions of using interface find information out i'm not sure how interface best used show kind of function relationships here. there cheats being able reference particular classes within inherited class?
edit:
to answer why wish this: want able loop on methods in object , ignore ones come class, in instance want ignore person class.
my real world example isn't quite simple this, couldn't think of better way of working out methods belong class without "flattening" them.
the isbridge method in method class can used check whether method declared in person or worker class.
public static void main(string[] args) { worker worker = new worker(); class clazz = worker.getclass(); method[] methods = clazz.getdeclaredmethods(); for(method method :methods) { system.out.println(method.getname()+ " in worker: "+ !method.isbridge()); } } the above code produce output as:
setname in worker: false getname in worker: false setjob in worker: true getjob in worker: true
Comments
Post a Comment