Function Code separation and code structure if return values are Boolean Functions and judgment optimization, if Boolean
When learning the first line of code, I learned that AppCompatActivity can be inherited by creating a new class. Other newly created activities inherit from the newly created class to manage all activities.
In this program that reads RFID content, the code structure is MainActivity inherited from SerialPortActivity, and SerialPortActivity inherited from ACtivity. SerialPortActivity
There are methods for enabling and disabling the Management port and a virtual class of onDataReceiver (this class is rewritten in MAinActivity, but has been called in SerialPortActivity .)
In MainAcitvity,
Public void btnOpen_Click (View btn ){
If (this. open ()){
This. btnOpen. setEnabled (false );
This. btnClose. setEnabled (true );
}
}
The open () method is written in SerialPortActivity.
Public boolean open (){
Boolean bret = false;
If (this. serialPortTool = null ){
This. serialPortTool = new SerialPortTool ();
}
Try {
This. mSerialPort = this. serialPortTool. getSerialPort ("/dev/ttyS2", 9600 );
This. mOutputStream = this. mSerialPort. getOutputStream ();
This. mInputStream = this. mSerialPort. getInputStream ();
If (this. mReadThread = null ){
This. mReadThread = new SerialPortActivity. ReadThread (SerialPortActivity. ReadThread) null );
This. mReadThread. start ();
}
Bret = true;
} Catch (SecurityException var3 ){
This. DisplayError (2131099651 );
} Catch (IOException var4 ){
This. DisplayError (2131099652 );
} Catch (InvalidParameterException var5 ){
This. DisplayError (2131099650 );
} Catch (Exception var6 ){
This. DisplayError (2131099652 );
}
Return bret;
}
The existence of the two classes makes the logic of this program clearer. In the if () Statement, something in () will be executed, rather than simply returning a Boolean value, instead of running in a sandbox, it still executes the functions that should be implemented.
In general, the content in the if () statement will be executed (not in the sandbox, the Value Returns a Boolean value)
For some basic functions, a class (including some virtual functions) can be formed separately. Other classes inherit this basic class. To achieve
Polymorphism inheritance and feature Separation