一个表达式子类的调用方法

时间:2017-11-09 09:19:25

标签: java class abstract-class parent-child

我有一个名为Router的类,它负责与Retrofit连接。所以在这里是所有的核心方法。然后我有一个名为ConfigurableRouter的抽象类(扩展了Router),它允许我配置我的路由器。现在我希望我可以创建ConfigurabelRouter的子项(实际上它是一个抽象类),具有不同的defults值。

这是一个如何运作的例子:

Router.configure(M_Rout.class)
            .setPath("close-pi")
            .setParams(params)
            .setRequestMethod(Router.RequestMethod.POST)
            .setIsAuthRequested(true)
            .setCallback(new RequestResponse() {
                @Override
                protected void onSuccess(HashMap<String, String> responseItems) {}

                @Override
                protected void onGeneralError(int responseCode) {}

                @Override
                public void onFailure() {}
            })
           .sendRequest(getActivity());

这是Router.configure()方法的工作方式:

public static ConfigurableRouter configure(Class<? extends ConfigurableRouter> aClass){
    ConfigurableRouter configurableRouter = null;
    try {
        configurableRouter = aClass.newInstance();
        //obj is a newly created object of the passed in type
    } catch (Exception ignored) { }
    return configurableRouter;
}

这是ConfigurableRouter方法的一个例子:

public ConfigurableRouter setParams(HashMap<Stthring, Object> params){
    super.setRouterParams(params);
    return this;
}

这是M_Router类:

public class M_Rout extends ConfigurableRouter {
@Override
public String setBasepath() {
    return "www.xxxxxxx.xx/";
}

@Override
public String setInDebigBasePath() {
    return "www.debugxxxxxxx.xx/";
}

@Override
public boolean isDebugging() {
    return false;
}

@Override
public RequestMethod setDefultRequestMethod() {
    return RequestMethod.POST;
}

@Override
public RequestResponse setDefultResponse() {
    return new RequestResponse() {
        @Override
        protected void onSuccess(HashMap<String, String> responseItems) {
            Log.d("RouterLog", "PigSUCSESSSpig");
        }

        @Override
        protected void onGeneralError(int responseCode) {

        }

        @Override
        public void onFailure() {

        }
    };
}

@Override
public ConfigurableRouter setAuthToken(String authToken) {
    return super.setAuthToken("tokenExample");
}

public void setIsAuthRequested(boolean b){
    // 
}
}

现在我的问题是我无法访问第一个片段中的M_Router类中的非重写方法,如setIsAuthRequested()。我不知道我怎么做......尝试不同的方式,但没有。我该怎么办?

1 个答案:

答案 0 :(得分:1)

public abstract class Person {

  abstract void sayName();

}

有两个实现:

public class LoudPerson extends Person {

  void sayName() {
    System.out.println("I yell my name!!");
  }

}

public class RegularPerson extends Person {

  void sayName() {
    System.out.println("I will say my name");
  }

  void givesBusinessCard() {
    // whatever
  }

}

现在,如果你创建一个这样的方法:

public void handlePerson(Person person) {

}

您可以在其上调用sayName()方法,因为无论它是什么类型Person,它都将始终具有sayName()的实现

现在,假设您要传递RegularPerson的实例,并调用givesBusinessCard(),这不会立即生效。

  1. 即使您传递的所有参数都是RegularPerson类型,运行代码的JVM也不会(不能)知道这个
  2. 其他人可以创建其他子类,并改变这种思路。
  3. 就JVM而言,它只是Person,所有Person提供的是sayName()方法。
  4. 假设您需要能够调用givesBusinessCard()方法,您有3种选择。

    1. 更改您调用的方法。如果您需要givesBusinessCard()被呼叫,您知道它是RegularPerson,因此您可以说:

      public void handlePerson(RegularPerson person){

      }

    2. 更改您的抽象类,在那里添加方法,并在LoudPerson

      中提供方法的失败或空实现

      public abstract class Person {

      abstract void sayName();

      abstract void giveBusinessCard();

      }

    3. public class LoudPerson extends Person {
      
        void sayName() {
          System.out.println("I yell my name!!");
        }
      
      void givesBusinessCard() throws UnsupportedOperationException {
        throw new UnsupportedOperationException("not needed here");
      }
      }
      

      或     公共类LoudPerson扩展了人{

        void sayName() {
          System.out.println("I yell my name!!");
        }
      
      void givesBusinessCard()  {
      
      }
      }
      
      1. 在调用之前将您的人员转移到RegularPerson,但请务必进行实例检查:
      2. public void handlePerson(Person person){
          // ..   if(person instanceof RegularPerson){     RegularPerson p =(RegularPerson)人;     p.givesBusinessCard();   }   // .. }

相关问题