C#构造函数与析构函数举例
来源:互联网 发布:ubuntu 怎么安装软件 编辑:程序博客网 时间:2024/04/30 12:41
1>构造函数参数可以有0到多个参数
2>构造函数无返回值
3>构造函数可以重载
4>没有构造函数的类将被指定一个默认的构造函数
5>析构函数不能带参数,也没有返回值,不能是静态的,也不能有访问修饰符
6>析构函数不能是继承而来的,不能显示调用析构函数。它自动被执行
实例:
Student.cs
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Ex7_7{ class Student { public string name; public int age; public string sex; public Student() { Console.WriteLine("无参构造函数已被执行,对象成功创建"); } public Student(string s) { name = s; } public Student(string s, int a) { name = s; age = a; } public Student(string s, int a, string se) { name = s; age = a; sex = se; } ~Student() { Console.WriteLine("析构函数已被执行,对象即将被销毁!"); } }}
Program.cs
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Ex7_7{ class Program { static void Main(string[] args) { Student p0 = new Student(); Student p1 = new Student("张三"); Student p2 = new Student("李四",32); Student p3 = new Student("王武",34,"女"); Console.WriteLine("对象一的数据有:姓名:"+p1.name); Console.WriteLine("对象二的数据有:姓名"+p2.name+"年龄"+p2.age); Console.WriteLine("对象三的数据有:姓名"+p3.name+",年龄"+p3.age+",性别"+p3.sex); Console.ReadLine(); } }}
结果:
- C#构造函数与析构函数举例
- 构造函数与析构函数举例
- c#构造函数与析构函数
- c#构造函数与析构函数
- C# 构造函数与析构函数
- C# 构造函数与析构函数
- C#构造函数与折构函数
- C#之析构函数与构造函数
- (四)C#之构造函数与析构函数
- C# 构造函数和析构函数
- C#构造函数和析构函数
- C#构造函数和析构函数
- C#构造函数&析构函数_161019
- java与C#构造函数
- objective-c构造函数举例
- C#类和函数(构造函数、析构函数)
- 构造函数与析构函数
- 构造函数与析构函数
- 华为终端董事长余承东:乔布斯走后创新少了(转)
- 为什么软件界面截图不存为jpg 而推荐png?
- dotnetnuke入門
- 实例化Spring IoC容器
- 动画xml 手势识别GestureDetector
- C#构造函数与析构函数举例
- Chrome下强制http重定向到https的方法
- 研究生常用网站
- uploadify3.1版本上传附件,后台用webservice处理上传文件总结
- Opera
- mybatis工具generator使用
- Java调用DotNet WebService为什么那么难? .
- Chrome
- UBUNTU中如何获得root权限