Getting Started with Java Basics-generics (1)

Source: Internet
Author: User

Why do I need to use generics?

1. Code Reuse

2. Guaranteed Type safety

3. Ease of programming


The first part, first of all, talk about code reuse

1) Create a Animal parent class

Package Com.ray.test;public class Animal {private string Name= "Animal";p ublic string GetName () {return name;} public void SetName (String name) {this.name = name;}}

2) Create a subclass of animal cat and dog

Package Com.ray.test;public class Cat extends Animal {private string Name= "Cat";p ublic string GetName () {return name;} public void SetName (String name) {this.name = name;}}

Package Com.ray.test;public Class Dog extends Animal {private string Name= "Dog";p ublic String getName () {return name;} public void SetName (String name) {this.name = name;}}

3) Set up a test class

Before using generics:

Package Com.ray.test;public class Test2 {private Animal animal;private Cat cat;private Dog dog;public Test2 (Animal Animal, Cat cat, Dog dog) {this.animal = Animal;this.cat = Cat;this.dog = Dog;} Public Animal Getanimal () {return Animal;} public void Setanimal (Animal Animal) {this.animal = Animal;} Public Cat Getcat () {return cat;} public void Setcat (cat cat) {this.cat = cat;} Public Dog Getdog () {return dog;} public void Setdog (dog dog) {this.dog = dog;} public static void Main (string[] args) {Test2 test2 = new Test2 (new Animal (), New Cat (), New Dog ()); System.out.println (Test2.getanimal (). toString ()); System.out.println (Test2.getcat (). toString ()); System.out.println (Test2.getdog (). toString ());}}



After using generics:


Package Com.ray.test;public class Test<t> {private T t;public Test (T t) {this.t = T;} Public T Gett () {return t;} public void sett (T t) {this.t = t;} public static void Main (string[] args) {test<animal> Test = new test<animal> (new Animal ()); test<cat> test2 = new test<cat> (new Cat ()); test<dog> test3 = new test<dog> (new Dog ()); System.out.println (Test.gett (). toString ()); System.out.println (Test2.gett (). toString ()); System.out.println (Test3.gett (). toString ());}}

Before and after comparison, the code is very concise, and if later I do not use animal, DOG, cat these classes, but use other classes, we can again reuse test<t> this class


The second part, we talk about type safety

Before using generics, the list can add anything, the following is one example, we notice that the last add of the object, actually with the above those completely different, but no error

This gives us a huge amount of constant use of this list, because it's easy to add a piece of stuff that's not what you wanted in the hands.

Package Com.ray.test;import Java.util.arraylist;import Java.util.list;public class Test2 {public static void main ( String[] args) {List list=new ArrayList (); List.add (new Animal ()); List.add (new Dog ()); List.add (new Cat ()); List.add (new Test2 ());}}

After the use of generics, the last direct error, so that the code during the compilation can identify errors, programming efficiency improved a lot.


Package Com.ray.test;import Java.util.arraylist;import Java.util.list;public class Test2 {public static void main ( String[] args) {list<animal> list=new ArrayList (); List.add (new Animal ()); List.add (new Dog ()); List.add (new Cat () );//The following direct error List.add (new Test2 ());}}

The third part, we carry on the above code to continue to say, generics for programming convenience

Because the type is determined, when we use this list, we can directly invoke the method inside the type, and do not need a strong turn, before 1.4, it is necessary to


Package Com.ray.test;import Java.util.arraylist;import Java.util.list;public class Test2 {public static void main ( String[] args) {list<animal> list=new ArrayList (); List.add (new Animal ()); List.add (new Dog ()); List.add (new Cat () ); for (int i = 0; i < list.size (); i++) {Animal Animal = List.get (i); animal.tostring ();}}}


The first section of the temporary generics is about here, and the next part, please continue.



Getting Started with Java Basics-generics (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.