简体   繁体   中英

Java : using objects created in a different class file

I'm trying to recreate some board game on Android and I'm stuck with the use of objects.

I basically have one main activity class, which does all the UI changes and calculations, but I have to create the cards somewhere. I can do it with no problem in the main activity after creating a WonderCard class somewhere else.

Here's WonderCard.java :

package com.example.phil.test3;

public class WonderCard {

    int id;
    String name_full;
    String name_short;
    int view_path;

public WonderCard(int id, String name_short, String name_full, int view_path) {
    this.id=id;
    this.name_full=name_full;
    this.name_short=name_short;
    this.view_path=view_path;
}

}

And here's the object construction in the main activity :

card = new WonderCard[] {
                new WonderCard(0,"Olympia","La Statue de zeus à Olympie",R.mipmap.wondercard_0),
                new WonderCard(1,"Gizah","La Grande Pyramide de Gizeh",R.mipmap.wondercard_1),
                new WonderCard(2,"Rhodos","Le Colosse de Rhodes",R.mipmap.wondercard_2),
                new WonderCard(3,"Babylon","Les Jardins Suspendus de Babylone",R.mipmap.wondercard_3),
                new WonderCard(4,"Alexandria","Le Phare d'Alexandrie",R.mipmap.wondercard_4),
                new WonderCard(5,"Halikarnassos","Le Mausolée d'Halicarnasse",R.mipmap.wondercard_5),
                new WonderCard(6,"Ephesos","Le Temple d'Artémis à Ephèse",R.mipmap.wondercard_6),
        };

(sorry about the French)

So this all works, and from my main activity I only have to call, say,

card[0].name_long

to get an object's property.

Everything is fine for now, except I'll have to create more than 150 cards at some point. I want to keep my main activity as clean as possible and would like to get rid of the object construction 150 lines high in my main activity.

My question is : how can I create my cards and their properties on a different file , and call those created objects easily from my main activity file ?

(Android allows me to use an SQLite database, if it may help)

Easiest approach: put all the cards into database and just load them from there.

Not sure if I understood the question correct, but doesn't a plain old instance provider just provide the structure you need as follows?

public class WonderCardProvider {
    private static final WonderCard[] cards = new WonderCard[] {
        new WonderCard(0,"Olympia","La Statue de zeus à Olympie",R.mipmap.wondercard_0),
        ..
    };

    public static WonderCard[] getAllCards() {
        return cards;
    }
}
// Note that the `getAllCards()` should probably return in as unmodifiable (thus returning a collection instead of array).

card = WonderCardProvider.getAllCards();

Or is the question on having the definition of 150 cards outside any Java class? Then .properties files is the way to go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM