2 changed files with 141 additions and 0 deletions
@ -0,0 +1,116 @@ |
|||||
|
public class TestFruits |
||||
|
{ |
||||
|
public static void main(String[] args) |
||||
|
{ |
||||
|
Apple a = new Apple(2); |
||||
|
System.out.println(Apple.count); |
||||
|
System.out.println(); |
||||
|
|
||||
|
Fruit[] fr = new Fruit[3]; |
||||
|
fr[0] = a; |
||||
|
fr[1] = new RedDelicious(); |
||||
|
fr[2] = new Fruit(4); |
||||
|
System.out.println(); |
||||
|
|
||||
|
System.out.println("Using array fr: "); |
||||
|
for(int i = 0; i < fr.length; i++) |
||||
|
{ |
||||
|
fr[i].display(); |
||||
|
System.out.println(Apple.count); |
||||
|
System.out.println(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
class Fruit |
||||
|
{ |
||||
|
public int f; |
||||
|
|
||||
|
public Fruit(int f) |
||||
|
{ |
||||
|
System.out.println("Fruit"); |
||||
|
this.f = f; |
||||
|
} |
||||
|
|
||||
|
public int getF() |
||||
|
{ |
||||
|
return this.f; |
||||
|
} |
||||
|
|
||||
|
public void f1() |
||||
|
{ |
||||
|
System.out.println("FRUIT f1"); |
||||
|
} |
||||
|
|
||||
|
public void f2() |
||||
|
{ |
||||
|
f1(); |
||||
|
System.out.println("FRUIT f2"); |
||||
|
} |
||||
|
|
||||
|
public void display() |
||||
|
{ |
||||
|
System.out.println("display method from Fruit invoked"); |
||||
|
f2(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
class Apple extends Fruit |
||||
|
{ |
||||
|
public static int count = 0; |
||||
|
|
||||
|
public Apple(int a) |
||||
|
{ |
||||
|
super(a); |
||||
|
System.out.println("Apple"); |
||||
|
count = count + getF(); |
||||
|
} |
||||
|
|
||||
|
public void f1() |
||||
|
{ |
||||
|
System.out.println("Apple f1"); |
||||
|
} |
||||
|
|
||||
|
public void f2() |
||||
|
{ |
||||
|
f1(); |
||||
|
System.out.println("Apple f2"); |
||||
|
super.f1(); |
||||
|
} |
||||
|
|
||||
|
public void display() |
||||
|
{ |
||||
|
System.out.println("display method from Apple invoked"); |
||||
|
f2(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
class RedDelicious extends Apple |
||||
|
{ |
||||
|
public static int count; |
||||
|
|
||||
|
public RedDelicious() |
||||
|
{ |
||||
|
super(3); |
||||
|
System.out.println("RedDelicious"); |
||||
|
count = count + getF(); |
||||
|
} |
||||
|
|
||||
|
public void f1() |
||||
|
{ |
||||
|
System.out.println("RedDelicious f1"); |
||||
|
} |
||||
|
|
||||
|
public void f2() |
||||
|
{ |
||||
|
f1(); |
||||
|
System.out.println("RedDelicious f2"); |
||||
|
super.f1(); |
||||
|
} |
||||
|
|
||||
|
public void display() |
||||
|
{ |
||||
|
System.out.println("display method from RedDelicious invoked"); |
||||
|
f1(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,25 @@ |
|||||
|
Fruit |
||||
|
Apple |
||||
|
2 |
||||
|
|
||||
|
Fruit |
||||
|
Apple |
||||
|
RedDelicious |
||||
|
Fruit |
||||
|
|
||||
|
Using array fr: |
||||
|
display method from Apple invoked |
||||
|
Apple f1 |
||||
|
Apple f2 |
||||
|
FRUIT f1 |
||||
|
5 |
||||
|
|
||||
|
display method from RedDelicious invoked |
||||
|
RedDelicious f1 |
||||
|
5 |
||||
|
|
||||
|
display method from Fruit invoked |
||||
|
FRUIT f1 |
||||
|
FRUIT f2 |
||||
|
5 |
||||
|
|
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue