如何访问继承和子类对象的方法

时间:2014-04-30 05:53:33

标签: java oop interface

我有这个:

public interface IDongle {
    public abstract boolean connect();
}

public abstract class DongleManager {

    private boolean connected = false;
    public void setConnected(boolean status) {
        connected = status;
    }
    public boolean getConnected() {
        return connected;
    }
}
public class SimulatedDongle extends DongleManager implements IDongle {
{
    @Override
    public boolean connect() {
        // ...
        return true;
    }
}

public class RealDongle extends DongleManager implements IDongle {
{
    @Override
    public boolean connect() {
        // ...
        return true;
    }
}

我应该如何创建一个SimulatedDongle实例,我可以访问它的所有方法? 例如

DongleManager dongle = new DongleSimulator();
dongle.getConnected();
dongle.connect(); // Erorr!!  

IDongle dongle = new DongleSimulator();
dongle.getConnected(); // Error!!
dongle.connect();

4 个答案:

答案 0 :(得分:2)

DongleManager实现接口IDongle

public abstract class DongleManager implements IDongle {

  private boolean connected = false;
  public void setConnected(boolean status) {
      connected = status;
  }
  public boolean getConnected() {
      return connected;
  }
}

答案 1 :(得分:1)

尝试

DongleSimulator dongle = new DongleSimulator();
dongle.getConnected();
dongle.connect();

<强>更新

如果您想在使用接口时获得灵活性,您应该编写如下代码:

IDongle.java

public interface IDongle {
    public boolean connect();

    public boolean getConnected();

    public void setConnected(boolean status);
}

DongleManager.java

public abstract class DongleManager implements IDongle {

    private boolean connected = false;

    @Override
    public boolean getConnected() {
        return connected;
    }

    @Override
    public void setConnected(boolean status) {
        connected = status;
    }
}

SimulatedDongle.java

public class SimulatedDongle extends DongleManager implements IDongle {
    @Override
    public boolean connect(){
    // ...
    return true;
}

没有错误。

public static void main(String[] args) throws IOException {

    IDongle dongleManager = new SimulatedDongle();
    dongleManager.connect();
    dongleManager.getConnected();
}

答案 2 :(得分:1)

第一种情况,

DongleManager dongle = new SimulatedDongle();
dongle.getConnected();
dongle.connect(); // Erorr!! 

它在connect中失败,因为您只能覆盖父类中定义的方法。在这种情况下,DongleManager没有connect。所以,它失败了。

第二种情况,

IDongle dongle = new SimulatedDongle();
dongle.getConnected(); // Error!!
dongle.connect();

您要将DongleSimulator对象分配给IDongle引用,其中只定义了connect,而不是getConnected。这就是它失败的原因。

我建议您设计系统,例如

interface IDongle {
    public abstract boolean connect();
}

abstract class DongleManager implements IDongle {

    private boolean connected = false;
    public void setConnected(boolean status) {
        connected = status;
    }
    public boolean getConnected() {
        return connected;
    }
}

class SimulatedDongle extends DongleManager {
    @Override
    public boolean connect() {
        return true;
    }
}

class RealDongle extends DongleManager {
    @Override
    public boolean connect() {
        return true;
    }
}

如果查看DongleManager,则会实现IDongle,此类的子级只会扩展DongleManager

Live Demo of the working solution

注意:由于getConnected仅在DongleManager中定义,IDongle界面对此不了解。所以,以下内容仍然会失败。

    IDongle dongle = new SimulatedDongle();
    dongle.getConnected();
    //  error: cannot find symbol
    dongle.connect();

如果您希望这样做,那么您还需要将其包括在IDongle中,就像这样

interface IDongle {
    public abstract boolean connect();
    public boolean getConnected();
}

答案 3 :(得分:1)

两者都不满足所需的方法访问

 Tips of overriding methods

1)In order to execute any code  it has to be passed in 2 phase
a)compile time
b)runtime

At compile time, only reference variable(ie left side of = in your case
DongleManager dongle = new DongleSimulator(); it is dongle of type DongleManager) 
will decide what method has to be called and compiler checks for such methods
inside reference variable type.
if not available then compile time error is thrown.

At runtime ,the object assigned to reference variable(ie right side of =,
in your case DongleManager dongle = new DongleSimulator(); it is DongleSimulator)
will be looking for method inside its type.

it looks for the method overriden inside object (not variable left side of =)

consider below case

    DongleManager dongle = new DongleSimulator();
    dongle.getConnected();
    dongle.connect(); // Erorr!!  

    here dongle.getConnected(); works well because getConnected() method available at compile time with DongleManager.

    dongle.connect(); fails because at compile time itself connect() method is not available with DongleManager so compile time error.

    second scenario

    IDongle dongle = new DongleSimulator();
    dongle.getConnected(); // Error!!
    dongle.connect();

    here
    dongle.getConnected(); // error because IDongle at compile time not having getConnected() method

    dongle.connect();//success because IDongle is having method at compile time and at run time it calls DongleSimulator overriden connect() method.

    so to get all methods as per your requirement. create a hierarchy in such a way it shoud have parent->child1->chid2 so that using parent you can access all parent's methods and child's overriden methods.

so it should be as below

    interface IDongle {
        public abstract boolean connect();
    }

    abstract class DongleManager implements IDongle {

        private boolean connected = false;
        public void setConnected(boolean status) {
            connected = status;
        }
        public boolean getConnected() {
            return connected;
        }
    }

    class SimulatedDongle extends DongleManager {
        @Override
        public boolean connect() {
            return true;
        }
    }

    class RealDongle extends DongleManager {
        @Override
        public boolean connect() {
            return true;
        }
    }

Now interface method is available inside abstract class and hence
using its reference variable we can call that method along with 
its own method as well.