简体   繁体   English

如何在Java中使用其他类的set方法?

[英]How to use set method of a different class in java?

How can I set the value of a parameter of class A in class B using the setvarA method of class A ? 如何设置类的参数值AB使用setvarA类的方法A

public class A{

String varA;
public String getvarA() {
        return varA;
    }
public void setvarA(String varA) {
        this.varA=varA;
    }
}

public class B{

String name = "abc";
A objA = new A();
objA.setvarA(name) ; ## THIS LINES THROWS AN ERROR

}

The error is: 错误是:

Syntax error on token "name", VariableDeclaratorId expected after this token and Syntax error on token(s), misplaced constructs
public class A{

    String varA;
    public String getvarA() {
        return varA;
    }
    public void setvarA(String varA) {
        this.varA=varA;
    }
}

class B {
    String name = "abc";
    A objA = new A();

    B() {
        objA.setvarA(name);
    }
}

I'm surprised that you only complain about one error. 我很惊讶您只抱怨一个错误。

There are a number of different errors in the code you posted: 您发布的代码中存在许多不同的错误:

  • It's String , not string (case is important) 它是String ,不是string (大小写很重要)
  • In the method setvarA , this is wrong: this.varA = var.A; 在方法setvarA ,这是错误的: this.varA = var.A; , should have been: this.varA = varA; ,应该是: this.varA = varA;
  • Forgot a semi-colon after String name = "abc" String name = "abc"后忘记分号
  • Forgot () after new A : A objA = new A(); new A之后忘了()A objA = new A();

Revised Code: 修改后的代码:

public class A{

        String varA; //<-String, not string
        public String getVarA() { 
                return varA;
            }
        public void setVarA(String varA) {
                this.varA=varA; //<- var.A not valid
            }
        }

        class B{
          B(){ //<- Need to write this code in a method or constructor
            String name = "abc"; //<- Semi-colon?
            A objA = new A(); //<- what about parenthesis?
            objA.setVarA(name); //<- Semi-colon?
            }
        }

Here's the corrected and complete code: 这是更正和完整的代码:

public class A{

    String varA;

    public String getvarA() {
        return varA;
    }

    public void setvarA(String varA) {
        this.varA = varA;
    }
}

public class B{

    public B(){
        String name = "abc"
        A objA = new A();
        objA.setvarA(name);
    }
}

Both classes must go in separate files, calles A.java and B.java, of course. 当然,这两个类都必须放在单独的文件中,分别称为A.java和B.java。 Add a main method in one of these files, creating B. 在这些文件之一中添加一个main方法,创建B。

It should be A objA = new A(); 应该是A objA = new A();

  • By doing this, you are invoking the constructor of class A which creates the object of class A and assigns the reference variable objA to it . 通过这样做,要调用类的构造函数A它创建类的对象A和参考变量分配objA
  • Also, objA.setvarA(name); 另外, objA.setvarA(name); . You missed semicolon here. 您在这里错过了分号。
  • change your string to String . 将您的string更改为String java is case sensitive. Java区分大小写。 String is a class. String是一个类。
  • change this.varA=var.A; 更改this.varA=var.A; to this.varA = varA; this.varA = varA;
  • Missing semi colon here String name = "abc" . 此处缺少半冒号String name = "abc" Change to String name = "abc"; 更改为String name = "abc";
  • You have put extra semi colon in setter method. 您已经在setter方法中添加了多余的半冒号。 Please correct that. 请更正。
  • Also, you need to call the method from some init block or constructor or a method 另外,您需要从一些init块或构造函数或方法中调用该方法

    B() { objA.setvarA(name); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java-如何告诉对象使用不同方法中的一组参数 - Java - how to tell an object to use a set of parameters from a different method 我如何在同一个 class 的不同方法中使用私有 static 方法(对象的扩展)的返回值? (爪哇) - How can i use the return value of a private static method (extension of object) in a different method in that same class? (java) Java 使用来自不同类中调用的不同方法的 var 值 - Java use var value from a different method that is called in a different class 如何在java中的不同类文件中运行方法 - How to run a method in a different class file in java 如何访问Java中不同类中方法的变量? - How to access variables of a method in a different class in Java? Java-如何在不同的类中调用add()方法 - Java - how to call an add() method in a different class 如何在其他类中使用方法? - How can I use a method in a different class? 如何在不同的类中使用特定于子类的方法? - how to use a subclass specific method in a different class? 如何从Java中的方法中选择多个不同类型的值并在主类中使用它? - How to multiple different types of values from a method in java and use it in the main class? Java 设计模式用于使用抽象 class 作为参数的方法的不同实现 - Java design pattern for use different implementation of method with an abstract class as param
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM