Scala 中的类型参数与 Any [英] Type parameter vs Any in Scala

查看:32
本文介绍了Scala 中的类型参数与 Any的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 RBase 类和一个继承自它的 RInt 类.Base 具有三个接口功能.由于继承的子类可以使用不同类型的值,因此函数参数的类型为 Any.因此,我需要使用 asInstanceOf 来实现子类.这是一个例子.

I have a class RBase, and a RInt class that inherits from it. The Base has three interface functions. As the subclasses that inherit can use different type of values, the function parameter is typed as Any. As a result, I need to use asInstanceOf to implement the subclasses. This is an example.

abstract class RBase(val name:String)
{
    def encode(value:Any) : Array[Byte]
    def decode(byteArray: Array[Byte]) : Any

    def check(value:Any) : Boolean
}

class RInt extends RBase("int")
{
    override def encode(value: Any) = {
        val byteValue = value.asInstanceOf[Int]
        Array[Byte](byteValue.toByte, byteValue.toByte, byteValue.toByte)
    }
    override def decode(byteArray: Array[Byte]) : Any = {
         byteArray.size   
    }
    override def check(value:Any) : Boolean = {
        val byteValue = value.asInstanceOf[Int]
        if (byteValue.toInt > 0) true
        else false
    }
}


object Main extends App {
    val b = new RInt
    println(b.decode(Array[Byte]()))
    println(b.encode(100).mkString(":"))
    println(b.check(-1))
}

// uncomment when compile
Main.main(args)

我认为可以使用类型参数删除 AnyasInstanceOf.这是我第一次尝试.

I think Any and asInstanceOf can be removed using type parameters. This is my first try.

abstract class TBase(val name:String)
{
    def encode[T](value:T) : Array[Byte]
    def decode[T](byteArray: Array[Byte]) : T
    def check[T](value:T) : Boolean
}

class TInt extends TBase("bit")
{
    override def encode[Int](value: Int) = {
        Array[Byte](value.toByte, value.toByte, value.toByte)
    }
    override def decode[Int](byteArray: Array[Byte]) : Int = {
         byteArray.size   
    }
    override def check[Int](value:Int) : Boolean = {
        if (value > 0) true
        else false
    }
}

object Main extends App {
    val b = new TInt
    println(b.decode(Array[Byte]()))
}

// uncomment when compile
Main.main(args)

很遗憾,我收到以下错误消息.

Unfortunately, I have the following error messages.

T.scala:11: error: value toByte is not a member of type parameter Int
        Array[Byte](value.toByte, value.toByte, value.toByte)
                          ^
T.scala:11: error: value toByte is not a member of type parameter Int
        Array[Byte](value.toByte, value.toByte, value.toByte)
                                        ^
T.scala:11: error: value toByte is not a member of type parameter Int
        Array[Byte](value.toByte, value.toByte, value.toByte)
                                                      ^
T.scala:14: error: type mismatch;
 found   : scala.Int
 required: Int(in method decode)
         byteArray.size
                   ^
T.scala:17: error: value > is not a member of type parameter Int
        if (value > 0) true
                  ^
5 errors found

  • Q1:如何改进使用 Any 和 asInstanceOf 的 RBase 方法?
  • Q2:TBase 有什么问题?
  • 推荐答案

    您的想法非常接近.参数化整个类而不是单个方法.

    You were quite close with your idea. Parameterize the whole class instead of individual methods.

    abstract class TBase[T](val name: String) {
      def encode(value: T): Array[Byte]
      def decode(byteArray: Array[Byte]): T
      def check(value: T): Boolean
    }
    
    class TInt extends TBase[Int]("bit") {
      override def encode(value: Int) = {
        Array[Byte](value.toByte, value.toByte, value.toByte)
      }
      override def decode(byteArray: Array[Byte]): Int = {
        byteArray.size
      }
      override def check(value: Int): Boolean = {
        if (value > 0) true
        else false
      }
    }
    

    回答为什么您的代码不起作用 - 因为您使用名为Int"的类型参数化了该方法.编译器认为它只是类型的名称(可以是 T、U、Int、Whatever).因此,在这种情况下,返回类型不是真正的 Int",而是您的虚构类型.

    To answer why your code doesn't work - because you parameterized the method with a type called "Int". Compiler thinks it's just a name for the type (it can be T, U, Int, Whatever). So the return type is not a "real Int" in that case, but your made-up type.

    这篇关于Scala 中的类型参数与 Any的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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