如何正确继承类并组织我的程序?

huangapple go评论59阅读模式
英文:

How do I properly inherit classes and structure my program?

问题

我正在为一个项目编写一些内容。我有各种世界对象(游戏中的实际对象,比如矿石、树木等),它们都有各自的区域。铜矿石有一个区域列表,而铁矿石有另一个区域列表。

有一段时间,我将矿石、树木等分别放入它们自己的类中。然而,我意识到它们共享了很多功能,本质上都是世界对象。

例如,我可能会查找与玩家等级最接近的矿石或树木。我可能会查找最靠近我的铜矿石,或者最靠近我的橡树。

在每个单独的类中,我都有一个执行这些确切操作的函数。我意识到我根本没有充分利用面向对象编程(OOP)。

我的问题是:你会如何设计和构建这样的内容?

其他类会继承自一个WorldObject类吗?

区域由我静态定义。我如何将一个区域附加到矿石类中的对象上?我会使用一个单独的LookUpTable类来存储信息,比如一个哈希表吗?区域会成为它自己的类,并且WorldObject类会继承它吗?

作为一个初学者,我只是不理解如何在这种情况下应用面向对象编程的原则。谢谢。

英文:

I’m writing something for a project. I have various world objects (actual objects in the game — such as ore, trees, etc.) and they all have their respective areas. Copper ore has have a list of areas while iron ore has another list of areas.

For a while, I separated Ore, Trees, etc into their own classes. However, I came to the realization that they shared a lot of functionality and are all essentially world objects.

For example, I may look up the ore or tree which is the closest to the players level. I may look up the closest Copper ore to me, or the closest Oak tree to me.

In each separate class, I had a function that did those exact things. I realized I wasn’t taking advantage of OOP at all.

My question is: how would you design and structure something like this?

Would there be a WorldObject class that the other classes extend from?

Areas are statically defined by me. How would I attach an area to the object Copper in the ore class? Would I use a separate LookUpTable class to store information, ie a hashmap? Would area be it’s own class and would the WorldObject class inherit it?

I’m just not understanding how to apply the principles of OOP in this scenario, specially as a novice. Thanks.

答案1

得分: 1

以下是翻译好的内容:

针对您的每个问题:

  1. 如何设计和构建类似的结构?

  2. 是否会有一个WorldObject类,其他类从它继承?

您可以为WorldObject使用超类,然后由您的对象类扩展:

public abstract class WorldObject { // 超类
    // 所有世界对象共有的私有方法
    private int posx;
    private int posy;
    
    // 所有世界对象共有的公有方法
    public double distance(int x, int y);
}

public class Tree extends WorldObject { 
    // 继承的方法
}

public class Ore extends WorldObject { 
    // 继承的方法
}
  1. 区域由我静态定义。

我不太清楚您所说的“区域”是什么意思?我假设这是对象的位置,您可以创建如下:

public class Position {
    private int x;
    private int y;
    private int z; // 如果需要的话
    // 构造函数,获取器和设置器
}

4A. 铜矿石?

如果“铜矿石”与其他“矿石”相比具有“特殊”属性,则可以将其创建为从WorldObjectOre类继承的新类。如果它只是具有不同属性,则将这些属性添加到Ore类中:

public class Ore extends WorldObject { 
    private String type; // 可能为“Copper”、“Iron”等
    private double weight; // ....等等

    // 继承的方法
}

4B. 如何将区域附加到矿石类中的铜对象?我是否应该使用单独的LookUpTable类来存储信息,例如哈希映射?

使用世界对象的列表,每个对象都有一个位置:

public List<WorldObject> objects = new ArrayList<>();

在您的情况下,实际上不需要使用映射。映射通常在ID、对象关系方面非常有用,您可以持续排序并快速找到所需的对象。

  1. 区域会成为自己的类,WorldObject类会继承它吗?

同样,我需要更多关于“区域”的信息。但是,如果它只是位置,您可以将其作为WorldObject中的一个字段包含进来。

英文:

Taking each of your questions:

  1. How would you design and structure something like this?

  2. Would there be a WorldObject class that the other classes extend from?

You can use super classes for a WorldObject which is then extended by your object classes:

public abstract class WorldObject { // Superclass
    // Private methods common to all world objects
    private int posx;
    private int posy;
    
    // Public methods common to all world objects
    public double distance(int x, int y);
}

public class Tree extends WorldObject { 
    // Inherited Methods
}

public class Ore extends WorldObject { 
    // Inherited Methods
}
  1. Areas are statically defined by me.

I am not sure what you mean by "area"? I am assuming it is the position of the object, which you can certainly create:

public class Position {
    private int x;
    private int y;
    private int z; // if needed
    // Contructor, getters and setters
}

4A. Copper Ore?

If "Copper Ore" has "special" properties compared to other "ores", then create it as a new class that extends from WorldObject, or Ore class. If it just has different properties, then add those properties to Ore class:

public class Ore extends WorldObject { 
    private String type; // which may be &quot;Copper&quot;, &quot;Iron&quot;, etc.
    private double weight; // ....etc.

    // Inherited Methods
}

4B. How would I attach an area to the object Copper in the ore class? Would I use a separate LookUpTable class to store information, ie a hashmap?

Use a list of World Objects, where each has a position:

public List&lt;WorldObject&gt; objects = new ArrayList&lt;&gt;();

You don't really need to use a Map in your case. Maps are generally useful in ID, OBJECT relationships, where you can continuously sort and quickly find the object you need.

  1. Would area be it’s own class and would the WorldObject class inherit it?

Again, I need more information on what "area" is. Though, if it is just position, you can include it as a field in WorldObject.

答案2

得分: 0

如果我是你,我不会选择使用继承,因为它可能会在以后带来一些限制。例如,在Java中,您不能有多个父对象,如果您想扩展另一个类,您该怎么办?

然而,在这种情况下,您可以设计您的层次继承来解决这种情况。但这并不总是(容易和轻松地)可能的。

我的解决方案是:优先选择接口而不是继承。您可以定义world接口,所有真实世界的对象都必须实现它。

英文:

If I were you I would not go for inheritance, since it may bring you several limitation later on. For instance you cannot have multiple parent object in java and if you want to extend another class what would you do?

However, in such cases you can design your hierarchical inheritance to solve such case.But that is not always (easily and effortlessly) possible.

My solution is : prefer interface over inheritance. you can define world interface and all real world objects must implement it.

huangapple
  • 本文由 发表于 2020年7月26日 09:03:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63095113.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定