java中,怎么在一个方法里面获得调用此方法的对象

java中,怎么在一个方法里面获得调用此方法的对象,第1张

看谁的方法

如果是普通方法,就是用this

如果是static标识的,那就用当前类

如果是native,那你得找jni关联的c代码

我看你的意思,应该使用接口设计。

public interface Super{

void getDetail();

}

public class Sub implements Super{

public void getDetail(){具体方法}

}

这样的话,使用的时候使用接口就好了,具体实现类的方法。

class类有newInstance() 这个方法就可以用来生成实例。参考下面代码:

public class Demo {

private String key1 = "1";

private String key2 = "2";

public String getKey1() {

return key1;

}

public void setKey1(String key1) {

thiskey1 = key1;

}

public String getKey2() {

return key2;

}

public void setKey2(String key2) {

thiskey2 = key2;

}

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

//参数 “Demo” 是类的全名,如果在包结构下,要有完整包路径 比如: comtestDemo

Class<> clazz = ClassforName("Demo");

//“Demo”类必须有默认构造方法,否则会抛出异常

Demo demo = (Demo) clazznewInstance();

Systemoutprintln(demogetKey1());

Systemoutprintln(demogetKey2());

}

}

如题:

如果是使用 判断来验证的话 可以 使用

instanceof 关键字来做比较。

在java中 instanceof 归类为 比较运算符(特殊的)

是用来 判断 某个 对象 是否是某个类的实例(所谓的实例 就是指,这个对象 是 由 这个类 或这个类的子类 new 出来的)

如: 你自己定义了一个类 Person类,而你用这个类new(创建) 了一个对象p ( Person p=new Person() ) ,则 这个 时候 boolean flag=p instanceof Person ;

的结果是 true;

注意 instanceof 左边的事对象,右边的事类名。

instanceof 关键字 的用法 基础的就是这样。

当然 如果 你比较的对象 不是 这个类 的实例,则结果当然是false了。

当时,此时 要注意了。

java中的类是可继承的。(在涉及到继承时候 用instanceof 就有意思了,要注意这里的情况)

如:

你写了 一个类 Student 继承了Person 类。

就像这样 class Student extends Person

那么 这个时候

Student 类的实例 如:Student stu=new Student();

此时 如果 你使用 stu instanceof Student ,那么 ,结果 当如是true的。

是吧?这个不会 有疑问吧? 有疑问 的话, 下面的就没法看了。

接着,再看这个:

Student stu1=new Student();

boolean result=stu1 instanceof Person;

那么 result 的结果 是true 还是 false呢?

结果是 true;

这 就是 在继承结构下,instanceof 要注意的地方。

我们 可以这样理解

Person p1=new Person();

p1 instanceof Person ---true ==>人(p1)是(Person)人类的 实例

Student stu2=new Student();

stu2 instanceof Student;---->true; ==>学生(stu2)是学生类(Student)的实例

stu2 instanceof Person --->true ==>学生也是人(继承结构)

补充 :

写的,不正确了。

应该写成

if(某变量 instanceof Integer){

//do some

}else if(某某变量 instanceof Float){

//do some

}

Integer 是int的包装类

Float 是float的包装类

不知 是不是你想要的答案。

可以通过“ThreadcurrentThread()getStackTrace()”,来获得当前调用堆栈。从而获得所需要的调用类及方法。

package demo;

public class Main {

public static void main(String[] args) {

//JFinalstart("webRoot", 8080, "/");

Main m = new Main();

ma1();

}

public void a1(){

new A()a();

}

}

class A{

public void a(){

new B()b();

}

}

class B{

public void b(){

new C()c();

}

}

class C{

public void c(){

StackTraceElement[] es = ThreadcurrentThread()getStackTrace();

for(StackTraceElement e : es){

Systemoutprintln(egetClassName());

}

}

}

运行结果:

局部变量在方法栈帧中根本没有名字,只有偏移地址。变量名是给程序员看的。

如果你了解了这点,你就知道你的要求是从原理上不可能实现的。

当然,你可以封装一个类:

class MyArray {

private String name;

public double[] array;

public MyArray(String name, double[] array){

thisname = name;

thisarray = array;

}

public String toString() {

return thisname + "的内容是:" + thisarray;

}

}

然后输出这个类的对象:

MyArray a = new MyArray("array1", new double[]{01, 02, 03});

Systemoutprint(a);

import javalangreflectInvocationTargetException;

import javalangreflectMethod;

public class Xx {

/

@param args

/

public static void main(String[] args) {

//所有public方法

Method[] ms = AclassgetMethods();

for (int i = 0; i < mslength; i++) {

if(ms[i]getName()startsWith("get"))

//get方法

;

}

try {

//set方法

Method setMethod = AclassgetMethod("setXxx", Objectclass);

//set值,根据具体参数类型来,可多个

Object setValue = new Object();

setMethodinvoke(new A(), setValue);

} catch (SecurityException e) {

// TODO Auto-generated catch block

eprintStackTrace();

} catch (NoSuchMethodException e) {

// TODO Auto-generated catch block

eprintStackTrace();

} catch (IllegalArgumentException e) {

// TODO Auto-generated catch block

eprintStackTrace();

} catch (IllegalAccessException e) {

// TODO Auto-generated catch block

eprintStackTrace();

} catch (InvocationTargetException e) {

// TODO Auto-generated catch block

eprintStackTrace();

}

}

}

class A {

}

以上就是关于java中,怎么在一个方法里面获得调用此方法的对象全部的内容,包括:java中,怎么在一个方法里面获得调用此方法的对象、java 如何只知类名和方法名得到对象和执行方法、java 怎么通过类对象来获得实例的等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9394730.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-27
下一篇2023-04-27

发表评论

登录后才能评论

评论列表(0条)

    保存