public void AimAt(Observer obs)
{
this.observers.Add(obs);
}
public void Cry()
{
Console.WriteLine("Cat cryed!");
foreach (Observer obs in this.observers)
{
obs.Response();
}
}
}
class MainClass
{
static void Main(string[] args)
{
Cat cat = new Cat();
Mouse mouse1 = new Mouse("mouse1", cat);
Mouse mouse2 = new Mouse("mouse2", cat);
Master master = new Master(cat);
cat.Cry();
}
}
Cat cryed!
mouse1 attempt to escape!
mouse2 attempt to escape!
Host waken!
请按任意键继续. . .
写出程序的输出结果
using System;
class Program
{
private string str = "Class1.str";
private int i = 0;
static void StringConvert(string str)
{
str = "A string being converted.";
}
static void StringConvert(Program c)
{
c.str = "B string being converted.";
}
static void Add(int i)
{
i++;
}
static void AddWithRef(ref int i)
{
i++;
}
static void Main()
{
int i1 = 10;
int i2 = 20;
string str = "str";
Program c = new Program();
Add(i1);
AddWithRef(ref i2);
Add(c.i);
StringConvert(str); //string无法被改变
StringConvert(c); //传递对象string可以被改变
Console.WriteLine(i1);
Console.WriteLine(i2);
Console.WriteLine(c.i);
Console.WriteLine(str);
Console.WriteLine(c.str);
}
}
运行结果:
10
21
0
str
B string being converted.
请按任意键继续. . .
写出程序的输出结果
using System;
public abstract class A
{
public A()
{
Console.WriteLine('A');
}
public virtual void Fun()
{
Console.WriteLine("A.Fun()");
}
}
public class B : A
{
public B()
{
Console.WriteLine('B');
}
public new void Fun()
{
Console.WriteLine("B.Fun()");
}
public static void Main()
{
A a = new B();
a.Fun();
}
}
运行结果:
A
B
A.Fun()
请按任意键继续. . .
有继承关系,在构造子类的时候会先构造父类;Fun函数在子类是被new了一下,只是显示隐藏,并没有重写,所以输出还是A.Fun()。如果子类是被override,则最后输出B.Fun
写出程序的输出结果
using System;
public class A
{
public virtual void Fun1(int i)
{
Console.WriteLine(i);
}
public void Fun2(A a)
{
a.Fun1(1); //以传递进来的对象选择哪个Fun1
Fun1(5); //以调用此方法的对象选择哪个Fun1
}
}
public class B : A
{
public override void Fun1(int i)
{
base.Fun1(i + 1);
}
public static void Main()
{
B b = new B();
A a = new A();
a.Fun2(b);
b.Fun2(a);
}
}
输出结果:
2
5
1
6
请按任意键继续. . .
冒泡排序
using System;
public class MainClass
{
public static void ArraySort()
{
int[] Arr = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
int t = 0;
//从小到大排序
for (int i = 0; i < Arr.Length - 1; i++) //总共需要N-1趟
{
for (int j = 0; j < Arr.Length - 1 - i; j++) //每趟总共需要(N-1-趟数)次比较
{
if (Arr[j] > Arr[j + 1])
{
t = Arr[j];
Arr[j] = Arr[j + 1];
Arr[j + 1] = t;
}
}
}
foreach (int i in Arr)
{
Console.Write(i + ",");
}
}
public static void Main()
{
ArraySort();
}
}
输出:1,2,3,4,5,6,7,8,9,请按任意键继续. . .
写出程序的输出结果
using System;
class A
{
public static int X;
static A()
{
X = B.Y + 1;
}
}
class B
{
public static int Y = A.X + 1;
static B() { }
static void Main()
{
Console.WriteLine("X={0},Y={1}", A.X, B.Y);
}
}
输出结果:
X=1,Y=2
请按任意键继续. . .
在.net(C# or vb.net)中如何用户自定义消息,并在窗体中处理这些消息。
在form中重载DefWndProc函数来处理消息:
protected override void DefWndProc(ref System.WinForms.Message m)
{
switch (m.msg)
{
case WM_Lbutton:
///string与MFC中的CString的Format函数的使用方法有所不同
string message = string.Format("收到消息!参数为:{0},{1}", m.wParam, m.lParam);
MessageBox.Show(message);///显示一个消息框
break;
case USER:
//处理的代码
default:
base.DefWndProc(ref m);///调用基类函数处理非自定义消息。
break;
}
}
请说明如下代码的结果
using System;
class Father
{
public virtual void show()
{
Console.WriteLine("Father");
}
static void Main(string[] args)
{
Father obj = new Child();
obj.show();
}
}
class Child : Father
{
public override void show()
{
Console.WriteLine("Child");
}
}
输出:
Child
请按任意键继续. . .
如果把virtual和override去掉,输出结果为Father