I want to call class2 from class1 but class2 doesn't have a main function to refer to like Class2.main(args);
我想从class1调用class2,但class2没有一个main函数来引用类似Class2.main(args);
5 个解决方案
#1
14
Suposse you have
你有的
Class1
1类
public class Class1 {
//Your class code above
}
Class2
等级2
public class Class2 {
}
and then you can use Class2 in different ways.
然后你可以用不同的方式使用Class2。
Class Field
类场
public class Class1{
private Class2 class2 = new Class2();
}
Method field
方法字段
public class Class1 {
public void loginAs(String username, String password)
{
Class2 class2 = new Class2();
class2.invokeSomeMethod();
//your actual code
}
}
Static methods from Class2 Imagine this is your class2.
Class2中的静态方法想象一下这是你的class2。
public class Class2 {
public static void doSomething(){
}
}
from class1 you can use doSomething from Class2 whenever you want
从class1开始,你可以随时使用Class2中的doSomething
public class Class1 {
public void loginAs(String username, String password)
{
Class2.doSomething();
//your actual code
}
}
#2
2
Simply create an instance of Class2
and call the desired method.
只需创建Class2的实例并调用所需的方法。
Suggested reading: http://docs.oracle.com/javase/tutorial/java/javaOO/
推荐阅读:http://docs.oracle.com/javase/tutorial/java/javaOO/
#3
2
If your class2 looks like this having static members
如果你的class2看起来像这样有静态成员
public class2
{
static int var = 1;
public static void myMethod()
{
// some code
}
}
Then you can simply call them like
然后你可以简单地称他们为
class2.myMethod();
class2.var = 1;
If you want to access non-static members then you would have to instantiate an object.
如果要访问非静态成员,则必须实例化对象。
class2 object = new class2();
object.myMethod(); // non static method
object.var = 1; // non static variable
#4
1
First create an object of class2 in class1 and then use that object to call any function of class2 for example write this in class1
首先在class1中创建class2的对象,然后使用该对象调用class2的任何函数,例如在class1中编写它
class2 obj= new class2();
obj.thefunctioname(args);
#5
1
Class2 class2 = new Class2();
Instead of calling the main, perhaps you should call individual methods where and when you need them.
也许您应该在需要的地方和时间调用单独的方法,而不是调用main。
#1
14
Suposse you have
你有的
Class1
1类
public class Class1 {
//Your class code above
}
Class2
等级2
public class Class2 {
}
and then you can use Class2 in different ways.
然后你可以用不同的方式使用Class2。
Class Field
类场
public class Class1{
private Class2 class2 = new Class2();
}
Method field
方法字段
public class Class1 {
public void loginAs(String username, String password)
{
Class2 class2 = new Class2();
class2.invokeSomeMethod();
//your actual code
}
}
Static methods from Class2 Imagine this is your class2.
Class2中的静态方法想象一下这是你的class2。
public class Class2 {
public static void doSomething(){
}
}
from class1 you can use doSomething from Class2 whenever you want
从class1开始,你可以随时使用Class2中的doSomething
public class Class1 {
public void loginAs(String username, String password)
{
Class2.doSomething();
//your actual code
}
}
#2
2
Simply create an instance of Class2
and call the desired method.
只需创建Class2的实例并调用所需的方法。
Suggested reading: http://docs.oracle.com/javase/tutorial/java/javaOO/
推荐阅读:http://docs.oracle.com/javase/tutorial/java/javaOO/
#3
2
If your class2 looks like this having static members
如果你的class2看起来像这样有静态成员
public class2
{
static int var = 1;
public static void myMethod()
{
// some code
}
}
Then you can simply call them like
然后你可以简单地称他们为
class2.myMethod();
class2.var = 1;
If you want to access non-static members then you would have to instantiate an object.
如果要访问非静态成员,则必须实例化对象。
class2 object = new class2();
object.myMethod(); // non static method
object.var = 1; // non static variable
#4
1
First create an object of class2 in class1 and then use that object to call any function of class2 for example write this in class1
首先在class1中创建class2的对象,然后使用该对象调用class2的任何函数,例如在class1中编写它
class2 obj= new class2();
obj.thefunctioname(args);
#5
1
Class2 class2 = new Class2();
Instead of calling the main, perhaps you should call individual methods where and when you need them.
也许您应该在需要的地方和时间调用单独的方法,而不是调用main。