How to handle polymorphism with JSF2?

前端 未结 2 1898
闹比i
闹比i 2021-01-06 02:41

I need to display/edit polymorphic entities.

My abstract class is Person. My concrete classes are PhysicalPerson and MoralP

2条回答
  •  执笔经年
    2021-01-06 03:20

    Another way is to create an abstract method in a base class, which will return you some mark of what instance you have, and implement it in your subclasses, like this:

    public abstract class Person {
    
    public abstract boolean isPhysical();
    
    }
    
    public PhysicalPerson extends Person {
    
    public boolean isPhysical() {
         return true;
    }
    
    }
    

    and then in jsf:

    
        

    this is a PhysicalPerson.

    this is a Moral Person.

    However the class checking approach is more universal.

提交回复
热议问题