📄 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 + -