调用另一个类中的方法的 Java 方法。

huangapple go评论78阅读模式
英文:

Java how to call method in another class

问题

抱歉,以下是您要翻译的内容:

抱歉这个问题比较基础但我正在学习刚开始学编程我尝试调用另一个类中的方法但是如果不传递给Cypher类构造函数所需的值我就无法调用它每次我在Menu类中使用Cypher类的方法并且想要调用Cypher类的方法时我真的需要一个新的Cypher类实例吗或者我该如何重写以避免下面的情况

public class Runner {
    private static Cypher c;
    public static void main(String[] args) {

        Menu m = new Menu();
        m.displayMenu();

        //c=new Cypher(3,2);// 除非我添加这一行,否则无法工作
        c.displayCypher("text");


    }
}

public class Cypher {
    private int keyTotalRows;
    private int keyStartRow;


    public Cypher(int key, int offset) throws Exception {


    }


    public void displayCypher(String text) {

        System.out.println("Plain text:" + text);
        System.out.println("Key: Number of Rows:" + keyTotalRows + "\n" + "Key Start Row: " + keyStartRow);

    }
}

public class Menu {
    private Scanner s;
    private void enterKey() {
        s = new Scanner(System.in);
        System.out.println("Enter  key >");

        int key = Integer.parseInt(s.next());

        System.out.println("Enter offset >");

        int offset = Integer.parseInt(s.next());

        System.out.println(" Key:" + key + "\n" + " Offset:" + offset);

    }


    public static void displayMenu() {
        System.out.println("Menu");    
    }
}
英文:

Sorry for the basic question but I'm learning, new to programming. I am trying to call a method that is in another class but I'm unable to without passing in the required values for the constructor of Cypher class. Do i really need a new instance of Cypher class each time i use a method in the Menu class and want to call a method of the Cypher class or how do I rewrite to avoid below.

public class Runner {
private static  Cypher c;
public static void main(String[] args)  {
Menu m = new Menu();
m.displayMenu();
//c=new Cypher(3,2);// wont work unless i add this line
c.displayCypher("text");
}
}
public class Cypher {
private int keyTotalRows;
private int keyStartRow;
public Cypher(int key, int offset) throws Exception {
}
public void displayCypher(String text) {
System.out.println("Plain text:" + text);
System.out.println("Key: Number of Rows:" + keyTotalRows + "\n" + "Key Start Row: " + keyStartRow);
}
}
public class Menu {
private Scanner s;
private void enterKey() {
s = new Scanner(System.in);
System.out.println("Enter  key >");
int key = Integer.parseInt(s.next());
System.out.println("Enter offset >");
int offset = Integer.parseInt(s.next());
System.out.println(" Key:" + key + "\n" + " Offset:" + offset);
}
public static void displayMenu()  {
System.out.println("Menu");	
}

答案1

得分: 2

Few things:. Your methods are private which means you cannot call them outside the class.
change that and you will be able to create your object and call them.

Menu menu = new Menu();
menu.otherMethod();

However as the method calls c which is null you will get a null exception.

You should test for it and then call object c inner methods

if (this.c != null)
{c.displayOtherCypherType(""); }
else
{//do something here}

In order to be able to send Cypher from outside the class use a constructor that receives a Cypher object and assigns it to c or have a public set method that receives it and assigns it

public setCypher(Cypher cypher) 
{
(this.c = cypher);
}

Now If you want to call Cypher methods without instantiating it you can create a static method inside it and call that method.
note that it too should be public or you won't be able to call it

英文:

Few things:. Your methods are private which means you cannot call them outside the class.
change that and you will be able to create your object and call them.

Menu menu=new Menu() ;
menu.otherMethod();

However as the method calls c which is null you will get a null exception.

You should test for it and then call object c inner methods

If (this.c! =null)
{c.displayOtherCypherType("" ) ;}
else
{//do something here}

In order to be able to send Cypher from outside the class use a constructor that receives a Cypher object and assigns it to c or have a public set method that receives it and assigns it

public setCypher(Cypher cypher) 
{
(this.c=cypher) ;
}

Now If you want to call Cypher methods without instantiating it you can create a static method inside it and call Tha method.
note that it to should be public or you won't be able to call it

答案2

得分: 1

你在这里声明了一个Cypher类型的静态变量 c。所以在没有对象声明的情况下,你不能使用 c 变量访问Cypher类的方法。但是你可以从任何类中调用 c 变量,因为它是一个静态变量。但是你的Cypher类没有任何静态方法,所以你不能在没有对象初始化的情况下调用这些方法。
所以你必须声明一个静态方法或者需要创建一个对象来访问Cypher类的方法。

但是如果你声明了一个带初始化的Cypher类型静态变量,那么你可以从任何类中调用 c,并且还可以使用 c 变量引用调用Cypher类的方法,就像这样:

// 在Runner类中声明这个
public static Cypher c = new Cypher();

// 然后在任何类中调用
Runner.c.yourMethod();

祝编码愉快。

英文:

You declare a Cypher type static variable c here. so you can't access the Cypher class method using c variable without object declaration. But you can call the c variable from any class because it's a static variable. But your Cypher class don't have any static Method so you can't call these methods without object initialization.
So you must declare a static method or need to create an object for the access method from Cypher class.

But if you declare Cypher type static variable with initialization. then you can call c from any class and also called Chyper class method using the c variable reference. like:

// Declare this on Runner class
public static Cypher c = new Cypher();
// And then call from any class like 
Runner.c.yourMethod();

Happy Coding.

答案3

得分: 0

如果你不想创建一个实例,你可以将方法设为静态的。

像这样:

public static void displayCypher(String text) {
    System.out.println("明文:" + text);
    System.out.println("密钥:总行数:" + keyTotalRows + "\n" + "密钥起始行:" + keyStartRow);
}

然后你可以在另一个类中调用这个函数:

Cypher.displayCypher();
英文:

If you don't want to create an instance, you can make the method static.

Something like this:

public static void displayCypher(String text) {
System.out.println("Plain text:" + text);
System.out.println("Key: Number of Rows:" + keyTotalRows + "\n" + "Key Start Row: " + keyStartRow);
}

Then you can call this function in another class like

Cypher.displayCypher()

huangapple
  • 本文由 发表于 2020年7月31日 10:55:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63185120.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定