C#语言基础(三)
类
18、类属性
using System;
using System.Collections.Generic;
using System.Text;
//类属性,类TimePeriod存储了一个时间段。类内部以秒为单位存储时间,但提供一个名称为Hours的属性,
//它允许客户端指定以小时为单位的时间。Hours属性的访问器执行小时和秒之间的转换。
namespace ClassPropertyTest
{
class ClassPropertyTest
{
static void Main(string[] args)
{
TimePeriod t=new TimePeriod();
t.Hours=24;//设置时间属性
Console.WriteLine("Time in hours:"+t.Hours);//获取时间属性
SampleRefType rt = new SampleRefType();
rt.value = 44;
ModifyObject(rt);
System.Console.WriteLine(rt.value);
}
class TimePeriod
{
private double hours;
public double Hours
{
get { return hours / 3600; }//返回属性值
set { hours = value * 3600; }//分配新值
}
}
public class SampleRefType
{
public int value;
}
static void ModifyObject(SampleRefType obj)
{
obj.value = 33;
}
}
}
19、索引器
using System;
using System.Collections.Generic;
using System.Text;
//索引器,代码中定义了一个泛型类,并为其提供了一个简单的get和set访问器方法。
namespace SuoyinqiTest
{
class SampleCollection<T>
{
private T[] arr = new T[100];
public T this[int i]
{
get
{
return arr[i];
}
set
{
arr[i] = value;
}
}
}
class Suoyinqi
{
static void Main(string[] args)
{
SampleCollection<string> stringCollection = new SampleCollection<string>();
SampleCollection<int> intCollection = new SampleCollection<int>();
stringCollection[0] = "Hello,Word";
intCollection[0] = 12345;
System.Console.WriteLine(stringCollection[0]);
System.Console.WriteLine(intCollection[0]);
}
}
}
20、构造函数
using System;
using System.Collections.Generic;
using System.Text;
//构造函数,代码中定义了一个构造函数Taxi类,使用new运算符来实例化该类。
namespace GouZTestTaxi
{
class TestTaxi
{
static void Main(string[] args)
{
Taxi t = new Taxi();
Console.WriteLine(t.isInitialized);
}
}
public class Taxi
{
public bool isInitialized;
public Taxi()
{
isInitialized = true;
}
}
}
21、实例构造函数
using System;
using System.Collections.Generic;
using System.Text;
//实例构造函数用于创建和初始化实例。创建新对象时将调用类构造函数,代码中演示包含两个类构造函数的类;
//一个类构造函数没有参数,另一个类构造函数带有两个参数。
namespace GouZTestConstruct
{
class TestConstruct
{
static void Main(string[] args)
{
CoOrds p1 = new CoOrds();
CoOrds p2 = new CoOrds(5, 3);
Console.WriteLine("CoOrds #1 at {0}", p1);
Console.WriteLine("CoOrds #2 at {0}", p2);
}
}
class CoOrds
{
public int x, y;
//默认构造函数
public CoOrds()
{
x = 0;
y = 0;
}
//构造函数中带有两个参数
public CoOrds(int x, int y)
{
this.x = x;
this.y = y;
}
//重写ToString方法
public override string ToString()
{
return (System.String.Format("({0},{1})", x, y));
}
}
}
22、私有构造函数
using System;
using System.Collections.Generic;
using System.Text;
//私有构造函数是一种特殊的实例构造函数。它通常用在只包含静态成员的类中。
//如果类具有一个或多个私有构造函数而没有公共构造函数,则不允许其他类(除了嵌套类)创建该类的实例。
namespace GouZTestCouter
{
class TestCounter
{
static void Main(string[] args)
{
Counter.currentCount = 100;
Counter.IncrementCount();
Console.WriteLine("New count:{0}", Counter.currentCount);
}
}
public class Counter
{
private Counter() { }
public static int currentCount;
public static int IncrementCount()
{
return ++currentCount;
}
}
}
23、静态构造函数
using System;
using System.Collections.Generic;
using System.Text;
//静态构造函数,代码中类Bus有一个静态构造函数和一个静态成员Drive()。当调用Drive()时将调用静态构造函数来初始化类。
namespace GouZTestBus
{
class TestBus
{
static void Main(string[] args)
{
Bus.Drive();
}
}
public class Bus
{
//静态构造函数
static Bus()
{
System.Console.WriteLine("The static constructor invoked.");
}
public static void Drive()
{
System.Console.WriteLine("The Drive method invoked.");
}
}
}
24、析构函数
using System;
using System.Collections.Generic;
using System.Text;
//析构函数用来执行对象的终止操作。
namespace GouZTestDestructors
{
class TestDestructors
{
static void Main(string[] args)
{
Third t = new Third();
}
}
class First
{
~First()
{
System.Console.WriteLine("First's destructor is called.");
}
}
class Second : First
{
~Second()
{
System.Console.WriteLine("Second's destructor is called.");
}
}
class Third : Second
{
~Third()
{
System.Console.WriteLine("Third's destructor is called.");
}
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.