简体   繁体   English

了解Scala中的类和对象

[英]Understanding class and object in scala

I'm very new to scala and now I have to work on a project which is written both in scala and java. 我对scala还是很陌生,现在我必须从事一个用scala和java编写的项目。 I came across with this-like construction: 我遇到了这样的结构:

class SomeType{
    //...
}

trait Trait1 extends scala.AnyRef{
}

trait Trait2 extends extends scala.AnyRef{
}

class MyClass(arg : SomeType) extends Trait1{
    //...
}

object MyClass extends Trait2{
    //...
}

It's kind of mind-numbing. 这有点麻木。 As far as I got by reading this answer we can think of object s as just classes with abstract methods. 据我阅读的答案,我们可以将object视为具有抽象方法的类。 Ie we could (In my opinion) define some mapping with Class<T> object in java. 即,我们可以(在我看来)使用Java中的Class<T>对象定义一些映射。

But in this example class and object extends different trait s. 但是在此示例中, classobject扩展了不同的 trait And that's what I was really confused by. 这就是我真正的困惑。 I cannot imagine what it means and why it was used. 我无法想象它的含义以及为什么使用它。

I'll try to explain what a Scala object is from the perspective of someone who knows Java. 我将尝试从了解Java的人的角度解释Scala object是什么。

In Java, you can define "normal" class members and static class members. 在Java中,您可以定义“普通”类成员和static类成员。 When something (for example a member variable) is static in Java, then there's only one instance of that member variable, which is shared by all instances of the class. 当Java中某些东西(例如成员变量)是static时,则该成员变量只有一个实例,该类的所有实例都共享该实例。

In Scala, there is no static . 在Scala中,没有static Compared to Java: whatever you would make static in Java, you would put in an object in Scala. 相比于Java的:无论你会使static在Java中,你将提出一个object在Scala中。

A Scala object is a singleton - there's only a single instance of it, just like static members in Java of which there's only a single instance. Scala object是一个单例object只有一个实例,就像Java中的static成员一样,它只有一个实例。

I hope that makes it a little bit more clear what a Scala object is exactly. 我希望这可以使一个Scala object的确切含义更加清楚。

A Scala object can extend classes or traits just like any other class or trait, so that it inherits whatever was defined in the class or trait that it extends. Scala object可以像其他任何类或特征一样扩展类或特征,以便它继承其扩展的类或特征中定义的任何内容。 There's really no reason why this should not be possible. 确实没有理由为什么这不可能。

Note that if you have a class and an object with the same name, then those two belong together - they can see each other's private members. 请注意,如果您有一个同名的class和一个object ,则这两个对象属于同一class -他们可以看到彼此的私有成员。 The object is called the companion object of the class. object称为类的伴随对象

Note that this does not mean that the object is an instance of the class. 请注意,这并不意味着该object是类的一个实例。 It is not - it is a separate thing that stands beside the class. 不是-它是类的另一部分。 Maybe your confusion about the class and object extending different traits comes from a misunderstanding about that point. 也许您对类和对象具有不同特征的困惑来自对这一点的误解。

A class is just like any other class in other languages. 一门课就像其他语言的其他课一样。 You define class just like any other language with some syntax difference. 您可以像定义任何其他语言一样定义类,就像其他任何语言一样。 Also, there are case classes which comes with some handy methods defined and allows for easy initialization of objects. 此外,还提供了一些案例类 ,这些案例类定义了一些方便的方法,可以轻松地初始化对象。

class Person(val name: String)
val me = new Person("My name")

However, object is a class with single object only. 但是,对象是仅具有单个对象的类。 This makes it interesting as it can be used to create static members of a class using companion object . 这使它很有趣,因为它可用于使用伴随对象创建类的静态成员。 This companion object has access to private members of the class definition and it has the same name as the class you're defining. 此伴随对象可以访问类定义的私有成员,并且与您定义的类具有相同的名称。

class Person(var name: String) {

  import Person._

  def hi(): String = sayHello(name)
}

object Person {
  private def sayHello(name: String): String = "Hello " + name
}

val me = new Person("My name")
me.hi()

Also, noteworthy point is that object class is lazily created which is another important point. 另外,值得注意的一点是对象类是延迟创建的,这是另一个重要点。 So, these are not instantiated unless they are needed in our code. 因此,除非我们的代码中需要它们,否则它们不会被实例化。

If you're defining connection creation for JDBC, you can create them inside object to avoid duplication just like we do in Java with singleton objects. 如果要为JDBC定义连接创建,则可以在对象内部创建它们以避免重复,就像在Java中使用单例对象一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM