java - How to call an object within an object -
i have class called man, punch , test
the punch object field within man class , works fine.
but in test class, want access method of punch class. how go in doing this? thanks
if have 3 classes , want access nested methods, have public or public static
if have getpunch() in man class, can access man object , let him fire punch.
your man class has have field punch , have have way create punch object.
public class man { punch punch; public man(){ punch = new punch(); } public punch getpunch(){ return punch(); } }
then can access punch object via getpunch
man man = new man(); man.getpunch().somepunchmethod();
or create reference punch field, , use access punch methods
man man = new man(); punch punch = man.getpunch(); punch.somepunchmethod();
Comments
Post a Comment