公共领域的替代办法是什么? [英] What are the alternatives to public fields?

查看:150
本文介绍了公共领域的替代办法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java编程游戏,作为问题标题建议我使用我的类中的公共字段。 (暂时)

I am programming a game in java, and as the question title suggestions i am using public fields in my classes. (for the time being)

从我所看到的公共领域是坏的,我有一些了解为什么。 (但如果有人可以澄清你为什么不应该使用它们,那将是值得赞赏的)

From what i have seen public fields are bad and i have some understanding why. (but if someone could clarify why you should not use them, that would be appreciated)

事情也是从我所看到的,是使用私有字段,但是使用getter和setter来访问它们也不好,因为它首先破坏了使用私有字段的点。

The thing is that also from what i have seen, (and it seems logical) is that using private fields, but using getters and setters to access them is also not good as it defeats the point of using private fields in the first place.

所以,我的问题是,什么是替代品?或者我真的必须使用getter和setter的私有字段吗?

So, my question is, what are the alternatives? or do i really have to use private fields with getters and setters?

这里是我的一个类,以及它的一些方法。

For reference here is one of my classes, and some of its methods.

如果需要,我会详细说明。

I will elaborate more if needs be.

public double health;
//The player's fields.
public String name;
public double goldCount;
public double maxWeight;
public double currentWeight;
public double maxBackPckSlts;
public double usedBackPckSlts; // The current back pack slots in use
public double maxHealth; // Maximum amount of health
public ArrayList<String> backPack = new ArrayList<String>();

//This method happens when ever the player dynamically takes damage(i.e. when it is not scripted for the player to take damage.
//Parameters will be added to make it dynamic so the player can take any spread of damage.
public void beDamaged(double damage)
{
    this.health -= damage;
    if (this.health < 0)
    {
        this.health = 0;
    }
}

EDIT:为了检查的目的,这是我的 Weapon 类看起来现在:(代码示例不工作的原因,所以它看起来不正确。)

For checking purposes, this is what my Weapon class looks like now: (Code sample is not working for some reason, so it does not look right.)

private final double DAMAGE;
private final double SPEED;

public Weapon(double initialDmg,double initialSpd,String startName,double initialWg)
{
    DAMAGE = initialDmg;
    SPEED = initialSpd;
    setItemName(startName);
    setItemWeight(initialWg);
}

public double getSpeed() 
{
    return SPEED;
}


public double getDamage()
{
    return DAMAGE;
}

正如你所看到的, Weapon's DAMAGE SPEED 不需要更改,它们可以是最终的。 (如果,在游戏的后期,我决定这些价值可以是升级,所以可以说,我可以添加setters然后,验证,或者只是做一个新的武器与升级的值)他们设置在<$ c $

As you can see, as the Weapon's DAMAGE and SPEED do not need to be changed, they can be final's for the time being. (if, later in the game, i decided these values can be "Upgraded" so to speak, i may add setters then , with validation, or just make a new weapon with the upgraded values) They get set in the Weapon's constructor.

结论:getters和setter是很好的,只要它们被巧妙地使用,并且只在需要的时候使用。 (但是)

Conclusion: getters and setters are fine, as long as they are used smartly, and only used when needed. (however)

推荐答案

通常使用getter和setter而不是给其他对象直接更改字段的权限。这可能没有任何意义,当你看到99.99%的getters和setters不做任何事情,除了你可以通过直接访问字段做。但是当你决定当一个球员被损坏超过一点时,他会丢掉一半的库存会发生什么?或者你想限制多少个背包插槽可以使用的魔法物品?你必须搜索代码中修改字段的所有地方,或者,如果你使用getters和setter,你可以完全在类中进行更改。这是面向对象编程的核心 - 你已经封装了对象在对象本身内做什么的知识,而不是在与该对象交互的所有对象之间传播。

It's common to use getters and setters instead of giving other objects permission to change your fields directly. That might not make any sense when you see that 99.99% of your getters and setters don't do anything except what you could have done with direct access to the fields. But what happens when you decide that when a player is damaged beyond a point, he drops half his inventory? Or you want to restrict how many backpack slots can be used by magical items? You either have to hunt down all the places in your code where you modify the fields, or, if you used getters and setters, you make the changes entirely in the class. That's the heart of object oriented programming - that you've encapsulated "knowledge" of what an object does within the object itself, not spread it out among all the objects that interact with that object.

这篇关于公共领域的替代办法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