⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 原代码详细说明是关于c++方面的希望可以帮助大家使用
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace EXAM2_5
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	abstract class Shape//定义一个形状抽象类
	{
		protected int x,y;      //坐标
		protected Color c;      //色彩
		protected Graphics g;   //图形对象
		public Shape(){}        //无参构造方法
		public Shape(int x,int y,Color c,Graphics g)//有参的构造方法
		{
			this.x=x;
			this.y=y;
			this.c=c;
			this.g=g;
		}	
		public  void setValue(int x,int y,Color c,Graphics g)
		{
			this.x=x;
			this.y=y;
			this.c=c;
			this.g=g;
		}	
		public abstract double area();    //抽象的求面积方法
		public abstract void draw();      //抽象的绘图方法
	} 
	class Square:Shape//正方形类,继承了形状类
	{ 
		protected int a;
		public Square(){}
		public Square(int x,int y,int a,Color c,Graphics g):base(x,y,c,g)
		{
			this.a=a;
		}
		public void setValue(int x,int y,int a,Color c,Graphics g)
		{
			base.setValue(x,y,c,g);  //调用父类的方法
			this.a=a;
		}
		public override double area()//实现求面积的方法
		{         
			return a*a;
		}
		public override void draw() //实现求画图的方法
		{           
			SolidBrush sb=new SolidBrush(c);
			g.FillRectangle(sb,x,y,a,a);     //画填充矩形	
		}
	}      	
	class Rectangle:Square//矩形类,继承了正方形类
	{
		private int b;
		public Rectangle(){}
		public Rectangle(int x,int y,int a,int b,Color c,Graphics g):base(x,y,a,c,g)
		{
			this.b=b; 	
		}  	
		public void setValue(int x,int y,int a,int b,Color c,Graphics g)
		{
			base.setValue(x,y,a,c,g);
			this.b=b;	
		} 	
		public override double area()
		{
			return a*b;
		}
		public override void draw()
		{
			SolidBrush sb=new SolidBrush(c);
			g.FillRectangle(sb,x,y,a,b);     //画填充矩形	
		}	
	}	
	class Circle:Shape//定义圆类,继承了形状类
	{
		private int r;
		public Circle(){}
		public Circle(int x,int y,int r,Color c,Graphics g):base(x,y,c,g)
		{
			this.r=r;	
		}	
		public void setValue(int x,int y,int r,Color c,Graphics g)
		{
			base.setValue(x,y,c,g);
			this.r=r;	
		}
		public override double area()
		{
			return 3.415926*r*r;
		}
		public override void draw()
		{
			SolidBrush sb=new SolidBrush(c);
			g.FillEllipse(sb,x-r,y-r,2*r,2*r);     //画填充矩形	
		}    			
	}	
	public class Form1 : System.Windows.Forms.Form
	{
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(375, 252);
			this.Name = "Form1";
			this.Text = "Form1";
			this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
		{
			Graphics g=e.Graphics;
			Square a=new Square();                                //构造时不赋值
			a.setValue(20,20,100,Color.Blue,g);                   //调用赋值的方法
			a.draw();//调用绘图方法
			SolidBrush sb=new SolidBrush(Color.Black);
			Font font=new Font("宋体",12);
			g.DrawString("area of a="+a.area(),font,sb,20, 130 );        //输出面积
			Square b=new Square(160,20,50,Color.Red,g);           //构造时直接赋值
			b.draw();                                             //调用绘图方法
			g.DrawString("area of b="+b.area(),font,sb,160, 80 );        //输出面积
			Rectangle c=new Rectangle(160,100,100,150,Color.Black,g);
			Square m;
			m=c;                                                  //父类变量存放子类对象
			m.draw();	
			g.DrawString("area of c="+c.area(),font,sb,160, 260 );        //输出面积
			Circle d=new Circle(340,100,50,Color.Green,g);
			d.draw();
			g.DrawString("area of d="+d.area(),font,sb,300, 160 );        //输出面积
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -