[英]how do you “transfer” classes in java?
我正在嘗試“導入”(盡管我認為這不是正確的術語。)來自 class 的 class,帶有/通過 IF 語句。 我應該使用什么語法?
我嘗試使用 import package.testclass,但我不知道從那里做什么。
//this is the first class,
import java.util.Scanner;
import package.testclass;
public class Main
{
public static void main(String[] args) {
System.out.println("Enter the word TEST to test if your code block worked.");
Scanner sc = new Scanner(System.in);
String test = sc.nextLine();
if (test.equals("TEST")){
System.out.print("tranferring class...");
}
}
}
//while this is the second class.
public class testclass
{
public static void main(String[] args) {
System.out.println("you managed to transfer classes!
hurrei!:D");
}
}
所以 IF 不完整,我有點想讓系統打印出第二個 class 中的東西。 所以基本上,當我在 main 中輸入“TEST”時,它應該打印“你設法轉移課程:hurrei,:D”,這是另一個 class
這應該有效,你有什么問題嗎?
if (test.equals("TEST")){
System.out.print("tranferring class...");
testclass.main(args);
}
通常你可以在你的 class 中有一個有意義的方法。
public class Testclass
{
public void printMessage(String msg) {
System.out.println(msg);
}
}
然后可以實例化並調用 this
Testclass tc = new Testclass ();
tc.printMessage ("say something");
請注意,class 名稱通常以大寫字母開頭,而方法則不
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.