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

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -