类之间的调用:C#矩形类代码翻译及技术内容保留需求
类之间的调用
嘿,这是一个展示C#里类之间调用的示例代码,我已经把代码里的注释都翻译成中文了,你可以直接参考:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Lesson02 { class Rectangle { private double length; // 默认访问级别 private double width; public Rectangle(double l, double w) { length = l; width = w; } public double GetArea() { return length * width; } } } class Program { static void Main(string[] args) { Rectangle rect = new Rectangle(10.0, 20.0); double area = rect.GetArea(); Console.WriteLine("矩形的面积为:{0}", area); } }
简单说下逻辑:我们先定义了Rectangle类来封装矩形的边长属性和计算面积的方法,接着在Program类的Main方法里实例化这个矩形类,调用它的GetArea()方法来计算并输出面积,清晰展示了类之间的调用过程~
内容的提问来源于stack exchange,提问作者Jonathan Bond
相关产品推荐
相关产品推荐

