What is the difference between override and new in "Override" and "hide "?

Source: Internet
Author: User
Note: textbox4sql is an idiot. This problem should be maintained at the SQL layer rather than on the UI.

For so long, oop seldom uses the new keyword "hide" members of the parent class. One day, the text attribute of a textbox needs to be modified. Since the SQL syntax is called in the original code, textbox is directly called. the text attribute is used to merge varchar data into SQL statements. It never filters out special characters for strings. Therefore, I entered "'" (single quotation marks) in a text box ), as a result, SQL is abnormal. However, this SQL call method is everywhere in the Code. Therefore, we plan to recreate textbox so that when reading the text attribute, we can replace the first single quotation mark with two, make the SQL syntax correct. The first textbox4sql class:

  1. Using system. Windows. forms;
  2. Namespace windowsformsapplication1
  3. {
  4. Public class textbox4sql: textbox
  5. {
  6. Public override string text
  7. {
  8. Get
  9. {
  10. Return base. Text. Replace ("'","''");
  11. }
  12. Set
  13. {
  14. Base. Text = value;
  15. }
  16. }
  17. Public String RealText
  18. {
  19. Get
  20. {
  21. Return base. text;
  22. }
  23. Set
  24. {
  25. Base. Text = value;
  26. }
  27. }
  28. }
  29. }

The text attribute of the parent class is overwritten, so that each of the single quotes is replaced with two single quotes (11 rows ). However, sometimes the original text content needs to be obtained, so a new RealText attribute is written and the text attribute content of the parent class is directly returned (24 rows ). Unfortunately, we use the override keyword to override the parent text attribute. The content returned by RealText will be processed by 11th lines of code. Obviously, I should use the new keyword instead of override rewriting text.

The New Keyword overwrites the text property of the textbox4sql class inherited by the parent class, but the text (base. text) is still valid. This is different from override. If the parent class uses the virtual keyword to modify, it wants the override of the quilt class to be overwritten. rewriting means the effect is completely replaced. Therefore, if you replace override with new, the override of the text attribute overwrites it, you can make the RealText attribute return the original text content of the parent class. Or we want the RealText attribute to be overwritten by the quilt class. Add the virtual keyword.

  1. Using system. Windows. forms;
  2. Namespace windowsformsapplication1
  3. {
  4. Public class textbox4sql: textbox
  5. {
  6. Public New String text
  7. {
  8. Get
  9. {
  10. Return base. Text. Replace ("'","''");
  11. }
  12. Set
  13. {
  14. Base. Text = value;
  15. }
  16. }
  17. Public Virtual string RealText
  18. {
  19. Get
  20. {
  21. Return base. text;
  22. }
  23. Set
  24. {
  25. Base. Text = value;
  26. }
  27. }
  28. }
  29. }

It also needs to be explained that new can hide members of any parent class. Override can be rewritten by virtual, abstract, or override modified members, so we call override as rewrite.

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.