java - How can i use methods of a class to a new class? -
well have menu method in class shows menu in emulator when clicked.
how can use method new class b
i want methods use class b :
public boolean oncreateoptionsmenu(menu menu) { super.oncreateoptionsmenu(menu); //inflates menu or show activity menuinflater awesome = getmenuinflater(); awesome.inflate(r.menu.main, menu); //main xml main return true; } //this manipulate menu public boolean onoptionsitemselected(menuitem item){ switch (item.getitemid()) { case r.id.menusweet: startactivity(new intent("sweet")); return true; case r.id.menutoast: toast andeggs = toast.maketext(mainactivity.this, "this toast", toast.length_long); andeggs.show(); return true; default: return super.onoptionsitemselected(item); } }
depending on want do, have 2 big options (among many): subclassing , composition:
subclassing
so if class b specialization of class a's behavior (b is a special case of a), extend class a:
class { public boolean onoptionsitemselected(menuitem item); } class b extends { // methods b has. }
thus may call
b b = new b(); b.onoptionsitemselected(someitem);
composition
the second option wrapping method call it's own method of same name (so b has an object , uses it):
class b { private a = new a(); public boolean onoptionsitemselected(item someitem) { a.onoptionsitemselected(someitem); } }
Comments
Post a Comment