目录
类与结构的实例比较
类与结构的差别
如何选择结构还是类
publicstructPerson
{
stringName;
intheight;
intweight;
publicbooloverWeight()
{
//implementsomething
}
}
类示例:
publicclassTestTime
{
inthours;
intminutes;
intseconds;
publicvoidpasstime()
{
//implementationofbehavior
}
}
publicclassTest
{
publicstaticovidMain
{
PersonMyperson=newPerson//声明结构
TestTimeMytime=NewTestTime//声明类
}
}
interfaceIImage
{
voidPaint();
}
structPicture:IImage
{
publicvoidPaint()
{
//paintingcodegoeshere
}
privateintx,y,z; //otherstructmembers
}
有默认的构造函数
有析构函数
可以使用abstract和sealed
有protected修饰符
必须使用new初始化
4.大多数情况下该类型只是一些数据时,结构时最佳的选择