What is the point of Getters and Setters? What do they do? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the point of Getters and Setters? What do they do?

17th Aug 2016, 6:15 PM
Ilana
2 Answers
+ 1
Getters and Setters a common functions / methods to get or set values of class members or fields in a controllable way. Examples: -- class Person { // field of class Person which is called age var age:Int = 0 // getter for age public func getAge() -> Int { return self.age } // setter for age (only values greater zero are accepted) public func setAge(age:Int) { if age > 0 { self.age = age } } }
17th Aug 2016, 10:45 PM
Jaschar Domann
Jaschar Domann - avatar
0
when you use those func you can do something to the value. as the setAge function you could check age illegible or not.
20th Aug 2016, 4:22 AM
叶砚
叶砚 - avatar