Check attribute of a subclass

后端 未结 3 870
甜味超标
甜味超标 2021-01-26 09:44

I stumbled across that situation but I don\'t know how to handle it the right way:

class Myclass { }
class MyclassWithAwesomeStuff extends Myclass {
    public b         


        
3条回答
  •  逝去的感伤
    2021-01-26 09:52

    Just to get yourself unblocked, you can do the following:

    if (m instanceof MyClassWithAwesomeStuff) {
        if (((MyClassWithAwesomeStuff) m).awesomeStuff) {
    
        }
    }
    

    But, using instanceof defeats the purpose of inheritance and it appears to be a design flaw to have a need to check for this flag for only some objects in list in your code. If you expand the context, probably something better can be suggested.

提交回复
热议问题