创建控制对象或方法来处理两个对象之间的关系更好吗?

Is it better to create control object or methods to handle the relation between two objects?

假设我有一个游戏,在游戏中允许玩家购买或出售物品form/to设计关卡中的卖家最好的选择是什么,再创建一个class进行交易,或者每个 class 应该有自己的 buy/sell 方法来处理操作,或者这两个都是错误的:) ?

第二种情况如下所示:

public class Player {
    privte int money;
    // Other fields
    List<Integer> itemsPrice = new ArrayList<>();
    List<Integer> itemsCart = new ArrayList<>();
    public void buy(){
        // Add item to itemsCart 
        // decrease the amount of money
    }
    public void sell(){
        // remove item from itemsCart 
        // increase the amount of money
    }
}

class Seller{
    private int money;
    List<Integer> itemsPrice = new ArrayList<>();
    List<Integer> itemsCart = new ArrayList<>();
      public void buy(){
        // Add item to itemsCart 
        // decrease the amount of money
    }
    public void sell(){
        // remove item from itemsCart 
        // increase the amount of money
    }
}

您可以为两者创建一个抽象 class 并扩展父 class 并放置相同的

逻辑和变量到此父级 class,例如,您有 moneyitemsPrice

itemCartsPlayerSeller 中你可以将它们添加到父 class

public class Parent {

    protected int money;
    protected List<Integer> itemsPrice = new ArrayList<>();
    protected List<Integer> itemsCart = new ArrayList<>();
    public void buy(){
        // Add item to itemsCart 
        // decrease the amount of money
    }
    public void sell(){
        // remove item from itemsCart 
        // increase the amount of money
    }
}

extends 父代 PlayerSeller

public class Player extends Parent {

}

public class Seller extends Parent {

}

,以及 PlayerSeller

中任何一个的用法
Player p = new Player();
p.buy();
p.sell();

Seller s = new Seller();
s.buy();
s.sell();