2010/04/13

Exam preparation C# Find bug

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;


 

namespace ConsoleApplication1

{


//Exam Practice1 Bug fix


class
Program

{


static
void Main(string[] args)

{


//*Bug* Change to create parent array


//tomykid[] a = new tomykid[3];


 


animal[] a = new
animal[3];


dog d = new
dog("tomy");


tomy t = new
tomy("tomy", "white");


tomykid tk = new
tomykid("tomykid","black");


 


//*Bug* Change to be assigned children objects to parent array


//a[0] = (tomykid)d;


//a[1] = (tomykid)t;


//a[2] = tk;


 

a[0] = (animal)d;

a[1] = (animal)t;

a[2] = (animal)tk;


 


//*Bug* Change from for to foreach


//for(tomykid k:a)


//{


//Console.WriteLine(k.size()+k.bark());


//}


 


foreach (animal ani in a)

{

ani.size();

ani.bark();

}


 

}

}


public
interface
animal

{


//*Bug* Remove interface cannot contain fields


//int i = 10;


 


void bark();


 


//*Bug* Add method size()


void size();

}


public
class
dog : animal

{


private
string name;


 


//*Bug* Add virtual


virtual
public
void bark()

{


Console.WriteLine("dog barks");

}


 


//*Bug* Add virtual


virtual
public
void size()

{


Console.WriteLine("size unknown");

}


 


public dog(string i)

{

name = i;

}


 

}


public
class
tomy : dog

{


string color;


 


//*Bug* Add override


override
public
void bark()

{


Console.WriteLine("Tomy barks");

}


 


//*Bug* Add override


override
public
void size()

{


Console.WriteLine("small size");

}


 


public tomy(string name, string color)

: base(name)

{


this.color = color;

}


 

}


public
class
tomykid : tomy

{


string haircolor;


 


//*Bug* Add override


override
public
void bark()

{


Console.WriteLine("tomykid barks");

}


 


//*Bug* Add override


override
public
void size()

{


Console.WriteLine("medium size");

}


 


//*Bug* Add :base(color)


public tomykid(string name,string color)

: base(name,color)

{

haircolor = color;

}

}

}

0 件のコメント:

コメントを投稿

UA-9417263-1