一.定义矩形类,可以定义矩形的长和宽
1. 新建一个控制台程序(File—>New->Project->conole project),名称为ClassTest;
2. 右键工程名->add->new items_>class,名称为Rec
3. 在rec.class中添加如下代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClassTest
{
class Rec
{
private int _width;
private int _height;
private string _name;
public Rec(int width, int height, string name)
{
this._width = width;
this._height = height;
this._name = name;
}
public string Name
{
get { return _name; }
set { _name = value; }
}
public int Width
{
get { return _width;}
set { _width = value; }
}
public int Height
{
get { return _height; }
set { _height = value; }
}
public void Show()
{
Console.WriteLine("矩?形?#" + _name + " " + "高?=" + _height + ",宽¨ª=" + _width);
}
public void Compare(Rec r2)
{
if (this._width * this._height > r2._width * r2._height)
Console.WriteLine("矩?形?" + r2._name + "小?于®¨²矩?形?" + this._name);
else if (this._width * this._height < r2._width * r2._height)
Console.WriteLine("矩?形?" + this._name + "小?于®¨²矩?形?" + r2._name);
else if (this._width * this._height ==r2._width * r2._height)
Console.WriteLine("矩?形?" + r2._name + "和¨ª" + this._name+"的Ì?长¡è和¨ª宽¨ª都?相¨¤等̨¨");
}
}
}
4. 在Progrss.cs中添加如下代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClassTest
{
class Program
{
static void Main(string[] args)
{
Rec r1 = new Rec(10, 15, "first");
Rec r2 = new Rec(15, 10, "second");
Rec r3 = new Rec(19,23,"third");
r1.Compare(r2);
r1.Compare(r3);
r2.Compare(r3);
}
}
}
按ctr+F5,运行图如下: