8.7.8 Instance constructors

An instance constructor is a member that implements the actions required to
initialize an instance of a class.
The example
using System;
class Point
{
public double x, y;
public Point() {
this.x = 0;
this.y = 0;
}
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public static double Distance(Point a, Point b) {
double xdiff = a.x - b.x;
double ydiff = a.y - b.y;
return Math.Sqrt(xdiff * xdiff + ydiff * ydiff);
}
public override string ToString() {
return string.Format("({0}, {1})", x, y);
}
}
class Test
{
static void Main() {
Point a = new Point();
Point b = new Point(3, 4);
double d = Point.Distance(a, b);
Console.WriteLine("Distance from {0} to {1} is {2}", a, b, d);
}
}
shows a Point class that provides two public instance constructors, one of
which takes no arguments, while
the other takes two double arguments.
If no instance constructor is supplied for a class, then an empty one with
no parameters is automatically
provided.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值