Here is an example of C # attributes.
In Java, This is generally the case.
Public Class Person
{
Private String _ name;
Public Void Setname (string name)
{
_ Name=Name. Upper ();
}
Public String getname ()
{
Return_ Name. Upper ();
}
}
Then, you need:
Person _ p = New Person ();
_ P. setname ( " Henkoo " ); // Setter
String _ pname = _ P. getname (); // Getter
As mentioned in refactoring Based on Java, there are two completely different ideas on the "value access method, in the class where the variable definition is located, the variable should be accessed freely (directly). In the other group's opinion, even in this class, only access functions should be used for indirect access. The debate between the two schools is in full swing.
Among them, the advantage of "indirect access to variables" is that subclass can change the way to obtain data through "overwriting a function", and more supports flexible data management methods. The benefit of "directly accessing variables" is that,CodeEasy to read. When reading the code, you don't need to stop and say, "Ah, this is just a value function ".
Then, refactoring provides the self encapsulate field method to change the direct access to the field to indirect access through the access function.
====================
In C #, you can.
Public Class Person
{
Private String _ Name;
Public String Name
{
Set
{
_ Name=Value. toupper ();
}
Get
{
Return_ Name. toupper ();
}
}
}
The following settings and settings are available:
Person _ p = New Person ();
_ P. Name = " Henkoo " ; // Setter
String _ pname = _ P. Name; // Getter
In fact, the change is not significant. The name here is a prototype ). The same access method as field, the same processing method as getter and setter access functions, and also supports Virtual, inheritance, and polymorphism. This is the so-called "Public consistent interface, and hidden get and set computing details ".
From this point of view, does the debate in Java seem redundant :)