java中showinfo方法,如何获取Java 8方法引用的MethodInfo?

Please have a look at the following code:

Method methodInfo = MyClass.class.getMethod("myMethod");

This works, but the method name is passed as a string, so this will compile even if myMethod does not exist.

On the other hand, Java 8 introduces a method reference feature. It is checked at compile time. It is possible to use this feature to get method info?

printMethodName(MyClass::myMethod);

Full example:

@FunctionalInterface

private interface Action {

void invoke();

}

private static class MyClass {

public static void myMethod() {

}

}

private static void printMethodName(Action action) {

}

public static void main(String[] args) throws NoSuchMethodException {

// This works, but method name is passed as a string, so this will compile

// even if myMethod does not exist

Method methodInfo = MyClass.class.getMethod("myMethod");

// Here we pass reference to a method. It is somehow possible to

// obtain java.lang.reflect.Method for myMethod inside printMethodName?

printMethodName(MyClass::myMethod);

}

In other words I would like to have a code which is the equivalent of the following C# code:

private static class InnerClass

{

public static void MyMethod()

{

Console.WriteLine("Hello");

}

}

static void PrintMethodName(Action action)

{

// Can I get java.lang.reflect.Method in the same way?

MethodInfo methodInfo = action.GetMethodInfo();

}

static void Main()

{

PrintMethodName(InnerClass.MyMethod);

}

解决方案

No, there is no reliable, supported way to do this. You assign a method reference to an instance of a functional interface, but that instance is cooked up by LambdaMetaFactory, and there is no way to drill into it to find the method you originally bound to.

Lambdas and method references in Java work quite differently than delegates in C#. For some interesting background, read up on invokedynamic.

Other answers and comments here show that it may currently be possible to retrieve the bound method with some additional work, but make sure you understand the caveats.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1.定义一个人的类(属性有名字,年龄,性别。写一个能输出各个属性值的方法showInfo()),定义一个学生类(属性有学号),学生继承人类,要求: (1)父类的属性赋值用构造方法来实现(分别用有参数构造方法和无参数构造方法实现); (2)子类的属性也用构造方法来赋值; (3)在子类重写父类的showInfo()方法 (4)声明学生类的对象,调用学生的显示信息的方法。 2、请编码实现动物世界的继承关系: 动物(Animal)具有行为:吃(eat)、睡觉(sleep)、移动(move) 动物包括:兔子(Rabbit),老虎(Tiger),鹰(eagle) 这些动物吃、移动的行为各不相同(eat,move动作不同);但睡觉的行为是一致的。 3、(1)设计一个表示二维平面上点的类Point,包含有表示坐标位置的protected类型的,成员变量x和y,获取和设置x和y值的public方法。 (2)设计一个表示二维平面上圆的类Circle,它继承自类Point,还包含有表示圆半径的protected类型的成员变量r、获取和设置r值的public方法、计算圆面积的public方法。 (3)设计一个表示圆柱体的类Cylinder,它继承自类Circle,还包含有表示圆柱体高的protected类型的成员变量h、获取和设置h值的public方法、计算圆柱体体积的public方法。 (4)建立Cylinder对象,输出其轴心位置坐标、半径、面积、高及其体积的值。
### 回答1: showinfo方法是自定义的方法,具体使用方法需要看该方法的实现。一般来说,调用showinfo方法需要先创建一个对象,然后通过该对象调用showinfo方法。例如: ``` public class MyClass { public void showinfo() { System.out.println("这是一个showinfo方法"); } } public class Main { public static void main(String[] args) { MyClass myObj = new MyClass(); myObj.showinfo(); } } ``` 在上面的例子,我们先创建了一个MyClass类,其定义了一个showinfo方法。在Main类,我们创建了一个MyClass对象myObj,并通过myObj调用了showinfo方法。运行程序后,会输出“这是一个showinfo方法”。 ### 回答2: Java的showInfo方法实际上是一个自定义方法,它可以用来显示一些信息,例如用户输入错误时的提示信息或者执行某些操作后的反馈信息等等。下面我们来看一下如何在Java使用showInfo方法: 1. 在Java定义showInfo方法Java定义showInfo方法方法很简单:在代码写下如下代码: public static void showInfo(String message) { System.out.println(message); } 其,public表示该方法是公共的;static表示该方法属于类而不是对象;void表示该方法不返回任何内容;而String message则是方法的参数,表示传递给showInfo方法的信息内容。 2. 调用showInfo方法 要调用showInfo方法,需要在代码使用类名.方法名(即:类名.方法名(参数))的格式,例如: ClassName.showInfo("Hello World"); 这里,ClassName表示类名,而showInfo("Hello World")表示对showInfo方法的调用,其参数为字符串Hello World。调用该方法后,程序将会在控制台上显示该字符串。 总的来说,Java的showInfo方法非常简单易用,只需要按照上述步骤定义方法并调用即可。当然,在实际开发,需要根据具体需求设计和优化方法,以更好地满足开发者的需求。 ### 回答3: 在Java,showinfo方法是一种常见的方法,用于显示或打印文本信息或变量的值。showinfo方法通常是用户自定义的,其名称和参数可以根据需要自由定义。 使用showinfo方法,首先需要定义一个类,其包含一个方法名为showinfo的公共方法。下面是一个示例代码: ``` public class MyClass { public void showinfo(String str) { System.out.println("The input string is: " + str); } } ``` 在上面的代码,MyClass是一个类名,其定义了一个名为showinfo的公共方法。该方法接受一个名为str的字符串参数,并在控制台上打印出该字符串。 在主程序,可以实例化这个类,并调用其的showinfo方法。示例代码如下: ``` public static void main(String[] args) { MyClass myObj = new MyClass(); myObj.showinfo("Hello World!"); } ``` 在上面的代码,myObj是MyClass类的一个实例化对象,并且通过对象调用showinfo方法,并将参数“Hello World!”传递给该方法。当程序执行时,该方法将在控制台上打印出“The input string is: Hello World!”。 总之,使用Java的showinfo方法,首先要定义一个类,其包含自定义的showinfo方法。然后,可以实例化该类,并调用其的showinfo方法,并将参数传递给该方法。最终,该方法将执行指定的操作,并将结果输出到控制台上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值