How to Understand override and new in C # Polymorphism

Source: Internet
Author: User

I have read many blogs and articles on how to understand override and new in C # and learned a lot about inheritance and polymorphism. However, the understanding layer is not deep enough and does not fall into the essence.

However, objects and inheritance and polymorphism are often compared with the real world. They are derived from the real world, and higher. We cannot start with the real world and compare its implementation mechanism. What should we do in a familiar way?

See the following code:


[Csharp]
01. namespace ClassLibrary1
02 .{
03. public class Teacher
04 .{
05. public virtual void Paper ()
06 .{
07. Console. Write ("the instructor gives the exam ");
08 .}
09 .}
10. public class S1: Teacher
11 .{
12. public override void Paper ()
13 .{
14. Console. WriteLine ("S1 exam ");
15 .}
16 .}
17. public class S2: S1
18 .{
19. public override void Paper ()
20 .{
21. Console. WriteLine ("S2 rewriting S1 exam ");
22 .}
23 .}
24. public class S3: S2
25 .{
26. public new void Paper ()
27 .{
28. Console. WriteLine ("S3 did not overwrite the S2 exam, but re-added the exam ");
29 .}
30 .}
31 .}
32.
33. namespace ConsoleApplication1
34 .{
35. class Program
36 .{
37. static void Main (string [] args)
38 .{
39.
40. Teacher t = new Teacher ();
41. t. Paper (); // The instructor gives the exam
42. S1 s1 = new S1 ();
43. s1.Paper (); // S1
44. S2 s2 = new S2 ();
45. s2.Paper (); // S2 overwrites the S1 exam
46. S3 s3 = new S3 ();
47. s3.Paper (); // S3 did not overwrite the S2 exam, but instead re-added the exam.
48.
49. t = s3;
50. t. Paper ();//!! S2 overwrites the S1 exam
51.
52. t = s2;
53. t. Paper (); // S2 overwrites the S1 exam
54.
55. t = s1;
56. t. Paper (); // S1 exam
57.
58.
59 .}
60 .}
61 .}
Namespace ClassLibrary1
{
Public class Teacher
{
Public virtual void Paper ()
{
Console. Write ("the instructor gives the exam ");
}
}
Public class S1: Teacher
{
Public override void Paper ()
{
Console. WriteLine ("S1 exam ");
}
}
Public class S2: S1
{
Public override void Paper ()
{
Console. WriteLine ("S2 rewriting S1 exam ");
}
}
Public class S3: S2
{
Public new void Paper ()
{
Console. WriteLine ("S3 did not overwrite the S2 exam, but re-added the exam ");
}
}
}

Namespace ConsoleApplication1
{
Class Program
{
Static void Main (string [] args)
{

Teacher t = new Teacher ();
T. Paper (); // The instructor gives the exam
S1 s1 = new S1 ();
S1.Paper (); // S1 exam
S2 s2 = new S2 ();
S2.Paper (); // S2 overwrites the S1 exam
S3 s3 = new S3 ();
S3.Paper (); // S3 did not overwrite the S2 exam, but instead re-added the exam.

T = s3;
T. Paper ();//!! S2 overwrites the S1 exam

T = s2;
T. Paper (); // S2 overwrites the S1 exam

T = s1;
T. Paper (); // S1 exam


}
}
}

 

Analysis: Why does t = s3 and t. Paper () show that S2 overwrites the S1 exam? Because the reference t is of the Teacher class, we first find the Paper () method from the Teacher class, and then find the Paper () method of S1 (), then I found that it was rewritten again. So I continued and Found Paper () of S2 (). At this point, it is found that Paper () has not been restarted, so it is stopped. So using override will find the farthest method to be rewritten. In layman's terms: the teacher gave an exam, and classmate A did it. Then student B made another modification on the basis of student A... student n made another modification on the basis of n-1. So the final exam must be the result of the n children's shoes. But the new method only re-writes his own exam. Only S3 s3 = new S3 () and s3.Paper () Can you view your own results. You can use your own references to process your own objects. I have read many blogs and articles on how to understand override and new in C # and learned a lot about inheritance and polymorphism. However, the understanding layer is not deep enough and does not fall into the essence.

