Rimilel
Rimilel

Reputation: 133

How to make a set of user-defined functions available to many other classes?

I'd like to start out saying Stack Overflow has been a huge help, just from looking up others' questions, but for this problem I had a hard time finding a solution because it was difficult to word a question.

I'm making a 2D platformer perspective shooting game in Java, and so I have a set of functions that create bullets. Example:

public void CreateShot01(x, y, angle, velocity, bulletID, delay){
/*...will do a bunch of stuff ultimately creating a object in the Projectiles[]    
array, which will be drawn on screen from the Board Object/Class*/
}

What's inside that function isn't the problem. I want to have this and other similiar functions availible to multiple classes, basically for any class that needs shooting. I don't want to copy paste this function into every class that uses it. So I it would help to put all the functions in a separate class BulletHandler that will be will be standard.

But how do I refer to those functions from all the classes that need them. I'm not sure if the thing to use is:

-import? Seems like the most logical solution, but how do you import your own stuff?

-extends? AFAIK, this involves a super class and overwriting functions, not what I need

What is the best way to go about this?

I wasn't sure what to information to supply so just mention it if there's anything that needs to be added and I'll do that right away.

Upvotes: 1

Views: 101

Answers (3)

benqus
benqus

Reputation: 1149

You're looking for abstract classes or interfaces (implements).

The other thing is you might want to do instead of doing "CreateShot01", "CreateShot02", etc. is this:

public void globalShoot (int limit, type? x, type? y, type? angle, type? velocity, type? bulletID, type? delay) {
    for (int i = 0; i < limit; i++) {
        this.wait(delay);
        doShooting(x, y, angle, velocity, bulletID);
    }
}

or something like that. :)

Upvotes: 0

SJuan76
SJuan76

Reputation: 24895

You are confusing terms.

Import is just for making reference to a file in another package, without having to add all of its package hierarchy in the code. Nothing more.

As long as you tell, it is a classic example of subclassing ('extends'). Create a "father" class that implements shoot(); all your objects that shoot extend from it. They just need to call to shoot() to use it, without having to provide a different implementation (unless you want to).

Of course, there may be issues (what happens if there are two methods, and the classes that use any of them do not overlap?), but apart from that you should go with extending a main class.

BTW, add a tag of "homework"


Added to answer to the comments from the poster:

Check abstract classes. They mean that they cannot be instantiated (you can not make a "new" of them) and that they can have some abstract methods of which you know the signature (parameters, return type) but don't provide implementation. You can have other methods fully implemented.

Also, regular "non abstract" classes can be extended and new methods added to them... please go back to the Java basics to tune your concepts (do not worry, all of us go through the pattern of learning-trying-going back to learn now that we know what it means).

Upvotes: 1

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285430

Try to map your objects to physical reality. Everything that shoots should perhaps inherit from an abstract class, say AbstractGun, that should encapsulate the basic idea behind aiming, shooting, loading, etc. Thus this portion is solvable by inheritance. Then if a character needs to shoot something, he will need to obtain a gun-like object. Thus this portion of the problem is solved by composition -- the shooter does not derive from AbstractGun (he does not fulfill the "is-a" relationship) but must have in his possession an object that inherits from AbstractGun and thus fulfills the "has-a" gun relationship.

Upvotes: 4

Related Questions