java中如何调用抽象类方法

发布于 2024-12-25 00:46:07 字数 727 浏览 2 评论 0原文

我想在我自己的类中调用抽象类的方法。抽象类是:

public abstract class Call {

    public Connection getEarliestConnection() {
         Connection earliest = null;

         ...

         return earliest;
    }    
} 

我想调用上面的方法,调用类是:

public class MyActivity extends Activity {

    Connection c = new Connection();

    private void getCallFailedString(Call cal)
    {
        c = cal.getEarliestConnection();

        if (c == null) {
            System.out.println("** no connection**");
        } else {
            System.out.println("** connection");
        }
    }
}

每当我尝试运行上面的类时,它都会在 c = cal.getEarliestConnection() 行上抛出 NullPointerException。谁能告诉我如何解决这个问题?

I want to call a method of an abstract class in my own class. The abstract class is:

public abstract class Call {

    public Connection getEarliestConnection() {
         Connection earliest = null;

         ...

         return earliest;
    }    
} 

I want to call the above method, and the calling class is:

public class MyActivity extends Activity {

    Connection c = new Connection();

    private void getCallFailedString(Call cal)
    {
        c = cal.getEarliestConnection();

        if (c == null) {
            System.out.println("** no connection**");
        } else {
            System.out.println("** connection");
        }
    }
}

Whenever I try to run the above class, it throws a NullPointerException on the line c = cal.getEarliestConnection(). Can anyone tell me how to resolve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

葬﹪忆之殇 2025-01-01 00:46:07

首先,调用一个抽象类,因此不能直接实例化它。您必须创建一个子类,例如 MyCall extends Call,它会覆盖 Call 中的任何抽象方法。

获取 NullPointerException 意味着您作为参数传递给 getCallFailedString() 的任何内容都尚未初始化。因此,在创建 Call 的子类之后,您必须实例化它,然后将其传递给您的方法,因此类似于:

class MyCall extends Call 
{ 
     //override any abstract methods here... 
}

无论您在哪里调用 getCallFailedString() ,都需要上面的内容,例如:

Call cal = new MyCall();
Activity activity = new MyActivity();
activity.getCallFailedString(cal);

Firstly, Call an abstract class, therefore you cannot instantiate it directly. You must create a subclass, say MyCall extends Call which overrides any abstract methods in Call.

Getting a NullPointerException means that whatever you are passing in as an argument to getCallFailedString() hasn't been initialized. So after you create your subclass of Call, you'd have to instantiate it and then pass this in to your method, so something like:

class MyCall extends Call 
{ 
     //override any abstract methods here... 
}

Wherever you are calling getCallFailedString() would then require something above it like:

Call cal = new MyCall();
Activity activity = new MyActivity();
activity.getCallFailedString(cal);
无悔心 2025-01-01 00:46:07

看起来 Call cal 在传递给函数 getCallFailedString 之前为 null。确保扩展Call并实例化扩展类并将其传递给getCallFailedString

Looks like the Call cal is null before it is passed into the function getCallFailedString. Make sure you extend Call and instantiate the extended class and pass it into getCallFailedString.

·深蓝 2025-01-01 00:46:07

确保您的对象“cal”已初始化且不为空。此外,您将无法实例化 Call 对象(作为其抽象类)。相反,将 Call 类声明为接口并在类中实现其方法 getEarliestConnection()。

Make sure your object "cal" is initialized and not null. Also, you won't be able to instantiate a Call object(as its an abstarct class). Instead, declare class Call as an interface and implement its method getEarliestConnection(), in your class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文