Java – what is the motivation for attributes?
I'm a little confused about why language has these I'm a java programmer at the beginning of my career, so Java is the only language I write because I actually, you know, get it
So we certainly don't have properties in Java. We write getthis () and setthat (...) methods
What do we get from owning property?
thank you.
Edit: another question: what naming conventions appear in languages with attributes?
Solution
Which one looks more natural?
// A person.setAge(25) // B person.age = 25; // or person.Age = 25; //depending on conventions,but that's beside the point
Most people will answer B
This is not only grammar sugar, but also helpful in reflection; In fact, you can distinguish between data and operations without relying on method names
The following are examples of unfamiliar properties in c#:
class Person { public int Age { set { if(value<0) throw new ArgumentOutOfRangeException(); OnChanged(); age = value; } get { return age; } } private int age; protected virtual void OnChanged() { // ... } }
In addition, most people always use attributes instead of promoting public members later, for the same reason that we always use get / set; There is no need to rewrite old client code bound to data members