, I introduced my case for writing Java code using good habits, explained why we should care about the way our code looks, and illustrated some general elements of good Java style. In this part, I illustrate more elements of good style and bring my case to a conclusion.
FfB北方站长站Source Files
FfB北方站长站There are many ways that a Java source file can be organized. Here is one that works well:
FfB北方站长站FfB北方站长站File header comment (optional).
FfB北方站长站Package declaration.
FfB北方站长站Blank line or other separator.
FfB北方站长站Import statements.
FfB北方站长站Blank line or other separator.
FfB北方站长站Class(es).
FfB北方站长站Example 1. Bad File Organization.
FfB北方站长站FfB北方站长站 package org.rotpad;
FfB北方站长站 import java.awt.*;
FfB北方站长站 import javax.swing.event.*;
FfB北方站长站 import org.javacogs.*;
FfB北方站长站 import javax.swing.*;
FfB北方站长站 import java.awt.event.*;
FfB北方站长站 class Foo {
FfB北方站长站 ...
FfB北方站长站 }
FfB北方站长站 public class RotPad extends JFrame {
FfB北方站长站 ...
FfB北方站长站 }
FfB北方站长站FfB北方站长站Example 2. Good File Organization.
FfB北方站长站FfB北方站长站 package org.rotpad;
FfB北方站长站 FfB北方站长站 // Java classes
FfB北方站长站 import java.awt.*;
FfB北方站长站 import java.awt.event.*;
FfB北方站长站 import javax.swing.*;
FfB北方站长站 import javax.swing.event.*;
FfB北方站长站 FfB北方站长站 // JavaCogs classes
FfB北方站长站 import org.javacogs.*;
FfB北方站长站 FfB北方站长站 /**
FfB北方站长站 * RotPad is a simple GUI application for performing rotation ciphers on plain
FfB北方站长站 * text.
FfB北方站长站 *
FfB北方站长站 * @author Thornton Rose
FfB北方站长站 * @version 1.0
FfB北方站长站 */
FfB北方站长站 public class RotPad extends JFrame {
FfB北方站长站 ...
FfB北方站长站 }
FfB北方站长站 FfB北方站长站 //-----------------------------------------------------------------------------
FfB北方站长站 FfB北方站长站 /**
FfB北方站长站 * Foo is ...
FfB北方站长站 *
FfB北方站长站 * @author Thornton Rose
FfB北方站长站 * @version 1.0
FfB北方站长站 */
FfB北方站长站 class Foo {
FfB北方站长站 ...
FfB北方站长站 }
FfB北方站长站FfB北方站长站Import Statements
FfB北方站长站A complex class can have a large number of imports, which can get unruly, especially if you prefer to import individual classes instead of whole packages (e.g., java.awt.*). To get a handle on imports, organize them as follows:
FfB北方站长站FfB北方站长站Java standard classes (java.*).
FfB北方站长站Java extension classes (javax.*).
FfB北方站长站Third-party classes.
FfB北方站长站Application classes.
FfB北方站长站Be sure to comment the third-party and application classes, particularly those that do not have obvious names. Use end-of-line comments, or put a comment at the beginning of the section. Also, if you really want to be a perfectionist, order each group of imports alphabetically.
FfB北方站长站Example 3. Bad Import Style.
FfB北方站长站FfB北方站长站 import java.util.*;
FfB北方站长站 import javax.swing.*;
FfB北方站长站 import java.awt.event*;
FfB北方站长站 import com.gensym.com.*;
FfB北方站长站 import javax.swing.table.*;
FfB北方站长站 import com.pv.jfcx.*;
FfB北方站长站 import java.awt.*;
FfB北方站长站 import com.melthorn.util.*;
FfB北方站长站FfB北方站长站Example 4a. Good Import Style.
FfB北方站长站FfB北方站长站 import java.awt.*;
FfB北方站长站 import java.awt.event*;
FfB北方站长站 import java.util.*;
FfB北方站长站 import javax.swing.table.*;
FfB北方站长站 import com.gensym.com.*; // BeanXporter
FfB北方站长站 import com.pv.jfcx.*; // ProtoView
FfB北方站长站 import com.melthorn.util.*; // Utilities
FfB北方站长站Example 4b. Good Import Style.
FfB北方站长站FfB北方站长站 FfB北方站长站 // Java classes
FfB北方站长站 import java.awt.*;
FfB北方站长站 import java.awt.event*;
FfB北方站长站 import java.util.*;
FfB北方站长站 import javax.swing.table.*;
FfB北方站长站 FfB北方站长站 // BeanXporter
FfB北方站长站 import com.gensym.com.*;
FfB北方站长站 FfB北方站长站 // ProtoView GUI components
FfB北方站长站 import com.pv.jfcx.*;
FfB北方站长站 FfB北方站长站 // Application classes
FfB北方站长站 import com.melthorn.util.*;
FfB北方站长站FfB北方站长站Classes
FfB北方站长站Organizing a Java source file without organizing the classes in it would not gain you much in the way of proper style. Here's how to organize the classes in your source files:
FfB北方站长站FfB北方站长站Javadoc comment or other header comment.
FfB北方站长站Class declaration.
FfB北方站长站Field declarations.
FfB北方站长站Blank line or other separator.
FfB北方站长站Constructors.
FfB北方站长站Blank line or other separator.
FfB北方站长站Methods, except main()
FfB北方站长站 FfB北方站长站, grouped logically.
FfB北方站长站Blank line or other separator.
FfB北方站长站Inner classes.
FfB北方站长站Blank line or other separator.
FfB北方站长站main()
FfB北方站长站 FfB北方站长站.
FfB北方站长站Example 5. Bad Class Style.
FfB北方站长站FfB北方站长站 // RotPad -- GUI app. for ROT ciphering
FfB北方站长站 public class RotPad extends JFrame {
FfB北方站长站 private static final String TRANSFORM_ROT13 = "ROT13";
FfB北方站长站 private static final String TRANSFORM_ROT13N5 = "ROT13N5";
FfB北方站长站 private static final String TRANSFORM_ROTASCII = "ROT-ASCII";
FfB北方站长站 FfB北方站长站 private void jbInit() throws Exception {
FfB北方站长站 ...
FfB北方站长站 }
FfB北方站长站 FfB北方站长站 public static final String TITLE = "RotPad";
FfB北方站长站 public static final String VERSION = "1.0";
FfB北方站长站 FfB北方站长站 public static void main(String[] args) {
FfB北方站长站 ...
FfB北方站长站 }
FfB北方站长站 FfB北方站长站 public RotPad() {
FfB北方站长站 ...
FfB北方站长站 }
FfB北方站长站 FfB北方站长站 private JPanel jPanel1 = new JPanel();
FfB北方站长站 private JPanel jPanel2 = new JPanel();
FfB北方站长站 private BorderLayout borderLayout1 = new BorderLayout();
FfB北方站长站 ...
FfB北方站长站 }
FfB北方站长站FfB北方站长站Example 6. Good Class Style.
FfB北方站长站FfB北方站长站 /**
FfB北方站长站 * RotPad is a simple GUI application for performing rotation ciphers on plain
FfB北方站长站 * text.
FfB北方站长站 *
FfB北方站长站 * @author Thornton Rose
FfB北方站长站 * @version 1.0
FfB北方站长站 &
FfB北方站长站
共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面