Interface and details

Source: Internet
Author: User
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 :)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.