scala 字段覆盖_Scala中的字段覆盖

scala 字段覆盖

Scala字段覆盖 (Scala field overriding)

Overriding is the concept in which the child class is allowed to redefine the members of the parent class. Both methods and variables/ fields can be overridden in object-oriented programming. In Scala as well both methods and Fields can be overridden but overriding of fields has some restrictions on implementation.

覆盖是允许子类重新定义父类成员的概念。 在面向对象的编程中,方法和变量/字段都可以被覆盖。 在Scala中,方法和字段都可以被覆盖,但是字段的覆盖对实现有一些限制。

Scala中的覆盖字段 (Overriding fields in Scala)

For overriding fields in Scala, some rules must be kept in mind to avoid errors that arise while implementing this concept,

为了覆盖Scala中的字段,必须牢记一些规则,以避免在实施此概念时出现错误,

  1. The use of override keyword is necessary to implement the concept field overriding. If you missed the keyword an error will be prompted and execution of the code will be stopped.

    要实现概念字段重写,必须使用override关键字 。 如果您错过了关键字,将提示错误并停止执行代码。

  2. Only fields that are defined using the val keyboard for defining fields in both parent class and child class can be overridden.

    只能覆盖使用val键盘定义的用于定义父类和子类中字段的字段。

  3. Fields that are defined using the var keyword cannot be overridden as they are editable ( that is both read and write operations are allowed on the variable) anywhere in the class.

    使用var关键字定义的字段不能被覆盖,因为它们在类中的任何位置都是可编辑的(即,允许对变量进行读写操作)。

Here are some programs that show the implementation of field overriding and Scala.

这里有一些程序显示了字段覆盖和Scala的实现

1) Python program without override keyword

1)没有替代关键字的Python程序

This program will show what will be the output if override keyword is not used?

如果不使用override关键字,该程序将显示什么输出?

class animal{  
    val voice = "animal's voice" 
}  

class dog extends animal{  
   val voice = "Dog bark's...!"  
    def says(){  
        println(voice)  
    }  
}  

class cat extends animal{
    val voice = "cat meow's...!";
    def says(){  
        println(voice)  
    } 
}

object MainObject{  
    def main(args:Array[String]){  
        var ani = new dog()  
        ani.says()  
    }  
} 

Output

输出量

error: overriding value voice in class animal of type String;
 value voice needs `override' modifier
   val voice = "Dog bark's...!"
       ^
error: overriding value voice in class animal of type String;
 value voice needs `override' modifier
    val voice = "cat meow's...!";
        ^
two errors found

2) Python program with override keyword

2)具有override关键字的Python程序

This program will show what will be the output if override keyword is used?

该程序将显示如果使用override关键字将输出什么?

class animal{  
    val voice = "animal's voice"
}  

class dog extends animal{  
   override val voice = "Dog bark's...!"  
    def says(){  
        println(voice)  
    }  
}  

class cat extends animal{
    override val voice = "Cat meow's...!";
    def says(){  
        println(voice)  
    } 
}

object MainObject{  
    def main(args:Array[String]){  
        var ani = new dog()  
        ani.says()  
        var ani2 = new cat()
        ani2.says()
    }  
} 

Output

输出量

Dog bark's...!
Cat meow's...!

3) Python program with var keyword

3)使用var关键字的Python程序

This program will show what will be the output if var keyword is used?

该程序将显示如果使用var关键字将输出什么?

class animal{  
    var voice = "animal's voice"
}  

class dog extends animal{  
   override var voice = "Dog bark's...!"  
    def says(){  
        println(voice)  
    }  
}

class cat extends animal{
    override var voice = "Cat meow's...!";
    def says(){  
        println(voice)  
    } 
}

object MainObject{  
    def main(args:Array[String]){  
        var ani = new dog()  
        ani.says()  
        var ani2 = new cat()
        ani2.says()
    }  
}  

Output

输出量

error: overriding variable voice in class animal of type String;
 variable voice cannot override a mutable variable
   override var voice = "Dog bark's...!"
                ^
error: overriding variable voice in class animal of type String;
 variable voice cannot override a mutable variable
    override var voice = "Cat meow's...!";
                 ^
two errors found


翻译自: https://www.includehelp.com/scala/field-overriding-in-scala.aspx

scala 字段覆盖

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Scala语言可以使用MongoDB的官方驱动程序MongoDB Scala Driver来接收MongoDB的document。在接收document时,可以使用BSON库来处理ObjectId类型的字段“_id”。具体操作可以参考MongoDB Scala Driver的官方文档。 ### 回答2: 在Scala接收MongoDB的文档可以使用MongoDB驱动程序来实现。在处理MongoDB文档时,我们可以利用Scala的强大类型系统和模式匹配的功能来处理不同类型的字段。在本例,假设我们已经连接到了MongoDB数据库,并且已经获取到了一个名为"collection"的集合。 首先,我们需要导入MongoDB驱动程序的相关库: ```scala import org.mongodb.scala.bson.ObjectId import org.mongodb.scala.{Document, MongoCollection} ``` 然后,我们可以定义一个case class来表示MongoDB的文档: ```scala case class MyDocument(_id: ObjectId, field1: String, field2: Int) ``` 接下来,我们可以通过使用MongoDB的find方法来获取文档,然后通过map操作将文档转换为我们定义的case class: ```scala val documents: Observable[MyDocument] = collection.find().map { doc => MyDocument( doc.getObjectId("_id"), doc.getString("field1"), doc.getInteger("field2") ) } ``` 在上面的代码,我们使用getObjectId、getString和getInteger方法来获取不同类型的字段值,并将其赋值给我们定义的case class的字段。 最后,我们可以遍历获取到的文档并进行处理: ```scala documents.subscribe { doc => // 处理每个文档 println(doc) } ``` 上述代码将打印出每个文档的内容。 总结一下,以上是使用Scala语言接收MongoDB带有ObjectId类型的文档的方法。我们可以使用MongoDB驱动程序的功能来获取不同类型的字段,并将其转换为Scala的case class来表示文档。 ### 回答3: 在使用Scala语言接收MongoDB的document时,可以使用mongo-scala-driver提供的API进行操作。对于字段“_id”是ObjectId类型的情况,我们可以使用BSON库的ObjectId类来处理。 首先,需要引入mongo-scala-driver和bson库的依赖,可以在build.sbt或者pom.xml文件添加以下内容: ``` libraryDependencies += "org.mongodb.scala" %% "mongo-scala-driver" % "2.9.0" libraryDependencies += "org.mongodb.scala" %% "mongo-scala-bson" % "2.9.0" ``` 接下来,我们可以通过MongoClient连接MongoDB数据库,然后选择要操作的数据库和集合: ```scala import org.mongodb.scala._ import org.mongodb.scala.bson.ObjectId val client: MongoClient = MongoClient() val database: MongoDatabase = client.getDatabase("your_db_name") val collection: MongoCollection[Document] = database.getCollection("your_collection_name") ``` 在获取到数据时,可以通过遍历集合的方式获取每个document。对于ObjectId类型的字段,可以使用ObjectId类进行处理: ```scala collection.find().foreach { document => val id = document("_id").asObjectId().getValue.toString // 其他字段的处理... } ``` 在上述代码,我们通过document("_id")获取到"_id"字段的值,然后使用asObjectId()将其转换为ObjectId类型。然后我们可以使用getValue方法获取ObjectId的具体值,并将其转换为字符串进行后续操作。 需要注意的是,如果字段"_id"不存在或者不是ObjectId类型,上述代码可能会出现异常。因此,建议在实际应用增加错误处理机制,以确保代码的健壮性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值