其实在C#和C++中类的使用基本相同,所以这里是C# 中类的使用小例子
using System; namespace ConsoleApp2 { class Rectangle { //成员变量 public double length, width; public double GetArea() { return length * width; } public void Display() { Console.WriteLine("长度:{0}", length); Console.WriteLine("宽度:{0}",width); Console.Write("面积:{0}",GetArea()); } } //Rectangle 类结束 class ExecuteRectangle { static void Main(string[] args) { Rectangle r = new Rectangle(); r.length = 4.5; r.width = 3.8; r.Display(); } } }
参考连接:
https://www.runoob.com/csharp/csharp-encapsulation.html