子类中的变量可见性

发布于 2024-10-01 16:00:58 字数 789 浏览 0 评论 0原文

这是一个非常简单的问题,我很抱歉我这么菜鸟:/

我是 Java 新手。我在市场上有一款游戏运行得很好,但代码都是垃圾。基本上,我写它就像写 JavaScript 一样。我正在尝试了解有关面向对象编程的更多信息。因此,在我的新游戏中,我一直在创建从自定义类扩展的对象。例如(这实际上不是我正在做的事情):

public class Color{
    public final int STROKE_WIDTH = 3;

    public Color(){}
}


public class Orange extends Color{
    public int alpha = 30;

    public Orange(){
        super();
    }
}

现在,假设在我的游戏主线程中,我实例化了许多颜色(橙色、红色、紫色),并将它们存储在 ArrayList中。。我在游戏中的另一个点迭代此列表,并且想要访问 Color 的 Alpha。我的问题是,由于它们都包含在超类中,所以我不能只说 colorObj.alpha,但我确实可以访问 colorObj.STROKE_WIDTH。同样,我将无法访问 Orange 类中的任何方法。

一定有办法做到这一点,我只是新人。我不希望有人坐在这里输入解释......我不想浪费你的时间。但如果有人可以粘贴一些初学者教程的链接或可以帮助我解决这个问题的东西,我将不胜感激!

This is a very simple question, and I apologize for being so noobish :/

I'm new(ish) to Java. I've got a game on the Market that works quite well, but the code is all rubbish. Basically, I wrote it like I was writing JavaScript. I'm trying to learn more about Object Oriented Programming. So, in my new game I've been creating Objects that extend from a custom class. For example (this isn't actually what I'm doing):

public class Color{
    public final int STROKE_WIDTH = 3;

    public Color(){}
}

public class Orange extends Color{
    public int alpha = 30;

    public Orange(){
        super();
    }
}

Now, say in my game's main thread I instantiate many Colors (Orange, Red, Purple) and I store them in an ArrayList<Color>. I iterate through this list at another point in the game, and I want to access the Color's alpha. My problem is, since they're all wrapped into the superclass, I can't just say colorObj.alpha, but I do have access to colorObj.STROKE_WIDTH. Similarly, I wouldn't be able to access any methods in the Orange class.

There must be a way to do this, I'm just new. I don't expect anyone to sit here and type out an explanation.. I wouldn't want to waste your time. But if someone could just paste a link to some beginner's tutorial or something that would help me out with this, I'd appreciate it!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鹿! 2024-10-08 16:00:58

我在游戏中的另一个点迭代此列表,并且我想访问颜色的 Alpha。我的问题是,因为它们都包含在超类中,所以我不能只说 colorObj.alpha

如果所有实际颜色都有 alpha,那么将其放入 Color 类中。它应该包含所有颜色共有的属性和行为。

旁注:面向对象编程的主要内容之一是封装。公共数据成员公开了类的内部表示。通常,您将该表示形式隐藏在访问器方法后面,例如:

class Color {
   private int alpha;
   public int getAlpha() { return alpha; }
}

现在没有人知道您的表示形式;没有人可以将 alpha 更改为对 Color 没有意义的值,您可以将其设置为只读或只写,您甚至可以将其更改为计算值,并且使用它的客户端都不会受到影响。

事实上,OOP 中的“最佳实践”是将其发挥到极致,对接口而不是类进行编程。您可以为 Colors 定义一个没有数据成员并且实际上无法实例化的接口。它代表 Colors 将遵守的合同,仅此而已。例如:

interface Color {
   int getR();
   int getG();
   int getB();
   int getA();
}

class Red implements Color {
   ...

当然,这对于像颜色这样的东西来说有点过分了。我知道这是一个人为的示例,但颜色没有不同的行为,只是不同的,因此您将拥有以下实例,而不是具有红色、橙色等类代表每个的颜色类。

同样,我将无法访问 Orange 类中的任何方法。

真的。但其想法是,处理颜色的代码不需要知道它们的实际颜色是什么。这就是多态性的力量。如果需要一些特殊情况处理,您可以将颜色“向下转换”为特定的子类型,但需要这样做通常是草率设计的症状。

I iterate through this list at another point in the game, and I want to access the Color's alpha. My problem is, since they're all wrapped into the superclass, I can't just say colorObj.alpha

If all your actual colors have alpha, then put that in the Color class. It should contain the attributes and behaviors that are common to all colors.

Side note: one of the tentants of object oriented programming is enscapsulation. Public data members expose the internal representation of your class. Typically you hide that representation behind accessor methods, such as:

class Color {
   private int alpha;
   public int getAlpha() { return alpha; }
}

Now nobody knows your representation; nobody can change alpha to a value that doesn't make sense for a Color, you could make it read-only or write-only, you could even change it to a computed value and none of the clients who use it are affected.

In fact, a "best practice" in OOP is taking this to extreme and programming to interfaces, not classes. You would define an interface for Colors which has no data members and in fact cannot be instantiated. It represents the contract that Colors will obey and nothing more. For instance:

interface Color {
   int getR();
   int getG();
   int getB();
   int getA();
}

class Red implements Color {
   ...

Of course, this is overkill for something like a Color. I know it's a contrived example, but Colors don't have different behavior, just different values, so rather than having class Red, Orange, etc. you would have instances of a Color class which represent each.

Similarly, I wouldn't be able to access any methods in the Orange class.

True. But the idea is that the code which is dealing with Colors doesn't need to know what actual colors they are. That's the power of polymorphism. If there is some special-case processing required, you can "down cast" a Color to a specific subtype, but the need to do that is often a symptom of sloppy design.

转角预定愛 2024-10-08 16:00:58

如果你写:

Color c = new Orange();
c.alpha = 10; // Won't compile

那不会编译,不是因为 alpha 被包装,而是你必须在超类中定义它。类似地,在上述声明中无法访问 Orange 中定义的所有方法,因为当您引用“c”时,意味着它是一个 Color,但它不一定是一个 Orange。总而言之,上面声明中的“c”允许您访问 Color 的所有公共成员,但不能访问 Orange 的成员。

学习 OOP 并不容易,特别是对于来自脚本开发的人来说。我建议您首先阅读一本有关 OOP 或 Java 的好书,而不是仅仅阅读在线材料。 Head First 系列是我强烈推荐的系列之一。

If you write:

Color c = new Orange();
c.alpha = 10; // Won't compile

That won't compile not because the alpha is wrapped, but you have to define that in superclass. Similarly, all methods defined in Orange is not accessible in the above declaration, because, when you reference 'c', it means it's a Color, but it is not necessarily an Orange. To wrap up, the 'c' in above declaration let you access to all public members of Color but NOT those in Orange.

Learning OOP is not easy especially for people come from Script dev. Instead of just reading online materials, I suggest you grab a good book about OOP or Java to start off. The Head First Series is one of those I highly recommended.

习惯成性 2024-10-08 16:00:58

那么,您必须将继承层次结构中所有类相同的所有变量和方法移动到基类,因此在您的情况下,我会将变量 alpha 移动到基类。

稍后,如果您想访问特定于继承类的方法或变量,您必须进行强制转换,以防万一您有一个 List myColors;假设列表中的第 i 个对象是橙色,并且它有一个名为 mix(Color otherColor) 的方法,那么您必须简单地执行此操作:

((Orange)myColors.get(i)).mix(someOtherColor);

最后一件事,不要使用公共类成员变量,始终为它们提供尽可能低的可见性(私有、受保护),并且始终使用 getter 来访问它们并使用 setter 来修改它们,因为这是一个非常基本的 OO 概念:)

Well you have to move all the variables and methods which are the same for all classes in the inheritance hierarchy to the base class so in your case i would move the variable alpha to the base class.

Later on if you want to access methods or variables specific to an inherited class you have to do a cast so in case you have a List myColors; and lets say the i-th object in the list is an Orange color and it has a method called mix(Color otherColor) then you have to simply do this :

((Orange)myColors.get(i)).mix(someOtherColor);

And one last thing, dont use public class member variables, always give them the lowest possible visibility (private, protected) and always use getters to access them and setters to modify them, as this is a very basic OO concept :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文