欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android基于反射技術(shù)實(shí)現(xiàn)的加減乘除運(yùn)算示例

 更新時(shí)間:2016年10月25日 08:58:16   作者:Terry_龍  
這篇文章主要介紹了Android基于反射技術(shù)實(shí)現(xiàn)的加減乘除運(yùn)算,較為詳細(xì)的描述了反射技術(shù)的原理,并結(jié)合完整實(shí)例形式分析了Android基于反射技術(shù)實(shí)現(xiàn)加減乘除四則運(yùn)算的相關(guān)操作步驟與實(shí)現(xiàn)技巧,需要的朋友可以參考下

本文實(shí)例講述了Android基于反射技術(shù)實(shí)現(xiàn)的加減乘除運(yùn)算。分享給大家供大家參考,具體如下:

JAVA反射機(jī)制定義:

JAVA反射機(jī)制是在運(yùn)行狀態(tài)中,對(duì)于任意一個(gè)類(lèi),都能夠知道這個(gè)類(lèi)的所有屬性和方法;對(duì)于任意一個(gè)對(duì)象,都能夠調(diào)用它的任意一個(gè)方法;這種動(dòng)態(tài)獲取的信息以及動(dòng)態(tài)調(diào)用對(duì)象的方法的功能稱(chēng)為java語(yǔ)言的反射機(jī)制。

Java反射機(jī)制主要提供了以下功能: 在運(yùn)行時(shí)判斷任意一個(gè)對(duì)象所屬的類(lèi);在運(yùn)行時(shí)構(gòu)造任意一個(gè)類(lèi)的對(duì)象;在運(yùn)行時(shí)判斷任意一個(gè)類(lèi)所具有的成員變量和方法;在運(yùn)行時(shí)調(diào)用任意一個(gè)對(duì)象的方法;生成動(dòng)態(tài)代理。

有時(shí)候我們說(shuō)某個(gè)語(yǔ)言具有很強(qiáng)的動(dòng)態(tài)性,有時(shí)候我們會(huì)區(qū)分動(dòng)態(tài)和靜態(tài)的不同技術(shù)與作法。我們朗朗上口動(dòng)態(tài)綁定(dynamic binding)、動(dòng)態(tài)鏈接(dynamic linking)、動(dòng)態(tài)加載(dynamic loading)等。然而“動(dòng)態(tài)”一詞其實(shí)沒(méi)有絕對(duì)而普遍適用的嚴(yán)格定義,有時(shí)候甚至像對(duì)象導(dǎo)向當(dāng)初被導(dǎo)入編程領(lǐng)域一樣,一人一把號(hào),各吹各的調(diào)。

一般而言,開(kāi)發(fā)者社群說(shuō)到動(dòng)態(tài)語(yǔ)言,大致認(rèn)同的一個(gè)定義是:“程序運(yùn)行時(shí),允許改變程序結(jié)構(gòu)或變量類(lèi)型,這種語(yǔ)言稱(chēng)為動(dòng)態(tài)語(yǔ)言”。從這個(gè)觀點(diǎn)看,Perl,Python,Ruby是動(dòng)態(tài)語(yǔ)言,C++,Java,C#不是動(dòng)態(tài)語(yǔ)言。

盡管在這樣的定義與分類(lèi)下Java不是動(dòng)態(tài)語(yǔ)言,它卻有著一個(gè)非常突出的動(dòng)態(tài)相關(guān)機(jī)制:Reflection。這個(gè)字的意思是 “反射、映象、倒影”,用在Java身上指的是我們可以于運(yùn)行時(shí)加載、探知、使用編譯期間完全未知的classes。換句話(huà)說(shuō),Java程序可以加載一個(gè) 運(yùn)行時(shí)才得知名稱(chēng)的class,獲悉其完整構(gòu)造(但不包括methods定義),并生成其對(duì)象實(shí)體、或?qū)ζ鋐ields設(shè)值、或喚起其methods1。 這種“看透class”的能力(the ability of the program to examine itself)被稱(chēng)為introspection(內(nèi)省、內(nèi)觀、反省)。Reflection和introspection是常被并提的兩個(gè)術(shù)語(yǔ)。

以上摘錄自百度百科,在Android 中有很多類(lèi)是被封閉的,比如 ServiceManager 藍(lán)牙模塊更是有N多個(gè)類(lèi)被Android 隱藏不開(kāi)放,要調(diào)用這些類(lèi)必須使用java 的反射技術(shù)將類(lèi)轉(zhuǎn)為對(duì)象進(jìn)行操作.Android 應(yīng)用也是基于JAVA 語(yǔ)言為基礎(chǔ),當(dāng)然也具備反射這一技術(shù),下面我寫(xiě)了一個(gè)DEMO 是如何通過(guò)反射技術(shù)調(diào)用類(lèi)名方法并完成一個(gè)加減乘除的記算器。

首先我們定義一個(gè)類(lèi),此為只是簡(jiǎn)單的定義幾個(gè)方法,即加減乘除四個(gè)方法,代碼如下:

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>

下面就是一些對(duì)反射技術(shù)的操作代碼了,由于本篇是反射機(jī)制的入門(mén)篇,在此只是通過(guò)一個(gè)小DEMO 講解反射的常用的幾個(gè)方法,這里的流程如下:

獲取相應(yīng)的類(lèi)對(duì)象名稱(chēng)

Class<?> classType = Class.forName("com.terry.operationClass");

如果知道類(lèi)名并且類(lèi)名存在于我們工程中,即jar 文件中包含可以使用如下寫(xiě)法

Class<?> classType = operationClass.class;

返回本類(lèi)對(duì)象

Object invokeOperation = classType.newInstance();

根據(jù)類(lèi)對(duì)象名稱(chēng)去查找對(duì)應(yīng)的方法

Method addMethod = classType.getMethod("add", new Class[] {
          int.class, int.class });

參數(shù)一:代碼需要查找類(lèi)名的方法,參數(shù)二:指定查找方法的參數(shù)類(lèi)型

調(diào)用查找 到的方法執(zhí)行此方法的處理

Object result = addMethod.invoke(invokeOperation, new Object[] {
  new Integer(first), new Integer(second)
});

通過(guò)調(diào)用查找到的方法即可實(shí)現(xiàn)方法體的功能。

Tip:反射比較耗費(fèi)系統(tǒng)資源,建議不在不得以的情況下不要用,尤其是在移動(dòng)設(shè)備上這種對(duì)資源要求十分苛刻的設(shè)備。

運(yùn)行效果如下:

下面給出全部頁(yè)面代碼:

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 {
    // 獲取相應(yīng)的類(lèi)對(duì)象名稱(chēng)
    Class<?> classType = Class.forName("com.terry.operationClass");
    // 如果知道類(lèi)名并且類(lèi)名存在于我們工程中,即jar 文件中包含可以使用如下寫(xiě)法
    //Class<?> classType = operationClass.class;
    // 返回本類(lèi)對(duì)象
    Object invokeOperation = classType.newInstance();
    if (oper.equals("+")) {
      // 根據(jù)類(lèi)對(duì)象名稱(chēng)去查找對(duì)應(yīng)的方法
      Method addMethod = classType.getMethod("add", new Class[] {
          int.class, int.class });
      // 調(diào)用查找 到的方法執(zhí)行此方法的處理
      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中可以通過(guò)main 函數(shù)打印,在Android 好像調(diào)用會(huì)出錯(cuò)

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Android調(diào)試技巧與常見(jiàn)問(wèn)題解決方法匯總》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論