简体   繁体   中英

how to create a type of object that can represent two different types in Java

The title may be confusing, but I will try to explain it with the following example.

I have defined two different types of classes, CDMarker and PDLMarker. In a processing method as below, I create a arraylist for one of the types and use a foreach loop to analyze each one of them.

ArrayList<CDMarker> cdMarkers = new ArrayList<CDMarker>();

......

for (CDMarker marker : cdMarkers) {
   .....
}

Which class to use depends on user input. For example with condition1, CDMarker is the type to use. Condition2, PDLMarker.

Instead of having code like:

if condition1
{
    ArrayList<CDMarker> cdMarkers = new ArrayList<CDMarker>();

    ......

    for (CDMarker marker : cdMarkers) {
       .....
    }
}

if condition2
{
    ArrayList<PDLMarker> pdlMarkers = new ArrayList<PDLMarker>();

    ......

    for (PDLMarker marker : pdlMarkers) {
       .....
    }
}

can I have a fake class (Marker) which can represent either type? Something like:

if condition1
    Marker = CDMarker;
if condition2
    Marker = PDLMarker;

ArrayList<Marker> markers = new ArrayList<Marker>();
for (Marker marker : markers) {
       .....
}

Edit: Looks like interface is the way to go. The problem is that the creation of the List of CDMarker/PDLMarker is done in a method:

if condition1
    List<Marker> markers = writeCDMarker(sheet, dir);
if condition2
    List<Marker> markers = writePDLMarker(sheet, dir);

public static List<PDLMarker> writePDLMarker(HSSFSheet sheet, File dir) {
    List<PDLMarker> pdlMarkers = new ArrayList<PDLMarker>();
    ....
    return pdlMarkers;
}

public static List<CDMarker> writeCDMarker(HSSFSheet sheet, File dir) {
    List<CDMarker> cdMarkers = new ArrayList<CDMarker>();
    ....
    return cdMarkers;
}

Now I got an error at line List<Marker> markers = writePDLMarker(sheet, dir); saying Multiple markers at this line - Type mismatch: cannot convert from List to List - Type mismatch: cannot convert from ArrayList to List

How do I fix this?

Create the following classes and interface:

public interface Marker {
}

public class CDMarker implements Marker {
    ...
}

public class PDLMarker implements Marker {
    ...
}

Use them in the following way:

//Create list
List<Marker> markers = new ArrayList<Marker>();

//Add objects
if(condition1)
    markers.add(new CDMarker());
else if(condition2)
    markers.add(new PDLMarker());

//Access elements
for(Marker m : markers) {
    if(m instanceof CDMarker)
        CDMarker cdMarker = (CDMarker)m;
    else if(m instanceof PDLMarker)
        PDLMarker pdlMarker = (PDLMarker)m;
}

Yes, create a marker class and have CDMarker and PDLMarker inherit from that class. You can then loop through all your Marker objects in the way you listed.

EDIT: Here's a simple example I found. http://examples.javacodegeeks.com/java-basics/java-inheritance-example/

You could have both CDMarker and PDLMarker extends the same super class Marker. Then declare the ArrayList as a superclass ArrayList, then loop through. In the loop, you could have different conditions to invoke different methods on the objects.

Marker marker1;                 //declare as superclass
if condition1
    marker1 = new CDMarker();   //assign to subclass object as needed
if condition2
    marker1 = new PDLMarker();  //assign to subclass object as needed

ArrayList<Marker> markers = new ArrayList<Marker>();
markers.add(marker1);           //add to subclass Arraylist
for (Marker marker : markers) { //loop through subclass Arraylist
       .....
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM