首页 > 编程语言 >C# 类的构造函数以及析构函数

C# 类的构造函数以及析构函数

时间:2022-12-21 14:00:47浏览次数:39  
标签:C# double width length 析构 Demo 构造函数

类的构造函数以及析构函数

构造函数

在类初始化的时候被调用,可以方便提前传参。

using System;


namespace MyNameSpace {
    class Demo {
        private double length;
        private double width;

        // 构造函数,带参数的构造函数
        public Demo(double length, double width)
        {
            this.length = length;
            this.width = width;
        }

        public double Area() { 
            return this.length * this.width;
        }

        static void Main(string[] args)
        {
		    // 在实例化类的时候进行传递参数。
            Demo demo = new Demo(10, 20);
            Console.WriteLine(demo.Area());
        }
    }
}

析构函数

类的 析构函数 是类的一个特殊的成员函数,当类的对象超出范围时执行。

析构函数的名称是在类的名称前加上一个波浪形(~)作为前缀,它不返回值,也不带任何参数。

析构函数用于在结束程序(比如关闭文件、释放内存等)之前释放资源。析构函数不能继承或重载。

using System;


namespace MyNameSpace {
    class Demo {
        private double length;
        private double width;

        // 构造函数
        public Demo(double length, double width)
        {
            this.length = length;
            this.width = width;
        }

        // 析构函数
        ~Demo()
        {
            Console.WriteLine("对象已经被删除");
        }

        public double Area() { 
            return this.length * this.width;
        }

        static void Main(string[] args)
        {
            Demo demo = new Demo(10, 20);
            demo.Area();
            Console.WriteLine(demo.Area());
        }
    }
}

输出结果:

200
对象已经被删除

标签:C#,double,width,length,析构,Demo,构造函数
From: https://www.cnblogs.com/shangcc205/p/16996089.html

相关文章

  • Protocol Buffers 3 学习笔记
    官方文档地址syntax="proto3";必须放在.proto文件首行,之前不能有空白行或者注释行,如果不存在或者没有放在首行,则编译器认为是proto2字段对应的序号,在1-15之内(含)时,编码......
  • cups文件损坏导致打印机服务不可用--中标麒麟
    操作系统:中标麒麟现象:打印服务不可用。在这台计算机中启动该服务或者连接到另一个服务解决方法:1、默认装完操作系统cups打印机服务是正常自启动的,文件如果损坏查看服务状......
  • C# Oracle数据库连接并执行类
    OracleHelper.cs usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Text;usingSystem.Data;usingSystem.Con......
  • C# SQLServer数据库连接并执行类
    SQLHelper.cs usingSystem;usingSystem.Collections.Generic;usingSystem.Configuration;usingSystem.Data;usingSystem.Data.SqlClient;usingSystem.Linq;......
  • [leetcode]第 5 天 查找算法(中等)
    04.二维数组中的查找思路直接遍历!两个for循环classSolution{publicbooleanfindNumberIn2DArray(int[][]matrix,inttarget){for(int[]row:mat......
  • Go/Python 基于gRPC传输图片
    python程序作为服务端,Go程序作为客户端,基于gPRC进行通信客户端定义proto文件:syntax="proto3";optiongo_package=".;transfer";serviceGreeter{rpcSendI......
  • TypeError: Assignment to constant variable
    场景: 使用跨组件通信时,想通过重置父组件的值时,浏览器报错TypeError:AssignmenttoconstantvariableES6标准引入了新的关键字const来定义常量,const与let都具有块......
  • cartographer 源码解析(六)
    这一章节呢,主要讲外推器。我们上一章节说到激光的畸变与矫正,最关键的是什么呢?其实最关键的就是关于每个发射点的发射位置的估计推测,这里就用到了外推器去推测每个发射点的位......
  • C++ 犯错修改指南
    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档文章目录​​前言​​​​2.SegmentationFault​​​​3.symbollookuperror:xxxundefinedsymbolxxx......
  • CMake实践(二)
    fPICadd_compile_options(-fPIC)​​大概意思是缺少-fPIC这个编译参数​​​​https://cxyzjd.com/article/winafa/114847300​​-fuse-ld=goldset(CMAKE_SHARED_LINKER_F......