Java语言怎么定义2个函数,函数的名字只有大小写的区分,当大写的函数不存在的时候,系统调用小写的对应的函数,如果都不存在则不调用
你可以自己修改
String functionNameUpperCase = "Functiona";
来看执行效果. 如有帮助给个采纳谢谢
效果如图:
代码如下 :
import java.lang.reflect.Method;
public class FunctionExample {
public static void main(String[] args) {
String functionNameUpperCase = "Functiona";
// 尝试调用大写函数
if (!invokeMethodIfExists(functionNameUpperCase)) {
// 大写函数不存在时,尝试调用小写函数
String functionNameLowerCase = functionNameUpperCase.toLowerCase();
invokeMethodIfExists(functionNameLowerCase);
}
}
public static boolean invokeMethodIfExists(String methodName) {
try {
Method method = FunctionExample.class.getMethod(methodName);
method.invoke(null);
return true;
} catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException | java.lang.reflect.InvocationTargetException e) {
return false;
}
}
public static void FunctionA() {
System.out.println("调用了大写函数 FunctionA");
}
public static void functiona() {
System.out.println("调用了小写函数 functiona");
}
}
函数命名有规范的(不是强制),但是调用不存在的函数时编译不通过。可以写名称相同但参数不同的多个函数(重载),调用时会根据参数自动匹配合适的函数去执行,有疑问可回复评论
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class TestMethod {
public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, ClassNotFoundException, InstantiationException {
// TODO Auto-generated method stub
// https://blog.csdn.net/gao_chun/article/details/42875575
TestMethod cls = new TestMethod();
Class c = cls.getClass();
// https://blog.csdn.net/ly_xiamu/article/details/82900482
Class<?> clazz= Class.forName("TestMethod");;
try {
Class[] cArg = new Class[2];
cArg[0] = String.class;
cArg[1] = String.class;
// 获取TestMethod类的小写方法add,如果不存在会抛NoSuchMethodException异常,
// 如果有此方法,则会继续往下执行
// https://blog.csdn.net/HaHa_Sir/article/details/124559273
Method dMethod = c.getDeclaredMethod("add",cArg);
System.out.println("方法add存在,调用如下:");
// https://blog.csdn.net/ly_xiamu/article/details/82900482
// 如果小写方法add存在,则使用反射调用此方法
Object obj1 = dMethod.invoke(clazz.newInstance(),"你好,","大家好!");
// 打印调用结果
System.out.println("add调用结果:"+obj1);
}catch(NoSuchMethodException e){
System.out.println(e.getMessage()+",方法add不存在!");
try {
Class[] cArg = new Class[3];
cArg[0] = int.class;
cArg[1] = int.class;
cArg[2] = int.class;
// 如果小写方法add不存在,则判断大写方法Add是否存在
// 同上,如果大写方法add不能存在,则抛NoSuchMethodException异常,
// 如果存在,会继续往下
Method dMethod = c.getDeclaredMethod("Add",cArg);
System.out.println("方法Add存在,调用如下:");
// 使用反射调用大写方法Add
//Object obj1 = dMethod.invoke(clazz.newInstance(),"一,","二,",",三!");
Object obj1 = dMethod.invoke(clazz.newInstance(),2,2,5);
System.out.println("Add调用结果:"+obj1);
}catch(NoSuchMethodException e2){
System.out.println(e2.getMessage()+",方法Add不存在!");
}
}
}
// 模拟小写方法
// public String add(String a,String b) {
// return (a+b);
// }
// 模拟大写方法
public int Add(int a,int b,int c) {
return (a+b+c);
}
}
select * from hw_studentinfo where course = ‘JAVA’ order by score asc limit 1;
优化后问题已经很清晰,没有其他方面需要优化。
下面是我对问题的解答:
解答:
在Java中,函数的名称是区分大小写的,因此我们不能定义仅仅在大小写上有区别的两个函数。如果你定义了一个名为myFunction
的函数,那么你不能再定义一个名为myfunction
的函数。
然而,如果你希望实现一个逻辑,当调用一个不存在的函数时系统会调用对应的小写形式的函数,我们可以通过使用反射来实现。
首先,我们需要创建一个函数调用的中间层,用于处理函数名的大小写。我们可以创建一个类,例如FunctionInvoker
,在这个类中包含两个方法:invoke
和invokeLowerCase
。invoke
方法接受一个函数名作为参数,通过反射来调用对应的函数。invokeLowerCase
方法首先将传入的函数名转换为小写,然后调用invoke
方法来执行对应的函数。
public class FunctionInvoker {
public void invoke(String functionName) {
try {
Method method = this.getClass().getMethod(functionName);
method.invoke(this);
} catch (ReflectiveOperationException ex) {
System.out.println("Function not found");
}
}
public void invokeLowerCase(String functionName) {
invoke(functionName.toLowerCase());
}
public void myFunction() {
System.out.println("Calling myFunction");
}
}
然后,我们可以在main
方法中使用FunctionInvoker
来测试调用函数:
public static void main(String[] args) {
FunctionInvoker invoker = new FunctionInvoker();
invoker.invoke("myFunction"); // Output: Calling myFunction
invoker.invokeLowerCase("myfunction"); // Output: Calling myFunction
invoker.invokeLowerCase("nonexistent"); // Output: Function not found
}
以上代码中,我们首先创建了一个FunctionInvoker
实例,然后分别调用了invoke
和invokeLowerCase
方法来测试函数调用。当传入的函数名与定义的函数名匹配时,对应的函数会被调用;当传入的函数名不匹配时,会输出"Function not found"。
希望以上答案能对您有帮助。如果您有任何其他问题,请随时提问。