Function Code separation and code structure if return values are Boolean Functions and judgment optimization, if Boolean

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.