java 创建自己的方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16720059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:46:36  来源:igfitidea点击:

Creating your own Method?

javamethods

提问by user2388169

I might have used the wrong word, so if I did, please tell me so I can correct it. I'm just going by what I think it would be called, based on what I gathered from the Java Hierarchy.
But here is my question:

我可能用错了词,所以如果我用错了,请告诉我,以便我改正。我只是根据我从 Java Hierarchy 收集到的信息,按照我认为的名称来命名。
但这是我的问题:

For this example, I will explain what I see, ask the question, and then provide an example. (I will be using the word Methodbecause that's the word I think is appropriate, unless told otherwise)

对于这个例子,我将解释我所看到的,提出问题,然后提供一个例子。(Method除非另有说明,否则我将使用这个词,因为这是我认为合适的词)

Do you know how you can have a variable, and for the sake of this example, it's a String? Now for this string, you could use a method like myString.setText("String");, or myString.equalsIgnoreCase(otherString);.

您知道如何拥有一个变量,在本示例中,它是一个String? 现在对于这个字符串,您可以使用类似myString.setText("String");, 或的方法myString.equalsIgnoreCase(otherString);

Now, for my question, is it actually possible to create your own method?

现在,对于我的问题,实际上是否可以创建自己的方法?

For the sake of this example, let's say myString.setText("Knife");. Now since we know myStringis a knife, could I create a method where I could call myStringto see if it was equipped, or to make it equipped? For example: myString.setEquipped(true);and if I wanted to check if it was equipped or not, I could call myString.getEquipped();

为了这个例子,让我们说myString.setText("Knife");。现在既然我们知道myString是一把刀,我能不能创建一个方法,我可以调用myString它来查看它是否被装备,或者让它装备?例如:myString.setEquipped(true);如果我想检查它是否配备了,我可以打电话myString.getEquipped();

Am I using the word Methodproperly? And if my original question is possible, could we provide a small example?

Method正确使用这个词吗?如果我最初的问题是可能的,我们可以提供一个小例子吗?

Thanks in advance for all your time and work.

提前感谢您的所有时间和工作。

回答by Peter Lawrey

Yes,, you can do all that, but I suggest you use your IDE to generate these methods. e.g. If you have.

是的,你可以做所有这些,但我建议你使用你的 IDE 来生成这些方法。例如,如果你有。

public class Item {
    private final String name;
    private boolean equiped;
}

Your IDE can generate these methods for you.

您的 IDE 可以为您生成这些方法。

public class Item {
    private final String name;
    private boolean equiped;

    public Item(String name) {
        this.name = name;
    }

    public boolean isEquiped() {
        return equiped;
    }

    public void setEquiped(boolean equiped) {
        this.equiped = equiped;
    }

    public String getName() {
        return name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Item item = (Item) o;

        if (equiped != item.equiped) return false;
        return !(name != null ? !name.equals(item.name) : item.name != null);
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + (equiped ? 1 : 0);
        return result;
    }

    @Override
    public String toString() {
        return "Item{" +
                "name='" + name + '\'' +
                ", equiped=" + equiped +
                '}';
    }
}

You can use it like this

你可以像这样使用它

Item knife = new Item("Knife");
knife.setEquiped(true);
System.out.println("Item is " + knife);

回答by Henry Keiter

I think you're just looking for the basics of object-oriented programmingas they are implemented in Java (and yes, "method" is correct.) You want to define an object with some methods that modify its internal state. For a very simple example:

我认为您只是在寻找面向对象编程的基础知识,因为它们是在 Java 中实现的(是的,“方法”是正确的。)您想用一些修改其内部状态的方法来定义一个对象。对于一个非常简单的例子:

public class MyObject {
    // Instance variables
    private String text;
    private boolean equipped;

    public MyObject() {
        // Default constructor
    }

    // setters
    public void setText(String text) {
        this.text = text;
    }
    public void setEquipped(boolean equipped) {
        this.equipped = equipped;
    }

    // getters
    public String getText() {
        return this.text;
    }
    public boolean isEquipped() {
        return this.equipped;
    }

    // main method
    public static void main(String[] args) {
        MyObject o = new MyObject();
        o.setEquipped(true);
        o.setText("Knife");
        System.out.println("Is 'o' equipped?");
        System.out.println(o.isEquipped());
        System.out.println("What is 'o'?");
        System.out.println(o.getText());
    }
}

You can't modify the Stringclass, as others have said, because it is final. However, you can (and should!) define your own classes, with all the methods and objects that they need to do what they need to do. For instance, in the example you gave, you wanted to be able to set the object's textand then tell whether it is equipped. That suggested to me that you want a class where each instance of the class contains a String(text) and a boolean(equipped)

Stringclass正如其他人所说,您不能修改,因为它是final。但是,您可以(并且应该!)定义您自己的类,以及它们需要做的所有方法和对象。例如,在您给出的示例中,您希望能够设置对象的text然后判断它是否已配备。这向我建议您想要一个类,其中该类的每个实例都包含一个String( text) 和一个boolean( equipped)

That's how I chose to write my example the way that I did: your object wants a "has-a" relationship to both Stringand boolean. This is in contrast to an "is-a" relationship, since it wouldn't make sense for your object to actually beeither a Stringor a boolean.

这就是我选择以我所做的方式编写示例的方式:您的对象希望与 和都具有“ has-a”关系。这与“ is-a”关系相反,因为您的对象实际上a或 a没有意义的。StringbooleanStringboolean

However, it might make sense for this object to bea Weapon, for instance. In that case, you would want to define a Weaponclass, and then extendthat class with a Knifeclass, a Swordclass, and so on.

但是,它可能是有意义的这个对象一个Weapon,例如。在这种情况下,您可能希望定义一个Weapon类,然后使用一个类、一个类等扩展该类。KnifeSword

回答by djechlin

Only via

仅通过

  1. reflection, which is not what you really want to do
  2. Modifying the original source. For most purposes you can't modify the source of String.
  1. 反思,这不是你真正想做的
  2. 修改原始来源。大多数情况下,您无法修改String.

Then there's inheritance in which you derive a class and add methods. Most classes you can, but Stringis not one of them as it is final.

然后是继承,您可以在其中派生一个类并添加方法。大多数课程都可以,但String不是其中之一final