Console.WriteLine(正数:{0}、负数: {1}、零:{2}
6.比尔在教他的女儿面向对象的知识。他写了一个类Bill和一个接口IPrintname。现在他希望女儿能够写一个类Jennifer,继承自Bill,重写基类函数WhoAmI(),将输出改为“Jennifer”;并且实现接口IPrintname,该接口只有一个方法Print,这个方法的实现会在控制台打印调用WhoAmI函数的结果(即:“Jennifer”)。请改正错误的代码。 public class Bill {
public string WhoAmI() //这一行改为public virtual string WhoAmI() {
return } }
public void interface IPrintname //本行应更正为:public interface IPrintname,即接口不应该有void {
void Print(); }
public class Jennifer : Bill, IPrintname {
public override string WhoAmI() {
return }
public void Print() {
Console.WriteLine(WhoAmI()); } }
7. We have two integers: m and n, write an function to compute m in power of n. And how you test it?
static float pow(int m, int n) {
if (m == 0)
return 1;//某些领域为1,或者抛出异常 else
{
if (n == 0) return 1; if (n > 0)
return m * pow(m, n - 1); else
return 1 / pow(m, -n); } }
测试:
pow(0,0) ? 1 Pow(0, -0)?1 Pow(1, 0)? 1 Pow (-2, 0) Pow (3, 2) Pow (3, -2)
Pow (300, 100) Pow (300, -100)
8. 接口和抽象类有什么区别?你选择使用接口和抽象类的依据是什么?
从形式上看 接口无实现方法只有方法声明 而抽象类有实现方法也有方法声明,从偶合度来说 由于抽象类 有实现的方法 所以容易紧偶合 而接口就比较好的实现了松散偶合。 I. 一般在仅实现单继承用途时, 尽量用基类; 反之使用接口.
II. 如果基类不作为业务对象(在应用时不需要声明其实例), 则尽量声明为抽象类; 否则声明为一般基类.
III. 各个子类如果 公共(重用)代码较多, 建议使用类继承方式, 把公共代码抽象到基类中. 请自行举例说明。
9. What is the different between data Set and data Reader in ADO.NET?
dataset表示一个数据集,是数据在内存中的缓存。 可以包括多个表
DatSet 连接数据库时是非面向连接的。把表全部读到Sql中的缓冲池,并断开于数据库的连接
datareader 连接数据库时是面向连接的。读表时只能向前读取,读完数据后有用户决定是否断开连接。
10. Reverse every word in a String (abc def becomes cba fed). private static string ReverseStringByWord(string input) { if (string.IsNullOrEmpty(input)) throw new ArgumentNullException( char[] sb = new char[input.Length]; int lastIndex = input.Length; while ((lastIndex > 0) && char.IsWhiteSpace(input[lastIndex - 1])) lastIndex--; int appendIndex = 0;
.
for (int i = lastIndex - 1; i >= 0; i--) { char c = input[i]; // if (char.IsWhiteSpace(input[i])) if ( c == \' \' || c == \'\\t\' || c == \'\\n\' || c == \'\\r\' ) { for (int j = i + 1; j < lastIndex; ++j) sb[appendIndex++] = input[j]; sb[appendIndex++] = input[i]; lastIndex = i; } }
for (int j = 0; j < lastIndex; ++j) sb[appendIndex++] = input[j]; return new string(sb); }
四、 程序题(请掌握以下程序中列出的要点)
1.编写一个程序,定义三个float类型的变量,分别从键盘上输入值给它们,然后用if..else选择语句找出它们中的最小(大)数,最后输出结果。
答案略,要求掌握从键盘读入数据的方法,及怎么转换字符串到数值(整型、float/double小数类型),以及如何输出。 2. 编写Test类,包含GetCircumference和GetArea方法,分别求矩形形的周长和面积,Main中根据键盘输入的长和宽,调用两个方法,最终显示结果。 class Program {
static void Main(string[] args) {
Test t = new Test(2, 3);
Console.WriteLine(周长是 Console.WriteLine(面积是 Console.ReadLine(); } }
public class Test {
double width, length;
public Test(double w, double l) {
width = w; length = l;
}
public double GetCircumference() {
return (width + length) * 2; }
public double GetArea() {
return width * length; }
}
本题要求:除了1题所要求外,掌握基本的类、方法编写方法。 3. 使用while语句求1到100中的奇数(偶数)和。 static void Main(string[] args) {
int i=1, sum = 0; while (i <= 100) {
sum += i; i+=2; }
Console.WriteLine(的奇数和为{0} Console.ReadLine(); }
要求掌握while/do..while语句的用法。 4.使用for循环输出九九乘法表。 static void Main(string[] args) {
for (int line = 1; line <= 9; line++) {
for (int i = 1; i <= line; i++) {