Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“你怎么看?”;“转让”;java中的类?_Java - Fatal编程技术网

“你怎么看?”;“转让”;java中的类?

“你怎么看?”;“转让”;java中的类?,java,Java,我正在尝试“导入”(我认为这不是正确的术语)一个类,来自一个类,带有/via 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) {

我正在尝试“导入”(我认为这不是正确的术语)一个类,来自一个类,带有/via 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是不完整的,我有点想让系统在第二节课上打印出来。所以基本上,当我在main中输入“TEST”时,它应该打印“youmanaged to transfer classes!hurrei!:D”,这在另一个类上

这应该有效,你有什么问题吗

if (test.equals("TEST")){
    System.out.print("tranferring class...");
    testclass.main(args);
}

通常你可以在课堂上有一个有意义的方法

public class Testclass
{
    public void printMessage(String msg) {
        System.out.println(msg);
    }
}
然后可以实例化并调用它

Testclass tc = new Testclass ();
tc.printMessage ("say something");

请注意,类名通常以大写字母开头,而方法不

所有公共类都应该在自己的*.java文件中。通常,
main
类是任何程序的入口点,因此通常只有一个。是的,那是单独的文件,我刚刚在这里发布了这两个文件。
testclass.main(args)
(紧跟在
System.out.print(“Transferring class…”)之后;
)-这是在执行一个方法(与“导入”无关)。