However, objects and inheritance and polymorphism are often compared with the real world. They are derived from the real world, and higher. We cannot start with the real world and compare its implementation mechanism. What should we do in a familiar way?

See the following code:


[Csharp]
01. namespace ClassLibrary1
02 .{
03. public class Teacher
04 .{
05. public virtual void Paper ()
06 .{
07. Console. Write ("the instructor gives the exam ");
08 .}
09 .}
10. public class S1: Teacher
11 .{
12. public override void Paper ()
13 .{
14. Console. WriteLine ("S1 exam ");
15 .}
16 .}
17. public class S2: S1
18 .{
19. public override void Paper ()
20 .{
21. Console. WriteLine ("S2 rewriting S1 exam ");
22 .}
23 .}
24. public class S3: S2
25 .{
26. public new void Paper ()
27 .{
28. Console. WriteLine ("S3 did not overwrite the S2 exam, but re-added the exam ");
29 .}
30 .}
31 .}
32.
33. namespace ConsoleApplication1
34 .{
35. class Program
36 .{
37. static void Main (string [] args)
38 .{
39.
40. Teacher t = new Teacher ();
41. t. Paper (); // The instructor gives the exam
42. S1 s1 = new S1 ();
43. s1.Paper (); // S1
44. S2 s2 = new S2 ();
45. s2.Paper (); // S2 overwrites the S1 exam
46. S3 s3 = new S3 ();
47. s3.Paper (); // S3 did not overwrite the S2 exam, but instead re-added the exam.
48.
49. t = s3;
50. t. Paper ();//!! S2 overwrites the S1 exam
51.
52. t = s2;
53. t. Paper (); // S2 overwrites the S1 exam
54.
55. t = s1;
56. t. Paper (); // S1 exam
57.
58.
59 .}
60 .}
61 .}
Namespace ClassLibrary1
{
Public class Teacher
{
Public virtual void Paper ()
{
Console. Write ("the instructor gives the exam ");
}
}
Public class S1: Teacher
{
Public override void Paper ()
{
Console. WriteLine ("S1 exam ");
}
}
Public class S2: S1
{
Public override void Paper ()
{
Console. WriteLine ("S2 rewriting S1 exam ");
}
}
Public class S3: S2
{
Public new void Paper ()
{
Console. WriteLine ("S3 did not overwrite the S2 exam, but re-added the exam ");
}
}
}

Namespace ConsoleApplication1
{
Class Program
{
Static void Main (string [] args)
{

Teacher t = new Teacher ();
T. Paper (); // The instructor gives the exam
S1 s1 = new S1 ();
S1.Paper (); // S1 exam
S2 s2 = new S2 ();
S2.Paper (); // S2 overwrites the S1 exam
S3 s3 = new S3 ();
S3.Paper (); // S3 did not overwrite the S2 exam, but instead re-added the exam.

T = s3;
T. Paper ();//!! S2 overwrites the S1 exam

T = s2;
T. Paper (); // S2 overwrites the S1 exam

T = s1;
T. Paper (); // S1 exam


}
}
}

 

Analysis: Why does t = s3 and t. Paper () show that S2 overwrites the S1 exam? Because the reference t is of the Teacher class, we first find the Paper () method from the Teacher class, and then find the Paper () method of S1 (), then I found that it was rewritten again. So I continued and Found Paper () of S2 (). At this point, it is found that Paper () has not been restarted, so it is stopped. So using override will find the farthest method to be rewritten. In layman's terms: the teacher gave an exam, and classmate A did it. Then student B made another modification on the basis of student A... student n made another modification on the basis of n-1. So the final exam must be the result of the n children's shoes. But the new method only re-writes his own exam. Only S3 s3 = new S3 () and s3.Paper () Can you view your own results. You can use your own references to process your own objects.

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.