In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to draw the specified shape of C#". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn "how to draw a specified shape in C#"!
The following example creates the Shape base class and the derived classes Circle, Rectangle, Triangle, and the Shape class provides a virtual method called Draw that is overridden in each derived class to draw the specified shape of the class.
Using System
Using System.Collections.Generic
Public class Shape
{
Public int X {get; private set;}
Public int Y {get; private set;}
Public int Height {get; set;}
Public int Width {get; set;}
/ / Virtual method
Public virtual void Draw ()
{
Console.WriteLine ("perform drawing tasks of the base class")
}
}
Class Circle: Shape
{
Public override void Draw ()
{
Console.WriteLine ("draw a circle")
Base.Draw ()
}
}
Class Rectangle: Shape
{
Public override void Draw ()
{
Console.WriteLine (draw a rectangle)
Base.Draw ()
}
}
Class Triangle: Shape
{
Public override void Draw ()
{
Console.WriteLine ("draw a triangle")
Base.Draw ()
}
}
Class Program
{
Static void Main (string [] args)
{
/ / create a List object and add Circle, Triangle and Rectangle to the object
Var shapes = new List
{
New Rectangle ()
New Triangle ()
New Circle ()
}
/ / use the foreach loop to iterate through the derived classes of the list and call the Draw method on each Shape object in it
Foreach (var shape in shapes)
{
Shape.Draw ()
}
Console.WriteLine ("Press any key to exit.")
Console.ReadKey ()
}
}
When the above code is compiled and executed, it produces the following results:
Draw a rectangle to execute the drawing task of the base class, draw a triangle to execute the drawing task of the base class, draw a circle to execute the drawing task of the base class, press any key to exit Thank you for reading, the above is the content of "how to draw the specified shape of C#". After the study of this article, I believe you have a deeper understanding of the problem of how to draw the specified shape of C#. The specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.