Java 如何从另一个类中的类调用方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19570332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call a method from class inside another class
提问by George
My main java code is like that:
我的主要java代码是这样的:
package Javathesis;
//import... etc
//...
public class Javathesis; // My main class
{
public static void // There are a lot of these classes here
//...
//...
class x
{
String a;
String b;
x(String a, String b)
{
this.a = a;
this.b = b;
}
}
public void getAllDataDB1
{
ArrayList<ArrayList<x>> cells = new ArrayList<>();
while(resTablesData1.next())
{
ArrayList<CellValue> row = new ArrayList<>();
for (int k=0; k<colCount ; k++) {
String colName = rsmd.getColumnName(i);
Object o = resTablesData1.getObject(colName);
row.add(new x(rsmd.getColumnType(),o.toString());
}
cells.add(row);
}
public static void main(String[] args)
{
connectToDB1();
// i want to call an instance of class "x" HERE!!!
}
}
How can i call class x in public static void main? Am i doing something wrong? I already test the way i know
如何在 public static void main 中调用类 x?难道我做错了什么?我已经按照我知道的方式测试了
Class class = new Class();
class.x(String a, String b);
but i receive errors. Can somebody help me to fix this? Thanks in advance.
但我收到错误。有人可以帮我解决这个问题吗?提前致谢。
采纳答案by Alex Pakka
There are multiple problems with this code:
这段代码有多个问题:
Class class = new Class();
class.x(String a, String b);
You cannot name a variable 'class', it is a reserved word. Also, x is a class - you cannot just call it, you need to instantiate it.
您不能将变量命名为“类”,它是一个保留字。此外, x 是一个类 - 你不能只是调用它,你需要实例化它。
Also, why would you instantiate a Class - which is a class encapsulating knowledge about Java class?
另外,为什么要实例化一个类——它是一个封装了 Java 类知识的类?
Something like that might work:
像这样的事情可能会奏效:
Javathesis thesis = new Javathesis();
Javathesis.x thesis_x = new thesis.x("a","b);
Also, please, start class names with the capital letter - it is a convention in Java.
另外,请以大写字母开头类名 - 这是 Java 中的约定。
回答by Reimeus
Since x
is an inner class it need an outer class instance to obtain a reference:
由于x
是内部类,因此需要外部类实例来获取引用:
Class x = new Javathesis().new x(a, b);
In addition the semi-colon needs to be removed from the class declaration
此外,需要从类声明中删除分号
Methods in x
can be invoked using the reference created
x
可以使用创建的引用调用in中的方法
Java Naming conventions show that classes start with an uppercaseletter. Also its good to give the class a more meaningful name than a single letter name
Java 命名约定表明类以大写字母开头。给类一个比单个字母名称更有意义的名称也很好
回答by Eel Lee
You should create an instance of your inner class
您应该创建一个内部类的实例
YourInnerClass inner = new YourOuterClass().new YourInnerClass();
and then call a method on it
然后在其上调用一个方法
inner.doMyStuff();
回答by roymcclure
To call a class method without instantiating an object from that class, the method must be declared as static, and then invoked from wherever you want using the class name dot method parentheses arguments.
要调用类方法而不实例化该类中的对象,必须将该方法声明为静态,然后使用类名点方法括号参数从您想要的任何位置调用。
public class x {
公共课 x {
//constructor, etc
//构造函数等
public static int add(int x,y) {
公共静态 int add(int x,y) {
return x+y;
返回 x+y;
}
}
}
}
//make the call from somewhere else
//从其他地方拨打电话
int y = x.add(4,5);
int y = x.add(4,5);