Testing of override of toString()

本文介绍了如何在 Java 中为 POJO 类覆盖 toString 方法来方便地打印对象属性。通过使用 Apache Commons Lang 库中的 ToStringBuilder,可以简洁地实现这一功能,并展示了两种不同的实现方式及其输出效果。

At fisrt,thanks for the tips of Xia,I just  run the situation he described.

        When we want to print the properties of POJO, we can override the function toString() for conveninence , here is my testing code:

        Created a object Student:

       

package mysrc;
import java.io.Serializable;

import org.apache.commons.lang.builder.ToStringBuilder;

public class Student implements Serializable{

	int id;
	String name;
	//construct
	Student (){		
	}
	
	Student(int id,String name){
		this.id = id;
		this.name = name;
	}
	
	//override
	public  String toString(){		
		return ToStringBuilder.reflectionToString(this);
	}
	
	//the different way to implement this function
	public String toStr(){
		return new ToStringBuilder(this).append("id",id).append("name",name).toString();
	}

}

    Then test these two methods:

   

package mysrc;

public class ToStringTest {	
	public static void main(String args[]){		
		Student stu = new Student(10086,"tanglei");		
		System.out.println(stu.toString());
		System.out.println(stu.toStr());
	}
}

   Then the result is :

          mysrc.Student@a62fc3[id=10086,name=tanglei]
          mysrc.Student@a62fc3[id=10086,name=tanglei]

   Ok,that's all

