The----of the Sunwen Tutorial C # advanced 4

Source: Internet
Author: User
Tags abstract inheritance
Tutorial Sunwen Tutorial----C # Advanced
Four
Mrfat@china.com
Everybody is good, I am Wuhan Hua Division's Sunwen. Oh, the afternoon finally have time to go out to play, to the Yuan temple. Tickets 10 yuan, not expensive, Sunwen took a lot of like, not too long, you can let everyone have a look.

Less gossip, let's get to the point. This time I'm going to talk to you about the attributes in C #. What is the attribute, for example, I am a man, and a man is one of my attributes. I am a freshman, freshman is also one of my attributes. A property is a property of an object. It's very simple, hehe! Below, I give two examples, The first example shows how to declare a property that can be modified, another example creates an abstract attribute (abstract), and shows how to discard it in a subclass. OK, let's get started here.

Example one:


M://Properties\person.cs
001:using System;
002:class person
003: {
004:private string myname = "N/a";
005:private int myage = 0;
006:
007://Declare attribute name of a character type
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 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 property
049:person. Name = "Joe";
050:person. Age = 99;
051:console.writeline ("person details-{0}", person);
052:
053://Increase in 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

Well, get up again, yesterday wrote to this, went to bed to sleep, hehe. 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 the properties of the set, borrowed 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 very simple, as in Java, when the adjuster uses a printing method, the object automatically calls its ToString () ( In Java is TOSTRING,TMD, and almost make a mistake! 33 lines, we can see the shadow of this method, override this keyword is probably the way to cover the parent class, this is not a bit redundant? We can see that the setting of an object's properties is done through a combination of get () and set (), of course, There is also a value of this dongdong. To add, you can also control the read/write access to a property, just take the Get () and set () simply to remove it, such as you do not write the attribute, do not set () on it, if you do not read, do not get (). Gotta feel that C # In this point there is no Java to the flexible (finished, but also be covered by the crazy hit!).

A second example:

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

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

M://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; This calls the Set builder for 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:}
Look at this program, it's actually very simple, when the object of this class pair is built, the initialization part is 007-010, which gives the attribute of the ID to the parameter s of the object. And then we do the previous example. In fact, we can compare abstract attributes with interfaces in Java (Interface) , they only lift the name of a method and do not provide the content of this method. Like the abstract property of area, 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 do. This thing, by its subclasses to do.

Second file: In this file, a class overrides (override) the area attribute.

M://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 is some let us confused,: is what, as if the inheritance, the equivalent of extends in Java. I think so. Let's take a look at it first.

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

M://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:}
In this case, the symbol is extends, which means inheritance. What does inheritance mean, plainly, is the meaning of having a child. For example, the following sentence Sunwenson extends Sunwen, this means Sunwenson this class inherits Sunwen, Sunwenson This class has everything in the Sunwen class, and can add and remove something from the Sunwen. It's that simple, but it's an important technology in the development of modern software, because it can greatly improve the reusability of the software. Alas, these are only three senior seniors who said, I will not be eligible, 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. But do not be afraid, courage to learn, there will be some gains. I also want to have a rest, Hey, breakfast has not eaten it!


Next page


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.