6 ms·
The concept of getters & setters exist in languages where it's not conventional to write them as methods named getX() or setX(y). Writing the that example in C
by systoll 9y ago
The concept of getters & setters exist in languages where it's not conventional to write them as methods named getX() or setX(y).
Writing the that example in C#, you'd likely have
public double Kelvins {get; set;};
public double Celsius {
get { ... };
set { ... };
}
and [modern] Javascript could have
let kelvins;
get celsius() { ... };
set celsius(c) { ... };
And you'd set both of them with [eg] s.celsius = 5. This is how getters/setter look when a language supports them.
Writing and using methods named 'getX' and 'setX' is conventional in Java because support for this feature doesn't exist at the language level, but is essentially implemented by the common IDEs. Probably not the ideal situation, but that's where Java is at.