Java 中的 getter/setter

发布于 2024-07-21 13:36:42 字数 1228 浏览 6 评论 0原文

我是 Java 新手,但有一些使用 ActionScript 3 的 OOP 经验,因此我尝试依靠我所知道的内容进行迁移。

在 ActionScript 3 中,您可以使用 get 和 set 关键字创建 getter 和 setter,这意味着您可以在类中创建方法并通过该类实例的属性访问数据。 我可能听起来很复杂,但事实并非如此。 下面是一个示例:

class Dummy{

    private var _name:String;

    public function Dummy(name:String=null){
        this._name = name;
    }

    //getter
    public function get name():String{
        return _name;
    }

    //setter
    public function set name(value:String):void{
    //do some validation if necessary
        _name = value;
    }

}

我将按以下方式访问对象中的 name

var dummy:Dummy = new Dummy("fred");
trace(dummy.name);//prints: fred
dummy.name = "lolo";//setter
trace(dummy.name);//getter

我将如何在 Java 中执行此操作?

仅仅拥有一些公共字段是不可能的。 我注意到在方法前面有使用 get 和 set 的约定,我对此很满意。

例如,

class Dummy{

    String _name;

    public void Dummy(){}

    public void Dummy(String name){
        _name = name;
    }

    public String getName(){
        return _name;
    }

    public void setName(String name){
        _name = name;
    }

}

Java 中是否有 ActionScript 3 getter/setter 的等效项,如 访问私有字段作为类实例的字段,但有一个在类内部实现该字段的方法?

I'm new to Java, but have some OOP experience with ActionScript 3, so I'm trying to migrate relying on stuff I know.

In ActionScript 3 you can create getters and setters using the get and set keywords, meaning you create a method in the class and access data through a property of an instance of that class. I might sound complicated, but it's not. Here's an example:

class Dummy{

    private var _name:String;

    public function Dummy(name:String=null){
        this._name = name;
    }

    //getter
    public function get name():String{
        return _name;
    }

    //setter
    public function set name(value:String):void{
    //do some validation if necessary
        _name = value;
    }

}

And I would access name in an object as:

var dummy:Dummy = new Dummy("fred");
trace(dummy.name);//prints: fred
dummy.name = "lolo";//setter
trace(dummy.name);//getter

How would I do that in Java?

Just having some public fields is out of the question.
I've noticed that there is this convention of using get and set in front of methods, which I'm OK with.

For example,

class Dummy{

    String _name;

    public void Dummy(){}

    public void Dummy(String name){
        _name = name;
    }

    public String getName(){
        return _name;
    }

    public void setName(String name){
        _name = name;
    }

}

Is there an equivalent of ActionScript 3 getter/setters in Java, as in
accessing a private field as a field from an instance of the class, but having a method for implementing that internally in the class?

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

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

发布评论

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

评论(7

眼趣 2024-07-28 13:36:43

您的 Java 代码很好,只是您希望将 _name 私有

Java 中没有像 AS3 示例中那样的 getset 关键字。 抱歉,这并没有比你已经做的更好。

更正的代码:

class Dummy {
  private String _name;

  public void Dummy() {}

  public void Dummy(String name) {
    setName(name);
  }

  public String getName() {
    return _name;
  }

  public void setName(String value) {
    _name = value;
  }
}

Your Java code is fine, except that you would, want to make _name private.

There are no get and set keywords in Java as in your AS3 example. Sorry, it doesn't get better than what you're doing already.

Corrected code:

class Dummy {
  private String _name;

  public void Dummy() {}

  public void Dummy(String name) {
    setName(name);
  }

  public String getName() {
    return _name;
  }

  public void setName(String value) {
    _name = value;
  }
}
静赏你的温柔 2024-07-28 13:36:43

没有。 AS3 getter 和 setter 是 ECMAScript 的东西。 在 Java 中,您只能使用 getVal() 和 setVal() 风格的函数——没有任何语法糖可以让您轻松完成任务。

我认为 Eclipse 可以帮助自动生成这些类型的东西......

Nope. AS3 getters and setters are an ECMAScript thing. In Java, you're stuck with the getVal() and setVal() style functions--there isn't any syntactic sugar to make things easy for you.

I think Eclipse can help auto-generating those types of things though...

九命猫 2024-07-28 13:36:43

遗憾的是,没有,java 中没有同等的语言级支持。

get* 和 set* 模式在 java 文化中已经非常成熟,您会发现 IDE 对它们有强大的支持(例如,eclipse 会自动为您创建它们),并且如果您正在使用首先使用表达式语言的东西为 jsps (EL) 制作,那么您将能够使用属性表示法来访问 getter 和 setter。

Sadly, no, there isn't the equivalent language-level support in java.

The get* and set* patterns though are so established in java culture that you'll find strong IDE support for them (e.g., eclipse will make them for you automatically), and if you're working in something that uses the expression language first made for jsps (EL), then you'll be able to use the property notation to access getters and setters.

飘然心甜 2024-07-28 13:36:43

我会考虑不使用 getter 或 setter,因为它们在您的情况下不会做任何事情,除了使代码更加复杂。 这是一个没有 getter 或 setter 的示例。

class Dummy {
  public String name;
  public Dummy(String name) { this.name = name; }
}

Dummy dummy = new Dummy("fred");
System.out.println(dummy.name);//getter, prints: fred
dummy.name = "lolo";//setter
System.out.println(dummy.name);//getter, prints: lolo

恕我直言,不要让事情变得比你需要的更复杂。 通常情况下,增加复杂性会受到 You-Aint-Gonna-Need-It

I would consider not having the getter or setter as they don't do anything in your case except make the code more complicated. Here is an example without getters or setters.

class Dummy {
  public String name;
  public Dummy(String name) { this.name = name; }
}

Dummy dummy = new Dummy("fred");
System.out.println(dummy.name);//getter, prints: fred
dummy.name = "lolo";//setter
System.out.println(dummy.name);//getter, prints: lolo

IMHO Don't make things more complicated than you need to. It so often the case that adding complexity will suffer from You-Aint-Gonna-Need-It

风筝在阴天搁浅。 2024-07-28 13:36:43

另外,在添加 setter 和 getter 之前,最好问问自己为什么要公开相关对象的内部数据。

我建议你读读这篇文章——
http://www.javaworld.com/javaworld/ jw-09-2003/jw-0905-toolbox.html

Also before adding setters and getters, it might be a good idea to ask yourself why are you exposing the internal data of the Object in question.

I suggests you read this article -
http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html

冷清清 2024-07-28 13:36:43

在 Java 中,在不暴露对象内部的情况下,唯一的选择是创建自己的 getter 和 setter,如示例中所示。

约定是在要更改的字段前面添加 getset。 因此,如您的示例所示,字段 name 将分别具有 getNamesetName 方法作为其相应的 getter 和 setter。

In Java, the only option you have without exposing the internals of your object is to make your own getters and setters as you have in your example.

The convention is to prepend get or set in front of the field which is being altered. So, as in your example, the field name would have getName and setName methods as their corresponding getter and setter, respectively.

浪荡不羁 2024-07-28 13:36:43

一种独立于 IDE 的方法是使用 Lombok,这是一个基于注释的库,可生成 getter、setter 和甚至equals()hashcode()。 它为编译器执行此操作,但不在源文件中执行此操作,因此您不必查看方法,只需使用它们即可。

An IDE-independent way is to use Lombok, an annotation-based library that generates getters, setters, and even equals() and hashcode(). It does this for the compiler, but not in the source file, so you don't have to look at the methods, just use them.

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