Sunwen Tutorial----C # Advanced (iv)

Source: Internet
Author: User
Hello everybody, I am the Sunwen of Wuhan Chinese teacher. Hehe, the afternoon finally had time to go out to play, to the Yuan temple. Tickets 10 yuan, not expensive, Sunwen took a lot of like, not long, you can let everyone see.

Talk less, let's get to the point. This time I'm going to talk to you about properties in C #. What is a property, for example, I am a man, and a man is one of my attributes. I am a freshman student, the freshman is also a property of me. The property is the nature of an object. So, let me give you two examples The first example shows how to declare a property that can be modified, another example that creates an abstract attribute (abstract), and shows how to discard it in a subclass. OK, let's get started.

Example one:


From://Properties\person.cs
001:using System;
002:class person
003: {
004:private string myName = "N/a";
005:private int myAge = 0;
006:
007://Declare a character-type property 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 age property of type int
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 an instance of person
043:person person = new person ();
044:
045://Print out its properties
046:console.writeline ("person details – {0}", person);
047:
048://Make some settings on the properties
049:person. Name = "Joe";
050:person. Age = 99;
051:console.writeline ("person details – {0}", person);
052:
053://Increase Age
054:person. Age + = 1;
055:console.writeline ("person details – {0}", person);
056:}
057:}
The output of this example is:

Simple Properties
Person Details-name = n/A, age = 0
Person Details-name = Joe, age = 99
Person Details-name = Joe, age = 100

OK, and get up again, yesterday wrote this, ran to the bed, and so on. Now is the second day of 51, see I can write a few today, wrote two yesterday.

From the above program we can see that a class of property settings, borrowed from the concept of VB, and Java is not the same. (This is m$,tmd!) Some friends may be very strange, why we can use Console.WriteLine () to print an object person. The truth is simple, as in Java, when the regulator uses a printing method, the object automatically calls its ToString () ( In Java is TOSTRING,TMD, and almost made a mistake!) method. In 33 rows, we can see the shadow of this method, override this keyword is probably the way to overwrite the parent class, is it a bit superfluous? We can see that the setting of an object's properties is done through a combination of get () and set (), of course, There is a value of this thing. You can also control the read/write permission for a property, just simply remove the get () and set (), such as the property you don't want to write, and don't set (), if you don't read it, don't get it. Gotta feel, C # There is no Java to be flexible at this point (it's over!).

A second example:

This example shows how to create abstract attributes, what are abstract properties, what is called abstract properties, is .... (Alas, every nonsense is so much!) FT) An abstract class does not provide an execution property accessor, and he can be ignored in subclasses. The following example has three files that you want to compile separately to get the results:

Abstractshape.cs:Shape class containing an area abstract property
Subclass of Shapes.cs:Shape
Shapetest.cs: Displays the program.
To compile these programs, run: csc abstractshape.cs shapes.cs Shapetest.cs on it. After running, it generates Shapetest.exe this executable program.

From://Properties\abstractshape.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; The set builder that invokes 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:}
Looking at this program, it is actually very simple, when the object of this class pair is built, the initialization part is 007-010, it gives the ID this attribute to the parameter s of the object being built. Then the previous example was taken. In fact, we can compare abstract attributes to interfaces in Java (Interface) , they only mention the name of a method and do not provide the contents of this method. Like area, an abstract attribute, there is a get, but it does not specify the contents of the Get method (perhaps it cannot be called a method), that is, what to get to do. This thing is done by its subclasses.

Second file: In this file, a class overrides the (override) Area property.

From://Properties\shapes.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 * 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 gives us some confused: what it is, as if it is inheritance, equivalent to the extends in Java. I think so. Let's have a look first.

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

From://Properties\shapetest.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:}
From this example, it is true that this symbol is a extends meaning, that is, inheritance. What is the meaning of inheritance? For example, the following sentence Sunwenson extends Sunwen, this means Sunwenson this class inherited Sunwen, Sunwenson This class has everything in the Sunwen class, and you can add and remove something from the Sunwen. It's that simple, but this is an important technology for modern software development, because it can make the reuse of software greatly improved. Alas, these are the only seniors who say, I will not be qualified. hehe.

The output of this program is:

Shapes Collection
Square #1 area = 25.00
Circle #1 area = 28.27
Rectangle #1 area = 20.00
It's over. To understand this section, there are some difficulties, especially for those who do not have Java or C + + programming experience of friends. But don't be afraid, take the courage to learn, there will be some gains. I also want to rest, hey, breakfast has not eaten!

The above is the Sunwen tutorial----C # Advanced (iv) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.