此方法是未定义的对象类型

时间:2022-07-03 20:59:03

I am trying to access the setName method from the WebView class but i get an error when i get an error in the call callSetNameFunction. I create and new webview object and stored it in the object in the webfragment class but doesnt have access to the methods in the webview class. How can i do that. I havent done java for long, still learning.

我试图从WebView类访问setName方法但是当我在调用callSetNameFunction中出现错误时出现错误。我创建了新的webview对象并将其存储在webfragment类的对象中,但是无法访问webview类中的方法。我怎样才能做到这一点。我没有做过很长时间的java,还在学习。

WebView class

public class WebView {

    private String name;

    public WebView (String name) {
        this.name = name;
    }

    public WebView () {
        this.name = name;
    }

    public void setName(String name) {
        if(name != null) {
            this.setName(name);
        } else {
            System.out.println("Thats not his name");
        }
    }

    public String getName() {
        return name;
    }   
}

WebFragment class

public class WebFragment {

    Object myObject;

    public WebFragment() {
        myObject = new WebView();
    }

    public Object getWebView() {
        return myObject; 
    }

    public void callSetNameFunction() {
        myObject.setName();
    }
}

2 个解决方案

#1


2  

3 corrections

  • Object myObject must be WebView myObject;
    If you want to use methods from WebView object, you must declare myObject attribute of WebFragment class as a WebView type if you want to use the methods it declares.
    NOTE: You must change also getter signature to public WebView getWebView() {

    对象myObject必须是WebView myObject;如果要使用WebView对象中的方法,则必须将WebFragment类的myObject属性声明为WebView类型,如果要使用它声明的方法。注意:您还必须将getter签名更改为公共WebView getWebView(){

  • myObject.setName(); should receive a parameter i.e: myObject.setName("name");

    myObject.setName();应该接收一个参数,即:myObject.setName(“name”);

  • Constructor without parameters does not make effect when asign a variable:

    没有参数的构造函数在设置变量时不起作用:

    public WebView () {
        this.name = name;  // Warning: The assignment to variable name has no effect
    }
    

    must be:

    public WebView () {
    }
    

#2


0  

change your methods to following

将您的方法更改为以下

public void setName(String name) {
    if(name != null) {
        //this.setName(name);
       this.name=name;
    } else {
        System.out.println("Thats not his name");
    }
}

and also this :

还有这个:

public void callSetNameFunction() {
    WebView wvob=(WebView)myObject;
    wvob.setName("ola");    
}

setName method is with WebView class.So Object class reference cant find it.

setName方法是用WebView class.So对象类引用找不到它。

#1


2  

3 corrections

  • Object myObject must be WebView myObject;
    If you want to use methods from WebView object, you must declare myObject attribute of WebFragment class as a WebView type if you want to use the methods it declares.
    NOTE: You must change also getter signature to public WebView getWebView() {

    对象myObject必须是WebView myObject;如果要使用WebView对象中的方法,则必须将WebFragment类的myObject属性声明为WebView类型,如果要使用它声明的方法。注意:您还必须将getter签名更改为公共WebView getWebView(){

  • myObject.setName(); should receive a parameter i.e: myObject.setName("name");

    myObject.setName();应该接收一个参数,即:myObject.setName(“name”);

  • Constructor without parameters does not make effect when asign a variable:

    没有参数的构造函数在设置变量时不起作用:

    public WebView () {
        this.name = name;  // Warning: The assignment to variable name has no effect
    }
    

    must be:

    public WebView () {
    }
    

#2


0  

change your methods to following

将您的方法更改为以下

public void setName(String name) {
    if(name != null) {
        //this.setName(name);
       this.name=name;
    } else {
        System.out.println("Thats not his name");
    }
}

and also this :

还有这个:

public void callSetNameFunction() {
    WebView wvob=(WebView)myObject;
    wvob.setName("ola");    
}

setName method is with WebView class.So Object class reference cant find it.

setName方法是用WebView class.So对象类引用找不到它。