有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java制作纸牌游戏,有关于类的问题吗

我是一个自学成才的程序员,只学过一门java课程,所以我对这门课还是很陌生,我正在尝试从头开始制作我的第一个真正的应用程序

基本上,我正在制作一款我自己的纸牌游戏的应用程序版本(只是为了我自己!不是为了卖什么的!)在这个纸牌游戏中,你基本上用你的起始牌从主牌堆中抽出的一系列牌中“购买”更多的牌。这些卡有几个与之相关的值,可以很好地转化为“力量等级”和“点值”之类的应用程序,此外,当你玩“抽一张额外的卡”、“销毁一张卡”或“给另一个玩家一张负面卡”之类的游戏时,这些卡都会做一些特殊的事情

我已经为游戏制作了整个UI,并且已经(大部分)完成了,现在我正在做所有的工作——代码。我创建了一个名为Card的构造函数类,如下所示:

public class Card 
{
private int power;
private int value;
private String type;
private String cardName;
private ImageIcon image;

    public Card(int power, int value, String type, String cardName, ImageIcon image)
    {
        this.power = power;
        this.value = value;
        this.type = type;
        this.cardName = cardName;
        this.image = image;
    }
...
}

省略号表示我的所有getter/setter方法

所以你可以看到,我的卡类将为它们分配所有必要的值,但只有一个-它们每个人都做的特殊“事情”(抽卡/销毁卡..等等)

我不确定我应该把它放在哪里/如何放在我的代码中。因为I有一个类型值,它将代表您可以绘制的不同类型的卡(装备/超级力量/英雄/等等)我想我应该为每种卡片类型单独设置一个类。。这是好的编码实践吗

总之,在结束这篇极其冗长的文章之前,我应该把我所有不同的卡片“类型”分成不同的类吗?我应该如何实现每张卡片所做的特殊“事情”?我觉得它将被包装在一个方法中,但据我所知,我不能让一个方法成为构造函数的一部分,对吗


共 (2) 个答案

  1. # 1 楼答案

    您可以使该卡成为接口抽象类。如果使用后者,实际上可以定义类似于上面已经定义的逻辑。根据所走的路线,每个“卡”要么实现接口,要么扩展抽象类

    该接口的缺点是必须将所有字段作为子类的私有变量重新实现

    摘要

    public abstract class Card  {
        protected int power;
        protected int value;
        protected String type;
        protected String cardName;
        protected ImageIcon image;
    
        // You cannot instantiate this directly, only through children.
        public Card(int power, int value, String type, String cardName, ImageIcon image) {
            this.power = power;
            this.value = value;
            this.type = type;
            this.cardName = cardName;
            this.image = image;
        }
    
        // Getters/Setters...
    }
    
    public class AttackCard extends Card {
        public AttackCard(int power, int value, String type, String cardName, ImageIcon image);
            super(power, value, type, cardName, image);
        }
    }
    

    接口

    public interface Card  {
        int getPower();
        int getValue();
        String getType();
        String getCardName();
        ImageIcon getImage();
    }
    
    public class AttackCard implements Card {
        private int power;
        private int value;
        private String type;
        private String cardName;
        private ImageIcon image;
    
        public AttackCard(int power, int value, String type, String cardName, ImageIcon image);
            this.power = power;
            this.value = value;
            this.type = type;
            this.cardName = cardName;
            this.image = image;
        }
    
        // Implement methods from Card...
    }
    

    编辑

    如果您正在实现DC Comics牌堆游戏,那么您可能只需要为所有牌创建一个抽象类,并为英雄、恶棍、超级力量和初学者创建一个接口。不知道他们都是什么类型的。。。然后,所有英雄将扩展抽象卡类,以获得卡的所有公共属性,并实现特定于英雄的方法

    您不需要为每张卡单独设置一个类。每个英雄都只是英雄的一个实例。你只需要收集一些英雄卡

    // Hierarchy
    abstract class Card
    interface Hero
    interface Villain
    class HeroCard extends Card implements Hero
    class VillainCard extends Card implements Villain
    
    // Example constructors definitions for reference?
    Card(String name, int value, int power);
    HeroCard(String name, int value, int power);
    VillainCard(String name, int value, int power);
    
    // Create all heroes
    List<Hero> heroes = new ArrayList<Hero>();
    heroes.add(new HeroCard("Green Arrow", 5, 2));
    
    // Create all villains
    List<Villain> villains = new ArrayList<Villains>();
    villains.add(new VillainCard("Ra's Al Ghul", 8, 3));
    
    // All all the heroes and villains to all cards
    List<Card> cards = new ArrayList<Card>();
    cards.addAll(heroes);
    card.addAll(villains);
    
  2. # 2 楼答案

    您可以使用inheritance在通用类将在那里的地方,您可以将functionality公共用于所有Card类,然后在每个子类中Card您可以放置类的特定功能,也可以使用基类的公共功能。还可以重写超类的方法

    http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

     public class SuperCardClass {
     ........
    
      public void someCommonFunction() {
    
      }
    
     }
    
     public class SubCardFirstClass extends SuperCardClass {
    
      @Override
       public void someCommonFunction() {
         //specific to sub class
       }
    
     }