很抱歉这个基本问题,但我正在学习,是编程新手。我正在尝试调用另一个类中的方法,但是如果不为Cypher
类的构造函数传递所需的值,我就无法调用。Cypher
每次我在Menu
类中使用方法并想要调用类的方法时,我是否真的需要一个新的类实例,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);// 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");
}