C# 程序中的构造函数是什么?
类构造函数是每次我们创建该类的对象时执行的类的特殊成员函数。
构造函数与类具有完全相同的名字,并且没有返回类型。
构造函数具有与类名完全相同的名字 −
class Demo {
public Demo() {}
}以下是一个示例 −
示例
using System;
namespace LineApplication {
class Line {
private double length; // Length of a line
public Line() {
Console.WriteLine("Object is being created");
}
public void setLength( double len ) {
length = len;
}
public double getLength() {
return length;
}
static void Main(string[] args) {
Line line = new Line();
// set line length
line.setLength(6.0);
Console.WriteLine("Length of line : {0}", line.getLength());
Console.ReadKey();
}
}
}输出
Object is being created Length of line : 6
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言
C++
C#
MongoDB
MySQL
Javascript
PHP