女刊瘦美人官网,查泰莱夫人的情人qvod,大学生当兵待遇
本文实例讲述了android基于反射技术实现的加减乘除运算。分享给大家供大家参考,具体如下:
java反射机制定义:
java反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法;这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。
java反射机制主要提供了以下功能: 在运行时判断任意一个对象所属的类;在运行时构造任意一个类的对象;在运行时判断任意一个类所具有的成员变量和方法;在运行时调用任意一个对象的方法;生成动态代理。
有时候我们说某个语言具有很强的动态性,有时候我们会区分动态和静态的不同技术与作法。我们朗朗上口动态绑定(dynamic binding)、动态链接(dynamic linking)、动态加载(dynamic loading)等。然而“动态”一词其实没有绝对而普遍适用的严格定义,有时候甚至像对象导向当初被导入编程领域一样,一人一把号,各吹各的调。
一般而言,开发者社群说到动态语言,大致认同的一个定义是:“程序运行时,允许改变程序结构或变量类型,这种语言称为动态语言”。从这个观点看,perl,python,ruby是动态语言,c++,java,c#不是动态语言。
尽管在这样的定义与分类下java不是动态语言,它却有着一个非常突出的动态相关机制:reflection。这个字的意思是 “反射、映象、倒影”,用在java身上指的是我们可以于运行时加载、探知、使用编译期间完全未知的classes。换句话说,java程序可以加载一个 运行时才得知名称的class,获悉其完整构造(但不包括methods定义),并生成其对象实体、或对其fields设值、或唤起其methods1。 这种“看透class”的能力(the ability of the program to examine itself)被称为introspection(内省、内观、反省)。reflection和introspection是常被并提的两个术语。
以上摘录自百度百科,在android 中有很多类是被封闭的,比如 servicemanager 蓝牙模块更是有n多个类被android 隐藏不开放,要调用这些类必须使用java 的反射技术将类转为对象进行操作.android 应用也是基于java 语言为基础,当然也具备反射这一技术,下面我写了一个demo 是如何通过反射技术调用类名方法并完成一个加减乘除的记算器。
首先我们定义一个类,此为只是简单的定义几个方法,即加减乘除四个方法,代码如下:
class operationclass { public float add(int parm1, int parm2) { return parm1 + parm2; } public float cut(int parm1, int parm2) { return parm1 - parm2; } public float ride(int parm1, int parm2) { return parm1 * parm2; } public float except(int parm1, int parm2) { return parm1 / parm2; } }
界面布局文件代码
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:orientation="vertical" android:layout_height="fill_parent"> <edittext android:id="@+id/edittext01" android:layout_width="fill_parent" android:layout_height="wrap_content"></edittext> <edittext android:id="@+id/edittext02" android:layout_width="fill_parent" android:layout_height="wrap_content"></edittext> <textview android:id="@+id/textview01" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content"></textview> <linearlayout android:id="@+id/linearlayout01" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <button android:text="+" android:id="@+id/button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></button> <button android:text="-" android:id="@+id/button02" android:layout_width="wrap_content" android:layout_height="wrap_content"></button> <button android:text="*" android:id="@+id/button03" android:layout_width="wrap_content" android:layout_height="wrap_content"></button> <button android:text="/" android:id="@+id/button04" android:layout_width="wrap_content" android:layout_height="wrap_content"></button> <button android:text="=" android:id="@+id/button05" android:layout_width="wrap_content" android:layout_height="wrap_content"></button> </linearlayout> </linearlayout>
下面就是一些对反射技术的操作代码了,由于本篇是反射机制的入门篇,在此只是通过一个小demo 讲解反射的常用的几个方法,这里的流程如下:
获取相应的类对象名称
class<?> classtype = class.forname("com.terry.operationclass");
如果知道类名并且类名存在于我们工程中,即jar 文件中包含可以使用如下写法
class<?> classtype = operationclass.class;
返回本类对象
object invokeoperation = classtype.newinstance();
根据类对象名称去查找对应的方法
method addmethod = classtype.getmethod("add", new class[] { int.class, int.class });
参数一:代码需要查找类名的方法,参数二:指定查找方法的参数类型
调用查找 到的方法执行此方法的处理
object result = addmethod.invoke(invokeoperation, new object[] { new integer(first), new integer(second) });
通过调用查找到的方法即可实现方法体的功能。
tip:反射比较耗费系统资源,建议不在不得以的情况下不要用,尤其是在移动设备上这种对资源要求十分苛刻的设备。
运行效果如下:
下面给出全部页面代码:
package com.terry; import java.lang.reflect.invocationtargetexception; import java.lang.reflect.method; import android.app.activity; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.textview; public class operationactivity extends activity { private edittext one, two; private textview result; private button add, cut, ride, except, sum; int first, second; string operaionfun = ""; /** called when the activity is first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); findview(); add.setonclicklistener(click); cut.setonclicklistener(click); ride.setonclicklistener(click); except.setonclicklistener(click); sum.setonclicklistener(click); } void findview() { one = (edittext) findviewbyid(r.id.edittext01); two = (edittext) findviewbyid(r.id.edittext02); result = (textview) findviewbyid(r.id.textview01); add = (button) findviewbyid(r.id.button01); cut = (button) findviewbyid(r.id.button02); ride = (button) findviewbyid(r.id.button03); except = (button) findviewbyid(r.id.button04); sum = (button) findviewbyid(r.id.button05); } onclicklistener click = new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub first = integer.parseint(one.gettext().tostring()); second = integer.parseint(two.gettext().tostring()); switch (v.getid()) { case r.id.button01: operaionfun = "+"; break; case r.id.button02: operaionfun = "-"; break; case r.id.button03: operaionfun = "*"; break; case r.id.button04: operaionfun = "/"; break; case r.id.button05: try { result.settext(operation(operaionfun, first, second)); } catch (securityexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (illegalargumentexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (classnotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (illegalaccessexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (instantiationexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (nosuchmethodexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (invocationtargetexception e) { // todo auto-generated catch block e.printstacktrace(); } break; } } }; /** * 操作方法 * * @param oper * @param first * @param second * @return * @throws classnotfoundexception * @throws illegalaccessexception * @throws instantiationexception * @throws securityexception * @throws nosuchmethodexception * @throws illegalargumentexception * @throws invocationtargetexception */ string operation(string oper, int first, int second) throws classnotfoundexception, illegalaccessexception, instantiationexception, securityexception, nosuchmethodexception, illegalargumentexception, invocationtargetexception { // 获取相应的类对象名称 class<?> classtype = class.forname("com.terry.operationclass"); // 如果知道类名并且类名存在于我们工程中,即jar 文件中包含可以使用如下写法 //class<?> classtype = operationclass.class; // 返回本类对象 object invokeoperation = classtype.newinstance(); if (oper.equals("+")) { // 根据类对象名称去查找对应的方法 method addmethod = classtype.getmethod("add", new class[] { int.class, int.class }); // 调用查找 到的方法执行此方法的处理 object result = addmethod.invoke(invokeoperation, new object[] { new integer(first), new integer(second) }); return result.tostring(); } else if (oper.equals("-")) { method cutmethod = classtype.getmethod("cut", new class[] { int.class, int.class }); object result = cutmethod.invoke(invokeoperation, new object[] { new integer(first), new integer(second) }); return result.tostring(); } else if (oper.equals("*")) { method ridemethod = classtype.getmethod("ride", new class[] { int.class, int.class }); object result = ridemethod.invoke(invokeoperation, new object[] { new integer(first), new integer(second) }); return result.tostring(); } else if (oper.equals("/")) { method execmthod = classtype.getmethod("except", new class[] { int.class, int.class }); object result = execmthod.invoke(invokeoperation, new object[] { new integer(first), new integer(second) }); return result.tostring(); } return ""; } }
tip:在java中可以通过main 函数打印,在android 好像调用会出错
更多关于android相关内容感兴趣的读者可查看本站专题:《android调试技巧与常见问题解决方法汇总》、《android开发入门与进阶教程》、《android多媒体操作技巧汇总(音频,视频,录音等)》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结》
希望本文所述对大家android程序设计有所帮助。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Android Manifest中meta-data扩展元素数据的配置与获取方式
Android实现获取meta-data和build.gradle的值
Android获取清单文件中的meta-data,解决碰到数值为null的问题
解决webview 第二次调用loadUrl页面不刷新的问题
解决Android WebView拦截url,视频播放加载失败的问题
Android webview加载https链接错误或无响应的解决
网友评论