01/03/2010
- inheritance
-
abstract
-
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; |
B | int k; |
int m; |
B:A | int 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
-
abstract: not complete
abstract class Animal
{
public abstract void Bark()
}
Exam
4 question
- Finding bugs inheritance about 400 lines
- Writing program
- Writing program
-
Writing program
- Finding bugs inheritance about 400 lines
0 件のコメント:
コメントを投稿