以下题目,我已全部作答,答案仅供参考!水平和理解有限,可能有误,欢迎指正,谢谢!
1. 填空: (1)面向对象的语言具有__继承______性、_____多态____性、____封装____性。
(2)能用foreach遍历访问的对象需要实现 _______IEnumerable_________接口或声明____GetEnumerator____________方法的类型。
(3)列举ADO.net中的五个主要对象__Connection____、_Dataset____、___Command____、__DataReader_____、____DataAdapter____。
2. 不定项选择:
(1) 以下叙述正确的是:B,C
A. 接口中可以有虚方法。 B. 一个类可以实现多个接口。
C. 接口不能被实例化。 D. 接口中可以包含已实现的方法。
(2) 从数据库读取记录,你可能用到的方法有:B,C,D
A. ExecuteNonQuery B. ExecuteScalar
C. Fill D. ExecuteReader
3. 简述 private、 protected、 public、 internal 修饰符的访问权限。
private:私有成员,仅类的内部可访问;
protected:受保护成员,仅在类与子类的内部可访问;
public:公供成员,不受限制,可在任意范围下访问;
internal :仅在同一个程式集中(即:同一命名空间中)可访问;
4. 写出一条Sql语句: 取出表A中第31到第40记录(SQLServer, 以自动增长的ID作为主键, 注意:ID可能不是连续的。)
一:select top 10 * from [table] where [id]>(select top 30 MAX([id]) from [table] order by [id])
order by [id]
二:select top 10 * from [table] where id not in (select top 30 id from [table] order by [id]) order by [id]
5 .列举ASP.NET 页面之间传递值的几种方式。
request.querystring,request.form,session,server.transfer,cookies,application
6. 写出程序的输出结果
class Class1 {
private string str = "Class1.str";
private int i = 0;
static void StringConvert(string str) {
str = "string being converted.";
}
static void StringConvert(Class1 c) {
c.str = "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";
Class1 c = new Class1();
Add(i1);
AddWithRef(ref i2);
Add(c.i);
StringConvert(str);
StringConvert(c);
Console.WriteLine(i1);
Console.WriteLine(i2);
Console.WriteLine(c.i);
Console.WriteLine(str);
Console.WriteLine(c.str);
}
}
10
21
0
str
string being converted.
7.写出程序的输出结果
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()
8. 写出程序的输出结果:
public class A
{
public virtual void Fun1(int i)
{
Console.WriteLine(i);
}
public void Fun2(A a)
{
a.Fun1(1);
Fun1(5);
}
}
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);
}
}
9. 一列数的规则如下: 1、1、2、3、5、8、13、21、34......
求第30位数是多少, 用递归算法实现。(C#语言)
static void Main(string[] args)
{
int result = Calc(30);
Console.WriteLine(result);
}
static int Calc(int num)
{
if (num <= 0)
{
return 0;
}
else if (num <= 2)
{
return 1;
}
else
{
return Calc(num - 1) + Calc(num - 2);
}
}
10. 程序设计: 猫大叫一声,所有的老鼠都开始逃跑,主人被惊醒。(C#语言)
要求: 1.要有联动性,老鼠和主人的行为是被动的。
2.考虑可扩展性,猫的叫声可能引起其他联动效应。
【该答案非本人作答,但明白其实现原理:观察者模式】
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();
}
}
}
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();
}
}
//---------------------------------------------------------------------------------------------
设计方法二: 使用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("cat 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("host waken");
}
}
class Class1
{
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();
}
}