I'm very new in Java and I have a small question. I believe it is due to some misunderstanding of the concepts.
So, I have main class menu:
/**
* menu.java
*/
public class menu {
public void run() {
println ("1. Option#1.");
println ("2. Option#2.");
println ("============");
int choose = readInt("Enter a choice:");
if (choose == 1) {
// QUESTION>>>>> // ### how can I call class option1.java here?
}
}
/**
* option1.java
*/
public class option1 {
public void scriepedos () {
setFont("Times New Roman-24");
while (true) {
String str = readLine("Please enter a string: ");
if (str.equals("")) break;
String rev = reverseString(str);
println(rev);
}
}
private String reverseString(String str) {
String result = "";
for (int i=0; i<str.length();i++){
result=str.charAt(i)+result;
}
return result.toLowerCase();
}
}
Many thanks in advance. Leo
You need an instance of option1 to call upon eg
option1 o1 = new option1();
o1.scriepedos();
Alternatively you can make the method static
. That means that you don't need the corresponding instance of the object eg in option1.java
public static void scriepedos () { ...
then in main.java
option1.scriepedos();
The above isn't very OO. You're now making use of the fact that you can have an object encapsulating state etc. and is a much more procedural style.
Notes:
Option1
, Main
You can not call to class. You have to create new object of class and call it's methods like below :
Option1 op1 = new Option1();
// call any Option1 method
op1.scriepedos();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.