基于STM32 F4的永磁同步电机无位置传感器控制策略研究内容概要:本文围绕基于STM32 F4的永磁同步电机(PMSM)无位置传感器控制策略展开研究,重点探讨在不依赖物理位置传感器的情况下,如何通过算法实现对电机转子位置和速度的精确估计与控制。文中结合嵌入式开发平台STM32 F4,采用如滑模观测器、扩展卡尔曼滤波或高频注入法等先进观测技术,实现对电机反电动势或磁链的估算,进而完成无传感器矢量控制(FOC)。同时,研究涵盖系统建模、控制算法设计、仿真验证(可能使用Simulink)以及在STM32硬件平台上的代码实现与调试,旨在提高电机控制系统的可靠性、降低成本并增强环境适应性。; 适合人群:具备一定电力电子、自动控制理论基础和嵌入式开发经验的电气工程、自动化及相关专业的研究生、科研人员及从事电机驱动开发的工程师。; 使用场景及目标:①掌握永磁同步电机无位置传感器控制的核心原理与实现方法;②学习如何在STM32平台上进行电机控制算法的移植与优化;③为开发高性能、低成本的电机驱动系统提供技术参考与实践指导。; 阅读建议:建议读者结合文中提到的控制理论、仿真模型与实际代码实现进行系统学习,有条件者应在实验平台上进行验证,重点关注观测器设计、参数整定及系统稳定性分析等关键环节。
package com.oplus.engineermode.anti.anticase.touch; import android.graphics.Color; import android.os.Bundle; import android.os.Handler; import android.os.HandlerThread; import android.os.Message; import android.os.OplusKeyEventManager; import android.provider.Settings; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.TextView; import com.oplus.engineermode.R; import com.oplus.engineermode.anti.anticase.AntiCaseBase; import com.oplus.engineermode.anti.bandsetting.rftoolkit.Tech; import com.oplus.engineermode.anti.bandsetting.rftoolkit.TxParam; import com.oplus.engineermode.anti.constant.Constants; import com.oplus.engineermode.anti.utils.Utils; import com.oplus.engineermode.core.sdk.utils.Log; import com.oplus.engineermode.device.base.DeviceManager; import com.oplus.engineermode.display.lcd.base.LcdTimingColorModeManager; import com.oplus.engineermode.display.sdk.LcdRefreshRateManager; import com.oplus.engineermode.lights.base.LightsManager; import com.oplus.engineermode.misc.sdk.utils.OplusMiscHelper; import com.oplus.engineermode.touchpanel.sdk.utils.OplusTouchHelper; import com.oplus.engineermode.touchscreen.base.AutoTestResult; import com.oplus.engineermode.touchscreen.base.TouchScreenAutoTest; import com.oplus.engineermode.touchscreen.base.TpCommonUtils; import com.oplus.engineermode.util.KeyEventInterceptor; import com.oplus.engineermode.util.ProjectFeatureOptions; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; import java.util.Locale; public class TouchAntiCase extends AntiCaseBase { private static final String TAG = "TouchAntiCase"; private static final int INVALID_SETTING_PARAMETER = -1; private static final int MSG_START = 0x11; private static final int MSG_FIRMWARE = 0x12; private static final int MSG_TOUCH = 0x13; private static final int MSG_ALL_DONE = 0x14; private static final int MOVE_DELEYY = 10000; private static final int MOVE_WHAT = 1001; private static final int VDET_TARGET_VALUE = 950; private static final int VDET_UNIT = 10; private AutoTestResult mTpAutoTestResult; private AutoTestResult mScreenOffTpAutoTestResult; private TouchScreenAutoTest mTouchScreenAutoTest; private HandlerThread mSubHandlerThread; private HandlerThread mTkHandlerThread; private TouchCaseAdapter mTouchCaseAdapter; private boolean mTpAutoTestDone; private boolean mScreenOffTpAutoTestDone; private boolean mIsTouchScreenTpEnabled = true; private int mScreenRateSettings; private boolean mIsScreenOffTouchScreenTpEnabled; private int mOldBrightness = INVALID_SETTING_PARAMETER; private int mOldBrightnessMode = INVALID_SETTING_PARAMETER; private LightsManager mLightsManager; private int mTpIcId = OplusTouchHelper.DEFAULT_TP_IC_ID; private int mCycleTimes; private int mTestTime; private boolean mIsTouchFlag = false; private boolean mIsTouchNeedStart =false; private RelativeLayout rootView; private TextView mFirmwareTv; private TextView mResultTextView; private ListView mTouchListView; private List<AntiTouchResult> mAntiTouchResultList; private Handler mHandler; private StringBuilder testRecord; private StringBuilder testFailRecord; private AntiTouchResult mAntiTouchResult; private TxParam mTxParam; private final KeyEventInterceptor mKeyEventInterceptor = new KeyEventInterceptor(TouchAntiCase.this, TAG, new OplusKeyEventManager.OnKeyEventObserver() { @Override public void onKeyEvent(KeyEvent keyEvent) { Log.i(TAG, keyEvent.toString()); if ((keyEvent.getAction() == KeyEvent.ACTION_UP) && (keyEvent.getKeyCode() == KeyEvent.KEYCODE_BACK)) { runOnUiThread(new Runnable() { @Override public void run() { Log.i(TAG, "KEYCODE_BACK"); if (testRecord != null) { testRecord.append(getString(R.string.anti_back_pressed)).append("\n"); testFailRecord.append(getString(R.string.anti_back_pressed)).append("\n"); } onAntiSkip(getString(R.string.anti_back_pressed)); } }); } } }); private final TouchScreenAutoTest.AutoTestCallback mAutoTestCallback = new TouchScreenAutoTest.AutoTestCallback() { @Override public void onScreenOnTpAutoTestDone(AutoTestResult result) { if (result != null) { Log.i(TAG, "onScreenOnTpAutoTestDone " + result.toString()); testRecord.append(getSystemTime()).append("\t").append(getString(R.string.anti_auto_up_callback)) .append(result.toString()).append("\n"); } mTpAutoTestDone = true; mTpAutoTestResult = result; if ((mTpAutoTestResult != null) && !mTpAutoTestResult.getTestResult()) { mScreenOffTpAutoTestDone = true; } mHandler.sendEmptyMessage(MSG_FIRMWARE); } @Override public void onScreenOffTpAutoTestDone(AutoTestResult result) { if (result != null) { Log.i(TAG, "onScreenOffTpAutoTestDone " + result.toString()); testRecord.append(getSystemTime()).append("\t").append(getString(R.string.anti_auto_down_callback)) .append(result.toString()).append("\n"); } mScreenOffTpAutoTestDone = true; mScreenOffTpAutoTestResult = result; mHandler.sendEmptyMessage(MSG_FIRMWARE); } }; private Handler moveHandler = new Handler() { @Override public void handleMessage(Message message) { super.handleMessage(message); moveHandler.sendEmptyMessageDelayed(MOVE_WHAT, MOVE_DELEYY); RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) rootView.getLayoutParams(); layoutParams.topMargin += 90; layoutParams.leftMargin += 15; if (layoutParams.topMargin >= 900) { layoutParams.topMargin = 0; layoutParams.leftMargin = 0; } Log.i(TAG, "moveHandler:topMargin:" + layoutParams.topMargin + ",leftMargin:" + layoutParams.leftMargin); rootView.setLayoutParams(layoutParams); } }; @Override protected String getAntiItemName() { return TouchAntiItemSetting.getInstance().getAntiItemName(); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.anti_touch_xml); testRecord = new StringBuilder().append(getString(R.string.anti_sensor_start_time_info)) .append(getSystemTime()).append(":").append("\n"); testFailRecord = new StringBuilder(); mTxParam = getIntent().getParcelableExtra(Constants.EXTRA_ANTI_RADIO_TXPARAM); int radioMeasurePower = getIntent().getIntExtra(Constants.EXTRA_ANTI_RADIO_TXMEASURE,Constants.TX_PARAM_DEFAULT_VALUE); Log.d(TAG, "radioMeasurePower : " + radioMeasurePower); if (mTxParam != null) { String txParamInfo = mTxParam.getTxParamInfo(); Log.d(TAG, "txParam info : " + txParamInfo); testRecord.append(getString(R.string.anti_sensor_channel_information)).append(txParamInfo) .append(Utils.getMeasurePowerInfo(getApplicationContext(), radioMeasurePower)).append("\n"); TextView tech = findViewById(R.id.tech_param); tech.setText(mTxParam.getTechInfo()); TextView band = findViewById(R.id.band_param); band.setText(String.valueOf(mTxParam.getBand())); TextView antenna = findViewById(R.id.antenna_param); antenna.setText(String.valueOf(mTxParam.getAntenna())); TextView channelType = findViewById(R.id.channelType_param); channelType.setText(mTxParam.getChannelTypeInfo()); TextView power = findViewById(R.id.power_param); TextView measurePower = findViewById(R.id.measure_power); if (Tech.BDS.name().equals(mTxParam.getTechInfo())) { TextView powerTitle = findViewById(R.id.power_title); powerTitle.setText(getString(R.string.anti_dialog_target_VDET)); TextView measurePowerTitle = findViewById(R.id.measure_power_title); measurePowerTitle.setText(getString(R.string.anti_dialog_measure_VDET)); power.setText(String.valueOf(VDET_TARGET_VALUE)); measurePower.setText(String.valueOf(radioMeasurePower / VDET_UNIT)); } else { power.setText(String.valueOf(mTxParam.getPower())); measurePower.setText(String.valueOf(radioMeasurePower)); } } rootView = (RelativeLayout) findViewById(R.id.rootView); mResultTextView = (TextView) findViewById(R.id.result_tv); mFirmwareTv = findViewById(R.id.anti_firmware); mTouchListView = findViewById(R.id.anti_touch_listview); mAntiTouchResultList = new ArrayList<>(); mTouchCaseAdapter = new TouchCaseAdapter(this, mAntiTouchResultList); mTouchListView.setAdapter(mTouchCaseAdapter); mScreenRateSettings = LcdRefreshRateManager.queryLCMFrequencySetting(this); mLightsManager = new LightsManager(this); final int maxBrightness = mLightsManager.getLcdBacklightMaximumBrightnessLevel(); mOldBrightness = mLightsManager.getLcdBackLightBrightness(this); mOldBrightnessMode = LightsManager.getLcdBrightnessMode(this); if (ProjectFeatureOptions.FEATURE_ARIES_DISPLAY) { LcdTimingColorModeManager.displayTimingAndColorModeInit(getApplicationContext()); LcdTimingColorModeManager.setRefreshRateByMode(120.0f); } else { LcdRefreshRateManager.updateLCMFrequencySetting(this, LcdRefreshRateManager.REFRESH_RATE_60HZ); } Log.i(TAG, "mMaxBrightness: " + maxBrightness + ", mOldBrightness: " + mOldBrightness + ", FEATURE_ARIES_DISPLAY=" + ProjectFeatureOptions.FEATURE_ARIES_DISPLAY); setLcdBackLightBrightness(maxBrightness); mIsScreenOffTouchScreenTpEnabled = false; final String tpDeviceInfo = OplusMiscHelper.getTPDeviceInfo(mTpIcId); final String strProductVersion = getResources().getString(R.string.tp_bin_version) + DeviceManager.getVersionFromDeviceInfo(tpDeviceInfo); mFirmwareTv.setText(strProductVersion); mResultTextView.setText(R.string.tp_autotest_warning); mSubHandlerThread = new HandlerThread("ANTI_TOUCH_PANEL_AUTO_TEST"); mSubHandlerThread.start(); mTkHandlerThread = new HandlerThread("ANTI_TOUCH_RESULT"); mTkHandlerThread.start(); mTouchScreenAutoTest = new TouchScreenAutoTest(this, getMainLooper(), mSubHandlerThread.getLooper()); mTouchScreenAutoTest.setTpIcId(mTpIcId); mTouchScreenAutoTest.init(mIsTouchScreenTpEnabled, mIsScreenOffTouchScreenTpEnabled); mTouchScreenAutoTest.setAutoTestCallback(mAutoTestCallback); mTouchScreenAutoTest.setAgingMode(true); } @Override public boolean onTouchEvent(MotionEvent event) { Log.i(TAG, "TouchEvent action =" + event.getAction()); if (mIsTouchNeedStart) { if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE || event.getAction() == MotionEvent.ACTION_UP) { mIsTouchFlag = true; String format = String.format(Locale.US, "%s: %.2f, %s, %.2f", "x:", event.getX(), "y:", event.getY()); testRecord.append(getString(R.string.anti_touch_fail)).append(format).append("\n"); testFailRecord.append(getString(R.string.anti_touch_fail)).append(format).append("\n"); return true; } } return false; } @Override protected void onResume() { super.onResume(); mTouchListView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { Log.i(TAG, "OnTouchListener action = " + event.getAction()); if (mIsTouchNeedStart) { if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE || event.getAction() == MotionEvent.ACTION_UP) { mIsTouchFlag = true; String format = String.format(Locale.US, "%s: %f, %s, %f", "x:", event.getX(), "y:", event.getY()); testRecord.append(getString(R.string.anti_touch_fail)).append(format).append("\n"); testFailRecord.append(getString(R.string.anti_touch_fail)).append(format).append("\n"); return true; } } return false; } }); setHandler(); initAntiSetting(); if (OplusTouchHelper.isTPCalibrationSupport(mTpIcId) && TpCommonUtils.isSamsungSoftScreen(mTpIcId)) { mResultTextView.append("\n" + getString(R.string.tp_autotest_soft_screen_warning)); mTouchScreenAutoTest.setBackgroundColor(Color.WHITE); mHandler.sendEmptyMessageDelayed(MSG_START, 1500); } else { mHandler.sendEmptyMessage(MSG_START); } mKeyEventInterceptor.registerKeyEventInterceptor(KeyEventInterceptor.KEY_MODE_FULL_MASK); moveHandler.sendEmptyMessageDelayed(MOVE_WHAT, MOVE_DELEYY); } @Override protected void onPause() { super.onPause(); mKeyEventInterceptor.unregisterKeyEventInterceptor(); moveHandler.removeCallbacksAndMessages(null); } @Override protected void onDestroy() { super.onDestroy(); LcdRefreshRateManager.updateLCMFrequencySetting(this, mScreenRateSettings); if (INVALID_SETTING_PARAMETER != mOldBrightness) { mLightsManager.setLcdBackLightBrightness(this, mOldBrightness); mOldBrightness = INVALID_SETTING_PARAMETER; } if (mOldBrightnessMode != INVALID_SETTING_PARAMETER) { LightsManager.setLcdBrightnessMode(this, mOldBrightnessMode); mOldBrightnessMode = INVALID_SETTING_PARAMETER; } mHandler.removeCallbacksAndMessages(null); mKeyEventInterceptor.unregisterKeyEventInterceptor(); if (mTouchScreenAutoTest != null) { mTouchScreenAutoTest.stop(); } if (mSubHandlerThread != null) { mSubHandlerThread.quit(); } if (mTkHandlerThread != null) { mTkHandlerThread.quit(); } if (testRecord != null) { testRecord.append(getString(R.string.anti_sensor_end_time_info)) .append(getSystemTime()).append("\n"); testRecord.append("-----------------------------------------------"); saveAntiItemRecordToSDcard(testRecord.toString()); testRecord = null; } if (testFailRecord != null) { testFailRecord = null; } if (moveHandler != null) { moveHandler.removeCallbacksAndMessages(null); moveHandler = null; } } private void setHandler() { mHandler = new Handler(mTkHandlerThread.getLooper()) { @Override public void handleMessage(Message msg) { super.handleMessage(msg); Log.i(TAG, "message = " + msg.what); switch (msg.what) { case MSG_START: if (mCycleTimes > 0) { mAntiTouchResult = new AntiTouchResult(AntiTouchResult.ANTI_TOUCH_TESTING, AntiTouchResult.ANTI_TOUCH_NOT_START); mTouchScreenAutoTest.start(); mAntiTouchResultList.add(mAntiTouchResult); mCycleTimes--; runOnUiThread(new Runnable() { @Override public void run() { mTouchCaseAdapter.notifyDataSetChanged(); } }); } else { sendEmptyMessage(MSG_ALL_DONE); } break; case MSG_FIRMWARE: if (mTpAutoTestDone && mScreenOffTpAutoTestDone) { boolean screenOnTpResult = false; boolean screenOffTpResult = false; if (mTpAutoTestResult != null) { screenOnTpResult = mTpAutoTestResult.getTestResult(); Log.i(TAG, "screenOnTpResult = " + screenOnTpResult); } if (mScreenOffTpAutoTestResult != null) { screenOffTpResult = mScreenOffTpAutoTestResult.getTestResult(); Log.i(TAG, "screenOffTpResult = " + screenOffTpResult); } if (screenOnTpResult && screenOffTpResult) { if (mAntiTouchResult != null) { mAntiTouchResult.setAutoTestResult(AntiTouchResult.ANTI_TOUCH_PASS); } } else { testFailRecord.append(getString(R.string.anti_touch_firmware_fail)).append("\n"); if (mAntiTouchResult != null) { mAntiTouchResult.setAutoTestResult(AntiTouchResult.ANTI_TOUCH_FAIL); } } mAntiTouchResult.setTouchResult(AntiTouchResult.ANTI_TOUCH_TESTING); runOnUiThread(new Runnable() { @Override public void run() { mTouchCaseAdapter.notifyDataSetChanged(); } }); mIsTouchNeedStart = true; mTpAutoTestDone = false; mScreenOffTpAutoTestDone = false; sendEmptyMessageDelayed(MSG_TOUCH, mTestTime * 1000); } break; case MSG_TOUCH: mIsTouchNeedStart = false; if (mIsTouchFlag) { if (mAntiTouchResult != null) { mAntiTouchResult.setTouchResult(AntiTouchResult.ANTI_TOUCH_FAIL); } mIsTouchFlag = false; } else { if (mAntiTouchResult != null) { mAntiTouchResult.setTouchResult(AntiTouchResult.ANTI_TOUCH_PASS); } } runOnUiThread(new Runnable() { @Override public void run() { mTouchCaseAdapter.notifyDataSetChanged(); } }); mAntiTouchResult =new AntiTouchResult(); sendEmptyMessage(MSG_START); break; case MSG_ALL_DONE: boolean result = true; for (int i = 0; i < mAntiTouchResultList.size(); i++) { int autoTestResult = mAntiTouchResultList.get(i).isAutoTestResult(); int touchResult = mAntiTouchResultList.get(i).isTouchResult(); if (autoTestResult != AntiTouchResult.ANTI_TOUCH_PASS || touchResult != AntiTouchResult.ANTI_TOUCH_PASS) { result = false; break; } } if (result) { runOnUiThread(new Runnable() { @Override public void run() { mResultTextView.setText(R.string.pass); mResultTextView.setTextColor(Color.GREEN); } }); onAntiPass(); } else { runOnUiThread(new Runnable() { @Override public void run() { mResultTextView.setText(R.string.fail); } }); onAntiFail(testFailRecord.toString()); } break; default: break; } } }; } private void initAntiSetting() { mCycleTimes = TouchAntiItemSetting.getInstance().getAntiTouchCycleTimes(getAntiItemSetting()); Log.i(TAG, "mCycleTimes = " + mCycleTimes); mTestTime =TouchAntiItemSetting.getInstance().getAntiTouchTestTime(getAntiItemSetting()); Log.i(TAG, "mTestTime = " + mTestTime); } private void setLcdBackLightBrightness(int brightness) { Log.i(TAG, "setLcdBackLightBrightness " + brightness); if (Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL != LightsManager.getLcdBrightnessMode(this)) { LightsManager.setLcdBrightnessMode(this, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); } //disable ghbm, avoid light sensor enabled background if (0 != LightsManager.getGlobalHighBrightnessMode(this)) { LightsManager.setGlobalHighBrightnessMode(this, 0); } mLightsManager.setLcdBackLightBrightness(this, brightness); } private String getSystemTime() { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy_MM_dd HH_mm_ss.SSS"); return simpleDateFormat.format(System.currentTimeMillis()); } } 给这段代码加中文注释
07-08
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值