Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to rewrite the method of C #

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you about how C # rewrites the method. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

C # rewriting method

The important principle of object-oriented design is polymorphism. Regardless of the advanced theory, polymorphism means that when the base class programmer has designed a method for rewriting, you can redefine (rewrite) the method of the base class in the derived class. Base class programmers can use the virtual keyword to design methods:

Virtual void CanBOverridden ()

When deriving from a base class, all you need to do is add the override keyword to the new method:

Override void CanBOverridden ()

When rewriting a method of a base class, you must understand that you cannot change the access properties of the method-you will learn more about access modifiers later in this chapter.

In addition to the fact that base class methods are overwritten, there is another, even more important, rewriting feature. When you cast a derived class to a base class type and then call a virtual method, the method of the derived class is called instead of the method of the base class.

((BaseClass) DerivedClassInstance). CanBOverridden ()

To demonstrate the concept of virtual methods, show how to create a triangular base class that has a member method (ComputeArea) that can be overridden.

Using System; class Triangle {public virtual double ComputeArea (int a, int b, int c) {/ / Heronian formula double s = (a + b + c) / 2.0; double dArea = Math.Sqrt (s* (smura) * (smurb) * (smurc)); return dArea;}} class RightAngledTriangle:Triangle {public override double ComputeArea (int a, int b, int c) {double dArea = aroombUnip 2.0; return dArea }} class TriangleTestApp {public static void Main () {Triangle tri = new Triangle (); Console.WriteLine (tri.ComputeArea (2,5,6)); RightAngledTriangle rat = new RightAngledTriangle (); Console.WriteLine (rat.ComputeArea (3,4,5));}}

The base class Triangle defines the method ComputeArea. It takes three parameters, returns a double result, and is publicly accessible. Derived from the Triangle class is RightAngledTriangle, which overwrites the ComputeArea method and implements its own area calculation formula. Both classes are instantiated and validated in the Main () method of the application class named TriangleTestApp.

I missed line 14:

Class RightAngledTriangle: Triangle

A colon (:) in a class statement indicates that RightAngledTriangle derives from class Triangle. That's what you have to do to let C # know that you want to use Triangle as the base class for RightAngledTriangle.

When you look closely at the ComputeArea method of right triangles, you will find that the third parameter is not used for calculation. However, you can use this parameter to verify whether it is a "right angle".

Class RightAngledTriangle:Triangle {public override double ComputeArea (int a, int b, int c) {const double dEpsilon = 0.0001; double dArea = 0; if (Math.Abs (Apolia + Beneb-cystec)) > dEpsilon) {dArea = base.ComputeArea (areco bpenc);} else {dArea = astatbp2.0;} return dArea;}}

The detection simply uses the Pythagorean formula, for right triangles, the test result must be 0. 5%. If the result is not 0, the class calls the ComputeArea of its base class to implement it.

DArea = base.ComputeArea (a _ r _ b _ m c)

The main point of the example is that by explicitly using the qualification check of the base class, you can easily call the base class to implement the C # rewriting method. This is very helpful when you need to implement its functionality in the base class and do not want to repeat it in the C# rewriting method.

Thank you for reading! This is the end of this article on "how to rewrite C#". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report