How can I declare a function like damagePlayer
below in Java?
player.getPlayer1().damagePlayer(50);
I already know that we have to declare something like this to get it:
ServerPlayer player = new ServerPlayer();
But, how do we put the second function (GetPlayer()
) in the first place? I'm learning Java, and this would be an awesome way to save code.
player.getPlayer1().damagePlayer(50);
damagePlayer(50) would be written as a method of the type that getPlayer1() returns. So implement your damagePlayer(int x) class in the class whose instance player.getPlayer1() returns.
so if getPlayer1() returns an object of the type (or class ) Foo , you would write the damagePlayer method in the Foo class.
eg.
Bar bar = new Bar();
bar.doThis().doThat(25) ;
class Bar{
public Foo doThis() {
Foo foo=new Foo();
//do some thing using foo
return foo;
}
}
class Foo() {
void doThat(int number){
// can do anything using parameter number and return anything
}
}