Chinaunix首页 | 论坛 | 博客
  • 博客访问: 65419
  • 博文数量: 43
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 420
  • 用 户 组: 普通用户
  • 注册时间: 2014-06-27 15:04
个人简介

记录,分享

文章分类

全部博文(43)

文章存档

2017年(24)

2015年(1)

2014年(18)

我的朋友

分类: Java

2014-06-27 16:34:38

     很多人说类的私有方法不用测试,理由是类私有方法只允许被本类访问,而其他类无权调用。事实上并非如此,来看一个范例程序,Commodity_Parent是一个商品父类,定义了两个商品类基础属性:commodity_name,commodity_price,分别是商品的名称和价格。二组公共和两组受保护的实例方法分别是getcommodity_name(),getcommodity_price,setcommodity_name(),setcommodity_price(),对应的功能是取商品名称、价格,设置商品名称、价格。具体代码如下:

package com.fastpoint.Private;

publicclass Commodity_Parent {

private String commodity_name;

private Double commodity_price;

public Commodity_Parent(String name, Double price) {

commodity_name = name;

commodity_price = price;

}

public String getCommodity_name() {

return commodity_name;

}

publicvoid setCommodity_name(String commodity_name) {

this.commodity_name = commodity_name;

}

public Double getCommodity_price() {

return commodity_price;

}

publicvoid setCommodity_price(Double commodity_price) {

this.commodity_price = commodity_price;

}

}

Commodity_Child类继承了Commodity_Parent类,并且增加了一个新的类属性Commodity_number,该属性用来记录具体商品类的计量数量。GetCommodityNumber()方法用来取得该类实例的商品数量,对应的SetCommodityNumber()实例方法被声明为私有的,从这里可以看出,程序不允许别人使用该方法对商品数量做直接的修改,而是通过一个共有的setData()方法实现更安全的商品数量操作,具体代码如下:

package com.fastpoint.Private;

publicclass Commodity_Child extends Commodity_Parent {

private Integer Commodity_number;

public Commodity_Child(String Commodity_name, double Commodity_price,

Integer number) {

super(Commodity_name, Commodity_price);

Commodity_number = number;

}

public Integer getCommodity_number() {

return Commodity_number;

}

privatevoid setCommodity_number(Integer commodity_number) {

Commodity_number = commodity_number;

}

publicvoid setData(Integer commoditynumber) {

if (isValidate()) {

if (isCommodityNumber(commoditynumber)) {

setCommodity_number(commoditynumber);

} else {

System.out.println("没有足够的商品数量!");

}

} else {

System.out.println("没有足够权限!");

}

}

publicboolean isValidate() {

//示例程序简化部分代码

returntrue;

}

publicboolean isCommodityNumber(double d) {

//示例程序简化部分代码

returntrue;

}

}

    可以利用java的反射机制来实现访问类的私有方法或者属性。

    使用java.lang.Class的相关方法,获得相关指定对象的Field,然后调用field.setAccessible(true);绕过访问权限的检查,然后可以访问Field的值,当然也可以设置Field的值。

    使用java.lang.Class的相关方法,获得相关指定对象的Method;然后调用field.setAccessible(true);绕过访问权限的检查;最后执行该方法.

测试代码如下所示:

package com.fastpoint.testPrivate;

import static org.junit.Assert.*;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

import org.junit.After;

import org.junit.Before;

import org.junit.Test;

public class Commodity_ChildTest {

@Before

public void setUp() throws Exception {

}

@After

public void tearDown() throws Exception {

}

@Test

public void testSetCommodity_number() throws ClassNotFoundException,

SecurityException, NoSuchMethodException, IllegalArgumentException,

IllegalAccessException, InvocationTargetException,

InstantiationException {

Commodity_Child comm = new Commodity_Child();

Class[] parameterTypes = new Class[1];// 这里你要调用的方法只有一个参数

parameterTypes[0] = Integer.class;// 参数类型为String[]

Method method = comm.getClass().getDeclaredMethod(

"setCommodity_number", Integer.class);// 要调用的方法是SetCommodity_number

Object[] args = new Object[1];

method.setAccessible(true);// 允许处理私有方法

method.invoke(comm, new Object[] { new Integer(15) });// 调用方法

method.setAccessible(false);

assertEquals(15, comm.getCommodity_number(), 0);

}

}

上面代码演示了测试Commodity_Child类中的私有方法setCommodity_Number(),主要运用了Java的反射机制。


阅读(747) | 评论(0) | 转发(0) |
0

上一篇:JAVA笔记_反射

下一篇:C语言错误处理策略

给主人留下些什么吧!~~