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<? extends animal> set) {
for (ITERATOR< 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<? Extends type 1> called upper limit can be passed to a subclass of type 1 or type 1
* 3<? 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