题一:
程序设计: 猫大叫一声,所有的老鼠都开始逃跑,主人被惊醒。(C#语言)
要求:
<1>.构造出Cat、Mouse、Master三个类,并能使程序运行
<2>从Mouse和Master中提取抽象
<3>联动效应,只要执行Cat.Cryed()就可以使老鼠逃跑,主人惊醒。
using System; using System.Collections; using System.Collections.Generic ; namespace gtkTest { public class ObserverClass { public ObserverClass () { } public static void ObserverFun () { Cat cat = new Cat(); Mouse mouse1 = new Mouse("mouse1", cat); Mouse mouse2 = new Mouse("mouse2", cat); Master master = new Master(cat); cat.Cry(); } public interface Observer { void Response(); //观察者的响应,如是老鼠见到猫的反映 } public interface Subject { void AimAt(Observer obs); //针对哪些观察者,这里指猫的要扑捉的对象---老鼠 } public class Mouse : Observer { private string name; public Mouse(string name, Subject subj) { this.name = name; subj.AimAt(this); } public void Response() { Console.WriteLine(name + " attempt to escape!"); } } public class Master : Observer { public Master(Subject subj) { subj.AimAt(this); } public void Response() { Console.WriteLine("Host waken!"); } } public class Cat : Subject { private ArrayList observers; public Cat() { this.observers = new ArrayList(); } public void AimAt(Observer obs) { this.observers.Add(obs); } public void Cry() { Console.WriteLine("Cat cryed!"); foreach (Observer obs in this.observers) { obs.Response(); } } } } } //2 using System; namespace gtkTest { public class ObserverDeleClass { public ObserverDeleClass () { } public static void ObserverDeleFun () { Cat cat = new Cat(); Mouse mouse1 = new Mouse("ObserverDelemouse1", cat); Mouse mouse2 = new Mouse("ObserverDelemouse2", cat); Master master = new Master(cat); cat.Cry(); } //--------------------------------------------------------------------------------------------- //设计方法二: 使用event -- delegate设计.. public delegate void SubEventHandler(); public abstract class Subject { public event SubEventHandler SubEvent; protected void FireAway() { if (this.SubEvent != null) this.SubEvent(); } } public class Cat : Subject { public void Cry() { Console.WriteLine("ObserverDelecat cryed."); this.FireAway(); } } public abstract class Observer { public Observer(Subject sub) { sub.SubEvent += new SubEventHandler(Response); } public abstract void Response(); } public class Mouse : Observer { private string name; public Mouse(string name, Subject sub) : base(sub) { this.name = name; } public override void Response() { Console.WriteLine(name + " attempt to escape!"); } } public class Master : Observer { public Master(Subject sub) : base(sub){} public override void Response() { Console.WriteLine("ObserverDelehost waken"); } } } } //3 gtkTest.ObserverDeleClass.ObserverDeleFun(); gtkTest.ObserverClass.ObserverFun ();
参考: https://blog.csdn.net/hunterxray/article/details/1532674
题二:
一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第30位数是多少, 用递归算法实现
public class MainClass { public static void Main() { Console.WriteLine(Foo(30)); } public static int Foo(int i) { if (i <= 0) return 0; else if(i > 0 && i <= 2) return 1; else return Foo(i -1) + Foo(i - 2); } }
参考:https://blog.csdn.net/zhangzuqing/article/details/20208385
题三
using System; namespace gtkTest { public class MyClassv { public MyClassv () { v1++; v2++; } public static int v1; public int v2; public static void MyClassvFun () { MyClassv mc1 = new MyClassv (), mc2 = new MyClassv (); Console.WriteLine("{0}{1}{2}",MyClassv.v1,mc1.v2,mc2.v2); } } }
gtkTest.MyClassv.MyClassvFun ();
注意:
题四
gtkTest.MyStringClass .MyStringClassFun(); gtkTest.MyStringClass .MyIntClassFun(); gtkTest.MyCountClass.MyCountClassFun ();
using System; namespace gtkTest { public class MyStringClass { public MyStringClass () { } public static void MyStringClassFun () { string sr1 = "123" + "abc"; string sr2 = "123abc"; Console.WriteLine(sr1==sr2); Console.WriteLine(System .Object.ReferenceEquals(sr1,sr2 )); } public static void MyIntClassFun () { int i = 5; int j = 5; if(object.ReferenceEquals (i,j))Console.WriteLine("Equal"); else Console.WriteLine("Not Equal"); } } }
using System; namespace gtkTest { public class MyCountClass { private static int count=0; static MyCountClass () { count++; } public MyCountClass () { count++; } MyCountClass o1=new MyCountClass(); MyCountClass o2=new MyCountClass(); public static void MyCountClassFun () { //question is o1.Count,but count static ,o1 is new not is static Console.WriteLine("{0}",MyCountClass.count ); } } }
题五
gtkTest .MyCallClass.MyCallClassFun();
using System; namespace gtkTest { public class MyCallClass { public MyCallClass () { } abstract class BaseClass { public virtual void MethodA(){} public virtual void MethodB(){} } class Class1:BaseClass { public void MethodA(string arg){} public void MethodB(string arg){} } class Class2:Class1 { new public void MethodB(){} } public static void MyCallClassFun () { Class2 o= new Class2 (); //Console.WriteLine(o.MethodA()); o.MethodA(); } } }
请问, o.MethodA 调用的是? BaseClass.MethodA
-
-
Class2.MethodA
-
Class1.MethodA
-
BaseClass.MethodA
-
都不是
-