Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package final_exam_04;
- import java.util.*;
- public class MyFruitList<E> extends ArrayList<E>
- {
- public static void main(String[] args)
- {
- MyFruitList<Fruit> fruts = new MyFruitList<Fruit>();
- fruts.add(new Fruit());
- fruts.add(new Fruit());
- MyFruitList<Apple> apps = new MyFruitList<Apple>();
- apps.add(new Apple());
- apps.add(new Apple());
- MyFruitList<GreenApple> gapps = new MyFruitList<GreenApple>();
- gapps.add(new GreenApple());
- gapps.add(new GreenApple());
- System.out.println(fruts.count());
- System.out.println(fruts);
- copyAll(fruts,apps);
- System.out.println(fruts.count());
- System.out.println(fruts);
- copyAll(fruts,gapps);
- System.out.println(fruts.count());
- System.out.println(fruts);
- }
- public int count()
- {
- return this.size();
- }
- public static <E,T> void copyAll(ArrayList<E> a , ArrayList<T> b)
- {
- ArrayList<Object> c = (ArrayList<Object>)a;
- c.addAll(b);
- }
- }
- class Fruit extends Object
- {
- private static int id;
- private int myid;
- Fruit()
- {
- myid = id;
- id++;
- }
- public String toString()
- {
- return "Fruit"+myid;
- }
- }
- class Apple extends Fruit
- {
- private static int id;
- private int myid;
- Apple()
- {
- myid=id;
- id++;
- }
- public String toString()
- {
- return "Apple"+myid;
- }
- }
- class GreenApple extends Apple
- {
- private static int id;
- private int myid;
- GreenApple()
- {
- myid=id;
- id++;
- }
- public String toString()
- {
- return "GreenApple"+myid;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement