mercredi 19 mai 2021

Class adapter pattern in Java

interface movement{
    void jump();    //Jump is 1 Feet high
}

class mario(){
    void specificJump(int feet);   //Use variable feet to jump higher than 1 Feet
}

public class myAdapter extends newMovement implements movement{
    int feet;
    public void jump(){
        specificJump(feet); 
    }
}

movement is an interface with a single method that lets you jump 1 feet high.
mario is a class with a method specificJump(int feet) that lets you jump higher than 1 feet.
The implementation of myAdapter should work like this:

  1. declare variable int feet
  2. implement interface movement by declare and initialize public void jump()
  3. use the function specificJump(feet) to jump to the height of int feet

So when trying to invoke jump() you actually will execute specificJump(feet).

Question: In order to execute specificJump(int feet) we first need to invoke jump().
However specificJump(int feet) needs a parameter, is it possible to just declare int feet, in myAdapter, like I did?
My issue is, how does someone get the int feet value in, when you have to call jump() with no parameter? (Using Class adapter pattern)

Aucun commentaire:

Enregistrer un commentaire