C # advanced tutorial (4) attributes in C #

Source: Internet
Author: User

C # attributes

Hello everyone, I am SUNWEN from Wuhan Hashi. good afternoon, it's hard to have time to go out and go to Guanyuan Temple. ten Yuan for tickets, not expensive. SUNWEN took a lot of pictures and it would take a long time for everyone to take a look.

Let's start with the question. this time I want to talk about the attributes in C. what is an attribute? For example, I am a male and a male is my one-person attribute. I am a freshman, and freshman is also a property of mine. attribute is the nature of an object. it's easy! Next, I will give two examples. The first example shows how to declare an attribute that can be modified, and the other example creates an Abstract attribute ), it also explains how to discard it in the subclass. okay. Let's get started.

Example 1:

000: // Propertiesperson. cs
001: using System;
002: class Person
003 :{
004: private string myName = "N/";
005: private int myAge = 0;
006:
007: // declare a struct attribute Name
008: public string Name
009 :{
010: get
011 :{
012: return myName;
013 :}
014: set
015 :{
016: myName = value;
017 :}
018 :}
019:
020: // declare an int-type Age attribute
021: public int Age
022 :{
023: get
024 :{
025: return myAge;
026 :}
027: set
028 :{
029: myAge = value;
030 :}
031 :}
032:
033: public override string ToString ()
034 :{
035: return "Name =" + Name + ", Age =" + Age;
036 :}
037:
038: public static void Main ()
039 :{
040: Console. WriteLine ("Simple Properties ");
041:
042: // create a Person instance
043: Person person = new Person ();
044:
045:File: //Print its attributes
046: Console. WriteLine ("Person details-{0}", person );
047:
048: // set attributes
049: person. Name = "Joe ";
050: person. Age = 99;
051: Console. WriteLine ("Person details-{0}", person );
052:
053: // increase the age
054: person. Age + = 1;
055: Console. WriteLine ("Person details-{0}", person );
056 :}
057 :}
The output in this example is:

Simple Properties
Person details-Name = N/A, Age = 0
Person details-Name = Joe, Age = 99
Person details-Name = Joe, Age = 100.

Okay, I got up again. I wrote this yesterday and went to bed. Haha. It's the second day of May Day. Let's see how many articles I can write today and I wrote two articles yesterday.

From the above program, we can see that the attribute settings of a class are different from those in JAVA by using the VB concept (this is M $, TMD !) Some may wonder why we can use the Console. writeLine () to print an object person. in fact, the principle is very simple. Like in JAVA, when the regulator uses a printing method, this object automatically calls its ToString () (in JAVA, It is toString, TMD, I almost made a mistake again !) Method. In row 33, we can see the shadow of this method. The override keyword is probably the method that overwrites the parent class. Isn't this redundant? We can see that the attribute settings of an object are completed through a combination of get () and set (). Of course, there is also a value. in addition, you can also control the read/write permissions of an attribute, but simply remove get () and set (), for example, the attribute you do not want to write, don't set (). If you don't want to read it, don't get. I have to feel that C # is not flexible in JAVA at this point (it's over, and it's going to be overwhelmed !).

Example 2:

This example shows how to create Abstract attributes. What are Abstract attributes? What are the so-called Abstract attributes! FT) an abstract class does not provide an access program for execution properties, and can be ignored in the subclass. the following example contains three files. You must compile them separately to obtain the results. They are:

Abstractshape. cs: Shape class, which contains an abstract attribute of Area
Shapes. cs: subclass of Shape
Shapetest. cs: display program.
To compile these programs, run csc abstractshape. cs shapes. cs shapetest.cs. after the job is finished, the executable program shapetest.exe will be created.

000: // Propertiesabstractshape. cs
001: using System;
002:
003: public abstract class Shape
004 :{
005: private string myId;
006:
007: public Shape (string s)
008 :{
009: Id = s; // This statement calls the set builder of the Id attribute.
010 :}
011:
012: public string Id
013 :{
014: get
015 :{
016: return myId;
017 :}
018:
019: set
020 :{
021: myId = value;
022 :}
023 :}
024:
025: public abstract double Area
026 :{
027: get;
028 :}
029:
030: public override string ToString ()
031 :{
032: return Id + "Area =" + double. Format (Area, "F ");
033 :}
034 :}
It is actually very simple to look at this program. when the object of this class pair is created, the initialization part is 007-010, and it gives the parameter s of the object to the Id attribute. then we performed the operation in the previous example. in fact, we can compare abstract attributes with interfaces in JAVA. They only name a method and do not provide the content of this method. like the abstract attribute "Area", there is a get, but it does not specify the content in the get method (maybe it cannot be called a method), that is, what to do with get. this is done by its subclass.

File 2: in this file, a class overwrites the Area attribute.

000: // Propertiesshapes. cs
001: public class Square: Shape
002 :{
003: private int mySide;
004:
005: public Square (int side, string id): base (id)
006 :{
007: mySide = side;
008 :}
009:
010: public override double Area
011 :{
012: get
013 :{
014: return mySide * mySide;
015 :}
016 :}
017 :}
018:
019: public class Circle: Shape
020 :{
021: private int myRadius;
022:
023: public Circle (int radius, string id): base (id)
024 :{
025: myRadius = radius;
026 :}
027:
028: public override double Area
029 :{
030: get
031 :{
032: return myRadius * System. Math. PI;
033 :}
034 :}
035 :}
036:
037: public class Rectangle: Shape
038 :{
039: private int myWidth;
040: private int myHeight;
041:
042: public Rectangle (int width, int height, string id): base (id)
043 :{
044: myWidth = width;
045: myHeight = height;
046 :}
047:
048: public override double Area
049 :{
050: get
051 :{
052: return myWidth * myHeight;
053 :}
054 :}
055 :}
This example is a bit confusing: What is it, like inheritance, which is equivalent to extends in JAVA. I think so. Let's take a look.

The third file below is a test file, which is very simple. Let's take a look.

000: // Propertiesshapetest. cs
001: public class TestClass
002 :{
003: public static void Main ()
004 :{
005: Shape [] shapes =
006 :{
007: new Square (5, "Square #1 "),
008: new Circle (3, "Circle #1 "),
009: new Rectangle (4, 5, "Rectangle #1 ")
010 :};
011:
012: System. Console. WriteLine ("Shapes Collection ");
013: foreach (Shape s in shapes)
014 :{
015: System. Console. WriteLine (s );
016 :}
017:
018 :}
019 :}
In this example, the symbol does mean extends, that is, inheritance. what does inheritance mean? To put it bluntly, it means giving birth to a child. for example, in the following sentence sunwenson extends sunwen, this indicates that the sunwenson class inherits sunwen. The sunwenson class has all the things of the sunwen class, and you can also add and delete things in sunwen. this is simple, but this is an important technology in the development of modern software, because it can greatly improve the reusability of software. alas, I am not qualified because only the junior and senior students have said this. haha.

The output of this program is:

Shapes Collection
Square #1 Area = 25.00
Circle #1 Area = 28.27
Rectangle #1 region = 20.00
This section is complete. it is difficult to understand this section, especially for those who have no JAVA or C ++ programming experience. but don't be afraid. If you have the courage to learn it, you will surely gain something. I have to take a break. Hey, I haven't eaten breakfast yet!

Related Article

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.