📜  斯卡拉 |有状态的对象

📅  最后修改于: 2022-05-13 01:54:28.024000             🧑  作者: Mango

斯卡拉 |有状态的对象

有状态的对象是具有可变成员或可变成员的对象,它们可能会改变之前在对象上执行的事务或操作。对于多次执行相同的操作,输出的结果可能与之前的结果不同。将有状态对象与现实世界对象进行比较是很常见的,其中对象的状态会随着时间而变化。

句法:

class classname
{
     // declaring some states that are mutable
     var state1
     var state2
     def changestate
     {
          // some operation to change the states of the object
     }
}

当对象的成员更改其值时,超时执行某些操作,这些操作是在对象上执行的。因此,对象的状态会根据先前执行的操作而改变。

下面是一些理解有状态对象的例子。
例子#1:

// A Scala program to illustrate 
// stateful objects
  
// creating a class
class waterbottle
{
    // creating states
    var water: Int = 0
    def drinkwater = { 
        if(water > 0)
        {
            water = water-1
            println("water left = "+water)
        }
        else
        {
            println("waterbottle empty fill water")
        }
    }
      
    // Defining method
    def fillwater (c: Int)=
    {
        if(water + c > 5)
        {
            water = 5
        }
        else
        { 
            water = water + c
        }
    }
    override def toString= "water in bottle = " + water
}
  
// Creating object
object GFG 
{
    // Main method 
    def main(args: Array[String]) 
    { 
        // waterbottle object
        var w = new waterbottle
          
        // calling 
        w.fillwater(3)
        println(w)
          
        // Changing state
        w.drinkwater
        w.drinkwater
        w.drinkwater
        w.drinkwater
    } 
}

输出 :

water in bottle = 3
water left = 2
water left = 1
water left = 0
waterbottle empty fill water

正如我们在上面的示例中看到的w.drinkwater的状态将其相同操作的结果更改为“waterbottle empty fill water” ,直到我们填充或更新水。可变状态取决于作为变量的水。因此我们可以说有状态的对象是由vars而不是val组成的,但这不是真的,一个类可以在不包含任何 vars 的情况下改变它的状态。

对于将其状态更改为另一种状态的对象,达到该特定状态的操作集或路径可能不同,但最终达到的状态应该是相同的。这也称为操作等价,其中x 和 y是不同的对象,但在不同操作集的末尾具有相同的状态。

例子#2:

// A Scala program to illustrate 
// stateful objects
  
// creating player class
class player
{
    // creating states
    var health: Int = 10
    def punch(p: player)
    {
        if(p.health > 0)
        {
            p.health = p.health-2
            println(p + " health is " + p.health)
            if(p.health < 1)
            {
                // checking the state
                dead(p)
            }
        }
        else
        {
            dead(p)
        }
    }
    def kick(p: player)
    {
        if(p.health > 0)
        {
            p.health = p.health-3
            println(p + " health is " + p.health)
            if(p.health < 1)
            {
                // checking the state
                dead(p)
            }
        }
        else
        {
            dead(p)
        }
    }
    def superp(p: player)
    {
        if(p.health > 0)
        {
            p.health = p.health - 5
            println(p + " health is " + p.health)
            if(p.health < 1)
            {
                // checking the state
                dead(p)
            }
        }
        else
        {
            dead(p)
        }
    }
    def dead(p:player)
    {
        println("Game Over")
        println(p +" is dead " + this + " is winner")
    }
}
  
// Creating object
object GFG
{
    // Main method 
    def main(args: Array[String]) 
    { 
        // Creating objects for player
        var p1 = new player
        var p2 = new player
        p1.kick(p2)
        p1.punch(p2)
        p1.superp(p2)
        p1.punch(p2)
  
    } 
}

输出 :

player@506e1b77 health is 7
player@506e1b77 health is 5
player@506e1b77 health is 0
Game Over
player@506e1b77 is dead player@4fca772d is winner
Game Over
player@506e1b77 is dead player@4fca772d is winner

同样在上面的例子中,我们可以知道 player 是一个有状态的对象,而无需查看类的内部工作。因为生命值不能降低到负值,所以玩家具有可变状态,因为相同的操作在不同或相同的输入处返回不同的输出。