在 Scala 中如何处理 POJO/JavaBean 模式? [英] How is the POJO/JavaBean pattern treated in Scala?

查看:62
本文介绍了在 Scala 中如何处理 POJO/JavaBean 模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像问题一样.scala 是否以与 Java 相同的方式推广它?或者它是否已经进化为更符合 Scala 的习惯?还是已经变得无关紧要了?

Just like the question. Does scala promote it same way as Java? Or has it been evolved to be more idiomatic to Scala? Or has it been made irrelevant?

POJOs 和 JavaBeans 的含义:

POJOs and JavaBeans meaning:

  1. 不带参数的构造函数
  2. 属性是私有的,getter 和 setter 是公开的
  3. getter 和 setter 定义属性,隐藏属性

<小时>

此外,Scala 是否对使用旧的 publicprivateprotected 属性设计有意见(对不起,我不喜欢使用这个术语)这与这个问题有关吗?


Also, does Scala have opinions (sorry, I dislike using this term) on using the old public, private, protected attribute designs that is relevant to this question?

推荐答案

Scala 也有类似 POJO 的习惯用法,但它们与 JavaBeans 不同,Scala 侧重于不同的方面.

Scala also has POJO-like idioms but they are different than JavaBeans and Scala puts emphasis on different aspects.

  1. Scala 有不同的命名约定:

  1. Scala has different naming conventions:

def foo: Foo        //Foo getFoo() in Java
def foo_=(foo: Foo)  //void setFoo(Foo foo) in Java

通过这种方式,您始终可以编写 obj.fooobj.foo = bar,即使您决定从 getter/setter 切换到直接字段访问,反之亦然.这称为统一访问原则.

This way you can always write obj.foo and obj.foo = bar even if you decide to switch from getters/setters to direct field access and vice-versa. This is called uniform access principle.

由于 Java 互操作性,引入了 @BeanProperty 注释:

Due to Java interoperability, @BeanProperty annotation was introduced:

@BeanProperty var foo: Foo = _

上面的代码不仅创建了类似 Scala 的 getter/setter,还创建了类似 Java 的方法,因此所有 Java 框架都可以无缝地工作.

the code above not only creates Scala-like getters/setters, but Java-like as well, so all Java frameworks work seamlessly.

Scala 强制您在变量 (var) 和值 (val) 之间做出决定,因此您会发现自己更频繁地使用不可变对象

Scala forces you to decide between variables (var) and values (val), so you find yourself much more often using immutable objects

我真的更喜欢不可变对象和构造函数中的初始化,这在 Scala 中变得非常容易:

I really prefer immutable objects and initialization in constructor, which has been made very easy in Scala:

class Foo(val age: Int, val name: String)

甚至(case 类中的 val 默认情况下):

or even (val by default in case classes):

case class Foo(age: Int, name: String)

这段代码非常简单.但是,如果需要配合Java框架,还是需要无参数的构造函数和setter:

This piece of code is brilliant in its simplicity. However, if you need to cooperate with Java frameworks, you still need no-argu constructor and setters:

public class Foo(var age: Int, var name: String) {

    def this() {
        this(0, "")
    }

}

注意 val 被替换为 var.

与 Java 相比,Scala 中的访问修饰符的默认设置稍好一些:

Access modifiers in Scala have slightly better defaults compared to Java:

class Foo(var a: Int, x: Int) {

    var b: Int = _

    private var c: Int = _

    private[this] var d: Int = _

    def twisted = a + b + c + d + x

}

变量 ab 将成为带有 public getter/setter 的 private 字段(字段默认是私有的,方法是公开的).cd 也是私有变量.但是额外的 private[this] 使得 d 可以在类内部直接访问,而不是通过私有的 getter/setter.

Variables a and b will become private fields with public getters/setters (fields are private by default, methods are public). c and d are private variables as well. But the extra private[this] makes d be accessible directly internally in the class rather than by private getter/setter.

因为 x 在构造函数旁边的某个地方使用,Scala 也会自动为其创建私有字段.但是没有生成 getter/setter,它直接在 twisted 方法中访问(与 d 相同).

Because x is used somewhere beside the constructor, Scala automatically creates private field for it as well. However no getters/setters are generated, it is accessed directly in twisted method (same as d).

更新:在评论中,您询问重命名 getter/setter.这是更复杂的例子.getter/setter 同时计算基于两个字段的值:

UPDATE: In comments you are asking about renaming getters/setters. Here is even more complex example. Getters/setters are computing the value based on two fields at the same time:

class Foo {

    private[this] var first: Char = _
    private[this] var rest: String = _

    def fake = first + rest

    def fake_=(s: String) {
        first = s.head
        rest = s.tail
    }

}

内部看起来很复杂,但从外面看却是不错的老房子:

Looks complicated inside, but from the outside it looks like good old property:

val foo = new Foo()
foo.fake = "ABC"
println(foo.fake)

就像是:

class Foo(var fake: String)

这篇关于在 Scala 中如何处理 POJO/JavaBean 模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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