Method visibility between classes in java

In Java (and in general) is there a way to make a class so publicly accessible that its methods, etc. accessible from small classes that don't even instantiate it? Ha, I mean ... If I have a dad class that has a method draw()

and it instantiates a child class called "Hand" and one is called "Deck" and then the deck creates a babier Card class that has method play()

, is there a way for play()

then to call draw()

from dad class?

The idea here is ... dad's class says "Deck! Play (card)!" and then the deck says "Card! play ()!" and then play, turns around and says: "Hey, dad! Draw!"

PS the idea is that ... in CCG, each card has a "play ()" method, which is different, but they are all essentially named the same. The opportunity to play cards comes and you name the game on it. But the card does nothing internal to itself: no, it calls a number of methods from the rules of the game, for which there is visibility. So, for example, a card in MTG that says "draw one card. Deal one damage to target player". (player, 1) and dealDamage (player, 1), which are presumably not in the map itself ... as they affect the variables supposedly created by the players when they started the game and agreed on life outcomes and rules such as what does "draw" mean?

(meta-question: as usual, someone can rename this question to reflect what I'm asking ... being a newbie is so frustrating!)

+1


source to share


5 answers


When the Daddy class runs the Baby classes, it (Daddy) can pass a reference to itself to the Baby constructor, giving Baby access to all of its public methods.



class Daddy {
    public foo(){...}
    public createBaby(){
        Baby baby = new Baby(this);
        // baby now has a reference to Daddy
    }
}


class Baby {
    Daddy daddy;
    public Baby(Daddy daddy){
        this.daddy = daddy;
    }
    ...
    public callDaddy(){
        daddy.foo();
    }
}

      

+4


source


You can:



  • Pass the object reference through the constructor. Or getters and setters. Or directly to the function.
  • Use inheritance.
  • Use static classes.
+1


source


From an OO perspective I would say to use inheritance. One way is to create an abstract class that does not implement methods that will not behave the same subclasses; and implements methods that will behave the same for all subclasses.

0


source


It looks like you are trying to implement double dispatch .

0


source


You can also define inner and outer classes. The inner classes then have access to all the fields of the outer class.

public class OuterClass{
    int x
    private class InnerClass{
        InnerClass(){
            x = 10;
        }
    }
}

      

However, you would probably be better off using either the Singleton pattern, where you have a static reference to one resource (like a deck for most card games, although CCGs have one-on-one players), or else passing to a shared resource in the object's constructor. In the case of MTG or something similar, I would probably give each map a cast () method that takes a Player and a list of targets. The player is a spellcaster, so a spell like Brainstorm can then call draw () on the Player to make them draw cards, while a permanent spell could then assign itself under the control of the Player. The targets would then be for whatever other targets the spell might need, and could potentially be an empty or empty list (whichever is more appropriate).

0


source







All Articles