?? p127.cs
字號:
using System;
namespace exam1
{
// public delegate void ChangedEventHandler(System.DateTime pt);
//顯示了一個類,它包含了具有各種可訪問性的成員。調用在Form1的btnP127_Click中
class MyClass
{
public MyClass()
{
Console.WriteLine("Instance constructor");
}
public MyClass(int value)
{
MyField = value;
Console.WriteLine("Instance constructor");
}
~MyClass()
{
Console.WriteLine("Destructor");
}
public const int MyConst = 12;
public int MyField = 34;
public void MyMethod()
{
Console.WriteLine("MyClass.MyMethod");
}
public int MyProperty
{
get
{
return MyField;
}
set
{
MyField = value;
}
}
public int this[int index]
{
get
{
return 0;
}
set
{
Console.WriteLine("this[{0}] = {1}", index, value);
}
}
//public event ChangedEventHandler MyEvent; //聲明事件本身
public event EventHandler MyEvent;
public void OnEvent()
{
System.EventArgs e=null;
if (MyEvent!= null) MyEvent(this,e);
}
public static MyClass operator+(MyClass a, MyClass b)
{
return new MyClass(a.MyField + b.MyField);
}
}
//重載示例
public struct Complex
{
public int real;
public int imaginary;
public Complex(int real, int imaginary)
{
this.real = real;
this.imaginary = imaginary;
}
// Declare which operator to overload (+), the types
// that can be added (two Complex objects), and the
// return type (Complex):
public static Complex operator +(Complex c1, Complex c2)
{
return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
}
// Override the ToString method to display an complex number in the suitable format:
public override string ToString()
{
return(String.Format("{0} + {1}i", real, imaginary));
}
public static void Display()
{
Complex num1 = new Complex(2,3);
Complex num2 = new Complex(3,4);
// Add two Complex objects (num1 and num2) through the
// overloaded plus operator:
Complex sum = num1 + num2;
// Print the numbers and the sum using the overriden ToString method:
Console.WriteLine("First complex number: {0}",num1);
Console.WriteLine("Second complex number: {0}",num2);
Console.WriteLine("The sum of the two numbers: {0}",sum);
}
}
//委托示例
public delegate void SimpleDelegate(); //聲明
class delegateTest
{
static void F()
{
System.Console.WriteLine("Test.F");
}
public void CallDelegate()
{
SimpleDelegate d = new SimpleDelegate(F); //聲明
d();
}
public void MultiCall(SimpleDelegate d, int count)
{
for (int i = 0; i < count; i++)
{
d();
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -