Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I write the equals (==) function?

class Character {
  
    construct new(name,life){
        _name = name
        _life = life
    }
    
    name  { _name }
    life  { _life }
}

If the name and life are the same, it should return true otherwise false.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class Character {
  
    construct new(name,life){
        _name = name
        _life = life
    }
    
    name  { _name }
    life  { _life }
    
    ==(other){
    return _name == other.name && 
           _life == other.life
    }
}

var player_one = Character.new("Nova",100)
var player_two = Character.new("Mint Hot Chocolate",50)
var player_three = Character.new("Nova",100)

System.print(player_one == player_three) // Output: true
System.print(player_one == player_two) // Output: false
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading