class1.cs

来自「ASP.NET动态网站开发基础教程(C#)光盘素材的开发示例」· CS 代码 · 共 87 行

CS
87
字号
using System;

namespace Inherit_Demo
{
	/// <summary>
	/// Class1 的摘要说明。
	/// </summary>
	class Class1
	{
		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			//
			// TODO: 在此处添加代码以启动应用程序
			//
			
			Rectangle rect = new Rectangle(0, 0, 20, 50);
			Square sqr = new Square(0, 0, 20, 50);
			rect.ShowArea();
			sqr.ShowArea();

			Console.WriteLine("\n\n按回车键退出");
			Console.ReadLine();
		}
	}

	
	class Rectangle 
	{
		public int left, top, width, height;
		protected string type;
     
		public Rectangle()
		{
			left = 0;
			top = 0;
			width = 10;
			height = 10;
			type = "矩形";
		}
    
		public Rectangle(int aLeft,int aTop, int aWidth,int aHeight)
		{
			left = aLeft;
			top = aTop;
			width = aWidth;
			height = aHeight;
			type = "矩形";
		}
    
		public void ShowArea()
		{
			Console.WriteLine("当前形状为{0}, 面积是{1}", 
				type, width*height);
		}
	}

	class Square : Rectangle
	{
		public Square():base() 
		{
			type = "正方形";
		}
    
		public Square(int aLeft,int aTop,int aWidth,int aHeight):this()
		{
			left = aLeft;
			top = aTop;
        
			//当宽和高不等时,去较小的值
			if (aWidth > aHeight) 
			{
				width = aHeight;
				height = aHeight;
			}
			else
			{
				width = aWidth;
				height = aWidth;
			}
		} 
	}
}

⌨️ 快捷键说明

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