收集的两个编译的测试题

下面这两个小测试题,从网上收集的,有兴趣的可以试试,

先不要编译测试结果,根据感觉作答:

一:有这样一个类:
                 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));
   } 
}

很有意思的题目,不过第二题,我觉得不太好做,大家可以先猜猜结果,再去编译测试



转载于:https://www.cnblogs.com/bugsharp/archive/2005/11/18/279803.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值