Step 1 for object-oriented programming: package
Willsound
introduction
For a programmer with rich structured programming experience, object-oriented programming may bring them very unnatural feelings. The package is the first step in achieving object-oriented programming, and the package is a collection of data or functions in one unit (we call it). The encapsulated objects are often referred to as an abstract data type. In this article, we will learn more about properties.
Significance of package
The meaning of the package is to protect or prevent code (data) is unintentionally destroyed. Data in object-oriented programming is seen as a center-oriented race, and it is very close to use its function to protect it from other modifications to other functions.
The package provides an effective way to protect the data from unexpected damage. Compared to our data (public) defined as a public (PRIVAT), we will define data (PUBLIC), which is defined as a private (privat) will be better in many ways. Private data can be indirectly controlled in two ways. Let's take some C # example to learn the two methods to encapsulate data. The first method, we use traditional deposits and take the method. Second method We use properties. Regardless of which we use, our goal is to use data while not making it any damage and changing.
Package with traditional reading, write method
Let us look at an example has a class department, in order to manipulate data in this class (String DepartName we define a reading method and a write method.
using system; public class Department {private string departname; ....... // read method public string GetDepartname () {return departname;} // write method public void SetDepartname (string a) {departname = a;}}
With the above method, we can protect private data from external programs. Now we use two different ways to write and read data
Public static int main (String [] args) {department D = new department (); D.SetDepartName ("Electronics"); console.writeline ("The Department is:" D.GetDepartName ()); return 0;}
In the above example, we cannot directly access the private data in instance D of class departments, and we can only access these two ways.
Implement package with attributes
Attributes are a language component introduced by C #, with only a few language support attributes. Protect the domain in the class by reading and writing attributes. The first method is also a good way, but the package will be more convenient to achieve the package.
Now let's take a look at an example:
using system; public class Department {private string departname; public string Departname {get {return departname;} set {departname = value;}}} public class Departmentmain {public static int Main (string [] args) {Department d = new Department (); d.DepartName = "Communication"; console.writeline ("The Department is: {0}", D.DepartName); return 0;}}
Through the above example, we can see how to achieve a package through properties. Attributes have two operations GET and SET. GET is used to return the value of the property domain. SET assigns the value domain by Value variable. The attribute can be set to read-only. This only needs only one set operation. Read-only properties
using system; public class ReadDepartment {private string departname; public ReadDepartment (string avalue) {departname = avalue;} public string Departname {get {return departname;}}} public class ReadDepartmain {public static int Main (string [] args) { ReadDepartment D = New ReadDepartment ("Computerscience"); Console.writeline ("The Department IS: {0}", D.DepartName); return 0;}}
In the above example we saw how to implement a read-only attribute. Class readDepartment has a departname property that only GET operations are implemented. It omits the write operation. This special class has a constructor to accept a string variable. The main method in class readDepartmain creates a new object D. An instance of the log D is used with a constructor with a string parameter with a class readDepartment. Since the above attribute is read-only, we do not assign a value to the domain departname and let us only read the value in this domain. Of course, the attribute can also be written (Write-only), which only has only one GET operation.
Only write attribute
using system; public class WriteDepartment {private string departname; public string Departname {set {departname = value; Console.WriteLine ( "The Department is: {0}", departname);}}} public class WriteDepartmain {public static int Main ( String [] args) {WriteDepartment D = new writeDepartment (); d.DepartName = "computerscience"; return 0;}}
In the above example we saw how to implement a only write attribute. Class WriteDepartment has a DepartName property that only sets the SET operation. It omits the read operation.
to sum up
The package is the first step towards the object-oriented programming. This article shows you some packaging knowledge. With traditional read, write two methods can achieve packaging, and another way to achieve packages is to use attributes. The advantage of using properties is that the user's user can use a statement to operate the internal data.