Three application scenarios for generic wildcard characters

Source: Internet
Author: User

Package com.java.GeneriationDmeo.www;

Import java.util.Collection;
Import Java.util.HashSet;
Import Java.util.Iterator;

Write an animal's parent class
Class animal{
private String name;
Private String Sex;

Public Animal () {
Super ();
TODO auto-generated Constructor stub
}

Public Animal (string name, string sex) {
Super ();
THIS.name = name;
sex = sex;
}

Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public String Getsex () {
return Sex;
}
public void Setsex (String sex) {
sex = sex;
}
@Override
Public String toString () {
Return "Animal [name=" + name + ", sex=" + Sex + "]";
}

}
Create a dog class to inherit from the parent class
Class Dog extends animal{
private String name;
Private String sex;

Public Dog () {
Super ();
}

Public Dog (string name, string sex) {
Super (name, sex);
This.name=name;
This.sex=sex;
}

Public String GetName () {
return name;
}

public void SetName (String name) {
THIS.name = name;
}

Public String Getsex () {
return sex;
}

public void Setsex (String sex) {
This.sex = sex;
}

@Override
Public String toString () {
Return "Dog [name=" + name + ", sex=" + Sex + "]";
}

}
Create a cat class to inherit the parent class
Class Cat extends animal{
private String name;
Private String sex;

Public Cat () {
Super ();
}
Public Cat (string name, string sex) {
Super (name, sex);
This.name=name;
This.sex=sex;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public String Getsex () {
return sex;
}
public void Setsex (String sex) {
This.sex = sex;
}
@Override
Public String toString () {
Return "Cat [name=" + name + ", sex=" + Sex + "]";
}
}

public class Generiationtest {

public static void Main (string[] args) {
Create a HashSet collection
Hashset<dog> set1=new hashset<dog> ();
Set1.add (New Dog ("Yellow", "female"));
Set1.add (New Dog ("Black", "male"));
Hashset<cat> set2=new hashset<cat> ();
Set2.add (New Cat ("Heart", "female"));
Set2.add (New Cat ("Ice Ice", "female"));
Iterators
Printlist (SET1);
}
/**
* Universal iterator
* @param set
*/
private static void Printlist (HASHSET&LT;? extends animal> set) {
for (ITERATOR&LT; extends animal> it = Set.iterator (); It.hasnext ();) {
System.out.println (It.next ());

}
}
}
/*
*
* When different collection types and different element types are called in the same method, we can pass in wildcards to extend the method
* Wildcard characters are available in three ways
* 1.<?> without any restrictions
* 2&LT;? Extends type 1> called upper limit can be passed to a subclass of type 1 or type 1
* 3&LT;? Super type 1> called lower limit can be passed to the parent class of type 1 or type 1
*
* /

Three applications of generic wildcard characters

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.