2010/03/04

C# week6_1


01/03/2010
  1. inheritance

  2. abstract



  3. inheritance: reuse



    Child class can use parent class.
    Parent class can't use child class.


    class ABC

    {

    protected static int j;

    }

    class XYZ : ABC

    {

    public static void Main()

    {

    Console.WriteLine(j);

    }

    }



A int i;
  int j;

 
Bint k;
  int m;

 
B:Aint i;
  int j;
  int k;
  int m;

 
inheritance
○ B is A  (Fruits is apple and orange. )
× B has A (Car has tire and engine.)

 
Method overriding
Different class and same method name

 
class one
{
public virtual void xyz()
{
}
}
class two : one
{
public override xyz()
{
Console.WriteLine( );
}
}

 
ex.
class Program
{
static void Main(string[] args)
{
Animal a = new Animal();
cat kitty = new cat();
Console.WriteLine(Animal.eyes);
Elephant jambo = new Elephant();
}
}

 
class Animal
{
static public int eyes;
static public int legs;
public int ears;
public virtual void walk()
{
Console.WriteLine("Animal is walking");
}
public Animal()
{
Console.WriteLine("Animal constructor runs");
eyes = 2;
legs = 4;
ears = 2;

 
}
}
class cat : Animal
{
string color;
public override void walk()
{
Console.WriteLine("Cat is walking");
}
public cat()
{
Console.WriteLine("Cat constructor runs");
color = "black";
}

 
}

 
class Elephant : Animal
{
public override void walk()
{
Console.WriteLine("Elephant is walking");
}
public Elephant()
{
Console.WriteLine("Elephant constructor runs");
}
}

 
Result
Animal constructor runs
Animal constructor runs
Cat constructor runs
2
Animal constructor runs
Elephant constructor runs

 

  1. abstract: not complete


    abstract class Animal


    {
    public abstract void Bark()
    }


    Exam

    4 question


    1. Finding bugs inheritance about 400 lines
    2. Writing program
    3. Writing program

    4. Writing program


0 件のコメント:

コメントを投稿

UA-9417263-1