How to calculate the area of images with different shapes by C #
This article mainly introduces "how C# calculates the area of images of different shapes". In daily operation, I believe many people have doubts about how to calculate the area of images of different shapes in C#. The editor consulted all kinds of data and sorted out a simple and easy-to-use method of operation. I hope it will be helpful to answer the question of "how to calculate the area of images of different shapes in C#". Next, please follow the editor to study!
The following program demonstrates using the virtual method area () to calculate the area of images of different shapes:
Using System
Namespace PolymorphismApplication
{
Class Shape
{
Protected int width, height
Public Shape (int aversion 0, int bread0)
{
Width = a
Height = b
}
Public virtual int area ()
{
Console.WriteLine ("area of parent class:")
Return 0
}
}
Class Rectangle: Shape
{
Public Rectangle (int axi0, int bacc0): base (a, b)
{
}
Public override int area ()
{
Console.WriteLine ("area of Rectangle class:")
Return (width * height)
}
}
Class Triangle: Shape
{
Public Triangle (int a = 0, int b = 0): base (a, b)
{
}
Public override int area ()
{
Console.WriteLine ("area of Triangle class:")
Return (width * height / 2)
}
}
Class Caller
{
Public void CallArea (Shape sh)
{
Int a
A = sh.area ()
Console.WriteLine (area: {0}, a)
}
}
Class Tester
{
Static void Main (string [] args)
{
Caller c = new Caller ()
Rectangle r = new Rectangle (10,7)
Triangle t = new Triangle (10,5)
C.CallArea (r)
C.CallArea (t)
Console.ReadKey ()
}
}
}
When the above code is compiled and executed, it produces the following results:
Area of Rectangle class: area of 70Triangle class: area: 25 so far, the study on "how to calculate the area of images of different shapes in C#" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!