先不要编译测试结果,根据感觉作答:
一:有这样一个类:
class T
{
protected string aaa;
public T(string val)
{ aaa = val; }
public string GetString(T t)
{ return t.aaa; }
}
和以下两种用法:
1):
T t1 = new T("test");
Console.WriteLine(t1.GetString(t1));
2):
T t1 = new T("test1");
T t2 = new T("test2");
Console.WriteLine(t1.GetString(t2));
请选择答案并说明理由:
a) 编译不通过
b)第一种用法不能通过
c)第二种用法不能通过
d)都能通过
二:写出控制台输出的结果
using
System;
class Base
{
private int m = 2 ;
public int GetM(Child c)
{
return c.m;
}
public int GetM(Base b)
{
return b.m;
}
public Base()
{}
public Base( int m)
{
this .m = m;
}
}
class Child : Base
{
private int m = 1 ;
public new int GetM(Child c)
{
return c.m;
}
public Child( int m): base (m)
{ this .m = 4 ; }
}
class test
{
public static void Main()
{
Base b = new Base();
Child c = new Child( 3 );
Console.WriteLine(b.GetM(c));
Console.WriteLine(c.GetM(c));
Console.WriteLine(b.GetM((Base)c));
}
}
class Base
{
private int m = 2 ;
public int GetM(Child c)
{
return c.m;
}
public int GetM(Base b)
{
return b.m;
}
public Base()
{}
public Base( int m)
{
this .m = m;
}
}
class Child : Base
{
private int m = 1 ;
public new int GetM(Child c)
{
return c.m;
}
public Child( int m): base (m)
{ this .m = 4 ; }
}
class test
{
public static void Main()
{
Base b = new Base();
Child c = new Child( 3 );
Console.WriteLine(b.GetM(c));
Console.WriteLine(c.GetM(c));
Console.WriteLine(b.GetM((Base)c));
}
}
很有意思的题目,不过第二题,我觉得不太好做,大家可以先猜猜结果,再去编译测试