我怎样才能使用;对象类“;在java中调用用户定义类的方法
在这两个类中,我通过main方法在TestClass的getName方法中传递了testclass2的对象,现在我想使用对象obj调用getTwoName方法。谁能帮我一下吗。谢谢 //下面是代码 //第一类我怎样才能使用;对象类“;在java中调用用户定义类的方法,java,Java,在这两个类中,我通过main方法在TestClass的getName方法中传递了testclass2的对象,现在我想使用对象obj调用getTwoName方法。谁能帮我一下吗。谢谢 //下面是代码 //第一类 package Test; public class TestClassTwo { public static String getTwoName() { return "2nd"; } } //类别2 包装试验 公共类TestClass{
package Test;
public class TestClassTwo {
public static String getTwoName()
{
return "2nd";
}
}
//类别2
包装试验
公共类TestClass{
public void getName(Object obj) throws InstantiationException, IllegalAccessException, ClassNotFoundException
{
// call getTwoName method of TestClassTwo using obj object
}
public static void main(String args[]) throws InstantiationException, IllegalAccessException, ClassNotFoundException
{
TestClass tc=new TestClass();
tc.getName(new TestClassTwo());
}
}将
对象
(超类)强制转换为TestClassTwo
(子类):
或:
请注意,使用实例调用静态方法是无用的。相反,使用类名调用静态方法:
String name = TestClassTwo.getTwoName();
为什么??您不需要使用任何对象,因为该方法是静态的。使用对象调用此方法是徒劳的。只需编写
TestClassTwo.gettwName()
[我强烈怀疑你的问题有问题。]
如果您确实需要通过未知类的对象访问该类的静态方法,请按照以下方式操作:
String twoName = obj.getClass().getMethod("getTwoName").invoke(null);
正如其他人所说,如果您打算静态访问该方法,则不需要实例,因此在
TestClass#getName
中根本不需要参数。但是,如果确实希望它成为实例方法,可以执行以下三项操作之一:
1)在TestClass\getName
中输入类型testclass2
:
public class TestClass {
public void getName(TestClassTwo obj) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
String name = obj.getTwoName();
// Do something with 'name'
}
public static void main(String args[]) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
TestClass tc=new TestClass();
tc.getName(new TestClassTwo());
}
}
public class TestClass {
public void getName(Object obj) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
if (obj instanceof TestClassTwo) {
TestClassTwo two = (TestClassTwo) obj;
String name = two.getTwoName();
// Do something with 'name'
} else {
// Handle failure accordingly (throw an exception, log an error, do nothing, etc.)
}
}
public static void main(String args[]) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
TestClass tc=new TestClass();
tc.getName(new TestClassTwo());
}
}
public interface HasTwoName {
public String getTwoName();
}
public class TestClassTwo implements HasTwoName {
@Override
public String getTwoName() {
return "2nd";
}
}
public class TestClass {
public void getName(HasTwoName obj) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
String name = two.getTwoName();
// Do something with 'name'
}
public static void main(String args[]) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
TestClass tc=new TestClass();
tc.getName(new TestClassTwo());
}
}
2)将对象强制转换为
TestClassTwo
的实例,检查类型:
public class TestClass {
public void getName(TestClassTwo obj) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
String name = obj.getTwoName();
// Do something with 'name'
}
public static void main(String args[]) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
TestClass tc=new TestClass();
tc.getName(new TestClassTwo());
}
}
public class TestClass {
public void getName(Object obj) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
if (obj instanceof TestClassTwo) {
TestClassTwo two = (TestClassTwo) obj;
String name = two.getTwoName();
// Do something with 'name'
} else {
// Handle failure accordingly (throw an exception, log an error, do nothing, etc.)
}
}
public static void main(String args[]) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
TestClass tc=new TestClass();
tc.getName(new TestClassTwo());
}
}
public interface HasTwoName {
public String getTwoName();
}
public class TestClassTwo implements HasTwoName {
@Override
public String getTwoName() {
return "2nd";
}
}
public class TestClass {
public void getName(HasTwoName obj) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
String name = two.getTwoName();
// Do something with 'name'
}
public static void main(String args[]) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
TestClass tc=new TestClass();
tc.getName(new TestClassTwo());
}
}
3)如果希望允许其他类具有
gettwName()
函数,请定义一个接口,并将该接口的实例作为测试类#getName
的参数:
public class TestClass {
public void getName(TestClassTwo obj) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
String name = obj.getTwoName();
// Do something with 'name'
}
public static void main(String args[]) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
TestClass tc=new TestClass();
tc.getName(new TestClassTwo());
}
}
public class TestClass {
public void getName(Object obj) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
if (obj instanceof TestClassTwo) {
TestClassTwo two = (TestClassTwo) obj;
String name = two.getTwoName();
// Do something with 'name'
} else {
// Handle failure accordingly (throw an exception, log an error, do nothing, etc.)
}
}
public static void main(String args[]) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
TestClass tc=new TestClass();
tc.getName(new TestClassTwo());
}
}
public interface HasTwoName {
public String getTwoName();
}
public class TestClassTwo implements HasTwoName {
@Override
public String getTwoName() {
return "2nd";
}
}
public class TestClass {
public void getName(HasTwoName obj) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
String name = two.getTwoName();
// Do something with 'name'
}
public static void main(String args[]) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
TestClass tc=new TestClass();
tc.getName(new TestClassTwo());
}
}
你说得对,我解释不清楚。问题是,getTwoName()方法存在于许多不同的类中。所以,我试图通过简单地传递该类的类名或对象来访问该方法。@MayurMaisuria在这种情况下,您需要根据我的编辑进行反射。