Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在同一类中继承方法?JAVA_Java_Inheritance - Fatal编程技术网

如何在同一类中继承方法?JAVA

如何在同一类中继承方法?JAVA,java,inheritance,Java,Inheritance,我想在同一个类中继承一个方法。我已经创建了一个connection(),我想从remotecmnd()方法继承它。我已经使用了this.connection(),但仍然没有得到它。在connection()中,我声明了socket变量和dataoutputstream变量。但是remotecmnd()不能继承它。那我该怎么办呢 这是我的remotecmnd(): 我的连接方式: public void connection(){ try { Socket cs = new Socket(

我想在同一个类中继承一个方法。我已经创建了一个
connection()
,我想从
remotecmnd()
方法继承它。我已经使用了
this.connection()
,但仍然没有得到它。在
connection()
中,我声明了socket变量和dataoutputstream变量。但是
remotecmnd()
不能继承它。那我该怎么办呢

这是我的remotecmnd():

我的连接方式:

public void connection(){
try {
    Socket cs = new Socket("192.168.1.100", 8002);
    DataOutputStream out = new DataOutputStream(cs.getOutputStream());
    if(cs.isConnected())
        Toast.makeText(MainActivity.this, "Connected to Server", Toast.LENGTH_LONG).show();

} catch (UnknownHostException er) {
    Toast.makeText(MainActivity.this, er.getMessage(), Toast.LENGTH_LONG).show();
} catch (IOException er) {
    Toast.makeText(MainActivity.this, er.getMessage(), Toast.LENGTH_LONG).show();
}
}

这两个方法在同一个类中。

您需要从
remoteCommand()
调用该方法,并在方法外部声明一个变量
out

此代码将返回
out

public DataOutputStream connection(){
    DataOutputStream out = null;
    try {
        Socket cs = new Socket("192.168.1.100", 8002);
        out = new DataOutputStream(cs.getOutputStream());
        if(cs.isConnected())
            Toast.makeText(MainActivity.this, "Connected to Server", Toast.LENGTH_LONG).show();

    } catch (UnknownHostException er) {
        Toast.makeText(MainActivity.this, er.getMessage(), Toast.LENGTH_LONG).show();
    } catch (IOException er) {
        Toast.makeText(MainActivity.this, er.getMessage(), Toast.LENGTH_LONG).show();
    }
    return out;
}

不能重写同一类中的方法。 重写总是需要继承关系,其中子类型(类/接口)继承父类型(类/接口)中存在的方法,并通过维护相同的签名进行相应的修改。 例如:

class A
{
public void method1()
{
...
...   //some definition

}

}


class B extends A
{
@Override
public void method1()   
{
.....
.....//same or different definition as in parent class A

}


}

这不是继承。您正在询问如何将被调用方法的局部变量转移到调用方法的作用域中,这是不可能的

通过将其设置为字段,可以允许两种方法使用相同的变量

private DataOutputStream out;

public void remotecmnd(){
    this.connection();
    // access DataOutputStream using this.out or out here
    ...
}

public void connection(){
    try {
        Socket cs = new Socket("192.168.1.100", 8002);
        out = new DataOutputStream(cs.getOutputStream());
        ...
但是,您需要注意这样一个事实:如果在
连接
方法中捕获异常,则字段可能仍然是
null
,这可能会导致
null点异常
。您需要某种方法在
remotecmnd
方法中对此进行检查,例如,在
connection
中不捕获异常,或者重新搜索异常


另一个选项是从
连接
方法返回
数据输出流

public void remotecmnd(){
    final DataOutputStream out = this.connection();
    ...
}

public DataOutputStream connection() {
     ...
     return out;

在类级别声明out变量,并在连接内部实例化它。这会奏效的。我认为您已经在类级别声明了变量,但创建了另一个内部连接方法。 用此方法替换线路连接方法

out = new DataOutputStream(cs.getOutputStream());

“我想在同一个类中继承一个方法”-我确信您真正想做的是其他事情。你想达到什么目标
out
是一个局部变量。我认为您要么想使它成为类的成员,要么从
connection()
方法返回它。这是一个简单的方法调用,应该可以工作。你确定没有人叫它吗。请尝试调试代码并通知我们。@Pramod错误为
错误:找不到符号变量。
@Axel对此表示抱歉。我想在remotecmnd方法中使用connection方法中的变量:/Thank,但它不起作用,仍然显示
找不到符号变量输出
您确定已建立连接吗?添加日志并检查。只能在连接方法中访问变量'out'。所以在方法外部和classits内部声明不是我想要的解决方案。您能告诉我,如何返回out变量吗?任何时候:-)在堆栈溢出中,我们不会接受感谢,只接受投票和接受的答案:-)如果答案有效,请接受答案。:-)
out = new DataOutputStream(cs.getOutputStream());