试题一
Write the outputs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
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 override void Fun()
{
Console.WriteLine("B.Fun()");
}
static void Main(string[] args)
{
A a = new B();
a.Fun();
Console.Read();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
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 override void Fun()
{
Console.WriteLine("B.Fun()");
}
static void Main(string[] args)
{
A a = new B();
a.Fun();
Console.Read();
}
}
}
A
B
B.Fun()
B
B.Fun()
试题二
Write the outputs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
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(string[] args)
{
B b = new B();
A a = new A();
a.Fun2(b);
b.Fun2(a);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
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(string[] args)
{
B b = new B();
A a = new A();
a.Fun2(b);
b.Fun2(a);
}
}
}
2
5
1
6
5
1
6
试题三
/*
*Write code to output a tree structure for objects in List<Employee>:
*struct Employee
*{
* public int Id;
* public string Name;
* public int MgrId;
*}
* Employee which mgrId is 0 must print as root
*
* For example:
* Assume we have 6 employee objects in list:
* Employee1: {id=1, Name = "Mick", mgrId= 0}
* Employee2: {id=2, Name = "John", mgrId= 1}
* Employee3: {id=3, Name = "Steve", mgrId= 1}
* Employee4: {id=4, Name = "Geogy", mgrId= 2}
* Employee5: {id=5, Name = "Billy", mgrId= 2}
* Employee6: {id=6, Name = "Cindy", mgrId= 3}
*
* The outputs should be:
* Mick
* ----John
* ----Geogy
* -----Cindy
* ----Billy
* ----Steve
*/
*Write code to output a tree structure for objects in List<Employee>:
*struct Employee
*{
* public int Id;
* public string Name;
* public int MgrId;
*}
* Employee which mgrId is 0 must print as root
*
* For example:
* Assume we have 6 employee objects in list:
* Employee1: {id=1, Name = "Mick", mgrId= 0}
* Employee2: {id=2, Name = "John", mgrId= 1}
* Employee3: {id=3, Name = "Steve", mgrId= 1}
* Employee4: {id=4, Name = "Geogy", mgrId= 2}
* Employee5: {id=5, Name = "Billy", mgrId= 2}
* Employee6: {id=6, Name = "Cindy", mgrId= 3}
*
* The outputs should be:
* Mick
* ----John
* ----Geogy
* -----Cindy
* ----Billy
* ----Steve
*/
(在你写出结果前请不要参看答案)
答案
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
struct Employee
{
public int Id;
public string Name;
public int MgrId;
}
class Class2
{
static List<Employee> employees = GetList();
public static void Main(string[] agrs)
{
//output child mgr
for (int i = 0; i < employees.Count; i++)
{
if (employees[i].MgrId == 0) //find the first one
{
Console.WriteLine(employees[i].Name);
WriteChild(employees[i].Id);
break;
}
}
Console.Read();
}
static void WriteChild(int mgrId)
{
for (int i = 0; i < employees.Count; i++)
{
if (employees[i].MgrId==mgrId)
{
for (int j = 1; j < mgrId; j++)
{
Console.Write("\t"); //Print Tab symbol
}
Console.WriteLine("-----" + employees[i].Name);
WriteChild(employees[i].Id);
}
}
}
static List<Employee> GetList()
{
List<Employee> lst = new List<Employee>();
Employee e1 = new Employee();
e1 = new Employee();
e1.Id = 1;
e1.Name = "Mick";
e1.MgrId = 0;
lst.Add(e1);
e1 = new Employee();
e1.Id = 2;
e1.Name = "Jhon";
e1.MgrId = 1;
lst.Add(e1);
e1 = new Employee();
e1.Id = 3;
e1.Name = "Geogy";
e1.MgrId = 2;
lst.Add(e1);
e1 = new Employee();
e1.Id = 4;
e1.Name = "Steve";
e1.MgrId = 1;
lst.Add(e1);
e1 = new Employee();
e1.Id = 5;
e1.Name = "Billy";
e1.MgrId = 2;
lst.Add(e1);
e1 = new Employee();
e1.Id = 6;
e1.Name = "Cindy";
e1.MgrId = 3;
lst.Add(e1);
return lst;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
struct Employee
{
public int Id;
public string Name;
public int MgrId;
}
class Class2
{
static List<Employee> employees = GetList();
public static void Main(string[] agrs)
{
//output child mgr
for (int i = 0; i < employees.Count; i++)
{
if (employees[i].MgrId == 0) //find the first one
{
Console.WriteLine(employees[i].Name);
WriteChild(employees[i].Id);
break;
}
}
Console.Read();
}
static void WriteChild(int mgrId)
{
for (int i = 0; i < employees.Count; i++)
{
if (employees[i].MgrId==mgrId)
{
for (int j = 1; j < mgrId; j++)
{
Console.Write("\t"); //Print Tab symbol
}
Console.WriteLine("-----" + employees[i].Name);
WriteChild(employees[i].Id);
}
}
}
static List<Employee> GetList()
{
List<Employee> lst = new List<Employee>();
Employee e1 = new Employee();
e1 = new Employee();
e1.Id = 1;
e1.Name = "Mick";
e1.MgrId = 0;
lst.Add(e1);
e1 = new Employee();
e1.Id = 2;
e1.Name = "Jhon";
e1.MgrId = 1;
lst.Add(e1);
e1 = new Employee();
e1.Id = 3;
e1.Name = "Geogy";
e1.MgrId = 2;
lst.Add(e1);
e1 = new Employee();
e1.Id = 4;
e1.Name = "Steve";
e1.MgrId = 1;
lst.Add(e1);
e1 = new Employee();
e1.Id = 5;
e1.Name = "Billy";
e1.MgrId = 2;
lst.Add(e1);
e1 = new Employee();
e1.Id = 6;
e1.Name = "Cindy";
e1.MgrId = 3;
lst.Add(e1);
return lst;
}
}
}