Scala中的类型转换

Scala中的类型转换

类型转换基本上是一种类型到另一种类型的转换。在Scala这样的动态编程语言中,经常需要将类型转换为另一种类型。Scala中的类型转换是通过 asInstanceOf[] 方法完成的。

asInstanceof方法的应用

  • 在从应用程序上下文文件中体现Bean时需要这个视角。
  • 它也被用来铸造数字类型。
  • 它甚至可以应用在复杂的代码中,比如与Java通信并向其发送一个Object实例的数组。

语法:
obj1 = obj.asInstanceOf[class];

其中,
obj1是返回obj的投射实例的对象,
obj是要投射的对象,
class是obj要投射到的类的名称。

在这里,只有一个扩展(子)类的对象可以被投射为其父类的对象,反之则不行。如果类A扩展了类B,那么A的对象可以被铸成类B的对象,而B的对象不能被铸成类A的对象。在运行时,如果提供的值/对象与指定的类型或类别不兼容,就会抛出一个异常。

例子

// Scala program of type casting
object GFG
{
    // Function to display name, value and
    // class-name of a variable
    def display[A](y:String, x:A)
    {
        println(y + " = " + x + " is of type " +
                            x.getClass.getName);
    }
     
    // Main method
    def main(args: Array[String])
    {
        var i:Int = 40;
        var f:Float = 6.0F;
        var d:Double = 85.2;
        var c:Char = 'c';
         
        display("i", i);
        display("f", f);
        display("d", d);
        display("c", c);
         
        var i1 = i.asInstanceOf[Char]; //Casting
        var f1 = f.asInstanceOf[Double]; //Casting
        var d1 = d.asInstanceOf[Float]; //Casting
        var c1 = c.asInstanceOf[Int]; //Casting
 
        display("i1", i1);
        display("f1", f1);
        display("d1", d1);
        display("c1", c1);
    }
}

输出

i = 40 is of type java.lang.Integer
f = 6.0 is of type java.lang.Float
d = 85.2 is of type java.lang.Double
c = c is of type java.lang.Character
i1 = ( is of type java.lang.Character
f1 = 6.0 is of type java.lang.Double
d1 = 85.2 is of type java.lang.Float
c1 = 99 is of type java.lang.Integer

例子

// Scala program of type casting
 
// The parent class
class Parent
{
    var i: Int = 10;
    var j: Int = 5;
     
    // Function to display i and j values
    def display()
    {
        println("Value of i : " + i +
                "\nValue of j : " + j);
    }
}
 
// The child class
class Child extends Parent
{
    // Used to change i and j values
    def change()
    {
        i = 6;
        j = 12;
        println("Values Changed");
    }
}
 
// Creating object
object GFG
{
    // Main method
    def main(args: Array[String])
    {
        var c:Child = new Child();
        c.display();
        c.change();
         
        // Casting
        var p:Parent = c.asInstanceOf[Parent];
        p.display();
         
        /* p.change(); This will have raised an error
        as p is seen as an object of class Parent and
        Parent does not contain change() */
    }
}

输出

Value of i : 10
Value of j : 5
Values Changed
Value of i : 6
Value of j : 12

在上面的例子中, p. change(); 将被添加,会发生以下错误。

error:value change is not a member of parent.

例子

// Scala program of type casting
 
class Parent
{
    // Member variables and functions.    
}
 
class Child extends Parent
{
    // Member variables and functions.    
}
 
class Unrelated
{
    // Member variables and functions.    
}
 
// Creating object
object GFG
{
    // Main method
    def main(args: Array[String])
    {
        var p:Parent = new Parent();
        try
        {
            p.asInstanceOf[Unrelated];
        }
        catch
        {
            // Used to print the thrown exception.
            case e: Exception => e.printStackTrace();
                        print(e);
        }
        try
        {
            p.asInstanceOf[Child];
        }
        catch
        {
            // Used to print the thrown exception.
            case e1: Exception => e1.printStackTrace();
                        print(e1);
        }
    }
}

输出

java.lang.ClassCastException: Parent cannot be cast to Unrelated
java.lang.ClassCastException: Parent cannot be cast to Child

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程