C # inheritance

Source: Internet
Author: User
For example:
There is a base class rectangleex 1 Class Rectangleex
2 {
3 Private   Int _ X, _ y, _ w, _ H;
4
5 Public   Int X
6 {
7 Get { Return _ X ;}
8 Set {_ X = Value ;}
9 }
10 Public   Int Y
11 {
12 Get { Return _ Y ;}
13 Set {_ Y = Value ;}
14 }
15 Public   Int W
16 {
17 Get { Return _ W ;}
18 Set {_ W = Value ;}
19 }
20 Public   Int H { Get { Return _ H ;} Set {_ H = Value ;}}
21
22 Protected Pen _ pen;
23
24 Public Rectangleex ( Int X, Int Y, Int W, Int H)
25 {
26 _ X = X;
27 _ Y = Y;
28 _ W = W;
29 _ H = H;
30 _ Pen =   New Pen (color. Black );
31 }
32
33 //
34 Public   Virtual   Void Draw (Graphics g)
35 {
36 G. drawrectangle (_ pen, _ x, _ y, _ w, _ H );
37 }
38
39 }

You can use: 1
2 Graphics g = E. graphics;
3 Pen P =   New Pen (color. Blue );
6
7 // Use rectangleex class instance;
8 Rectangleex Rex =   New Rectangleex ( 15 , 15 , 30 , 30 );
9 Rex. Draw (g );
10 .

Now you need to draw a square, which can be like this: 1 Class Square: rectangleex
2 {
3 Public Square ( Int X, Int Y, Int W)
4 : Base (X, y, W, W)
5 {
6 }
7 }

The above base is the same usage as the initialization list of c ++. when calling the base class method in C #, you can use the base keyword

Usage: 1
2 // Use Square
3 Square SQ =   New Square ( 20 , 20 , 20 );
4 Sq. Draw (g ); // The above griphics object from painteventargs e is used.
5

In this case, if I want to have a new derived class and draw two rectangles at the same time, I need to transform the draw () method (I have two methods)
First: I can change the draw () method of the base class to virtual, and then the derived class override it;
Second: I can also use the new keyword to hide methods with the same name in the base class.
The second method is as follows: 1 Class Dblrectangleex: rectangleex
2 {
3 Public Dblrectangleex ( Int X, Int Y, Int W, Int H ): Base (X, y, W, h ){
4 _ Pen =   New Pen (color. Red );
5
6 }
7
8 Private Pen _ rpen =   New Pen (color. seagreen );
9
10
11 Public   New   Void Draw (Graphics g) // Use the New Keyword to overwrite the method of the same name in the base class.
12 {
13 G. drawrectangle (_ pen, X, Y, W, H );
14 G. drawrectangle (_ rpen, X + 5 , Y + 5 , W, H );
15 }
16
17 /*
18 Public override void draw (Graphics g) // if the method is the same (parameters and return values), only override can be used (Virtual keywords are required for base class Declaration)
19 {
20 G. drawrectangle (_ pen, X, Y, W, H );
21 G. drawrectangle (_ rpen, x + 5, Y + 5, W, H );
22 } */
23
24
25 }

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.