You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

类之间的调用: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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:58:58