建体彩网|中彩网双色球连号|
?
快捷搜索:  as  test  1111  test aNd 8=8  test++aNd+8=8  as++aNd+8=8  as aNd 8=8

www9778con:Java面向對象編程實例詳解

?

Java是一種面向工具的說話,是實現面向工具編程的強大年夜對象。但若何在編程中實際運用并發揮其最大年夜效能呢?本文經由過程一個實際Java法度榜樣的開拓歷程,具體闡清楚明了若何應用面向工具實現Java編程。

我們要實現的Java利用法度榜樣是:當用戶輸入一個球體的半徑,法度榜樣將顯示該球體的體積與外面積。在您涉獵下文曩昔,請您自己思慮一分鐘,您將若何設計該Java利用法度榜樣。

一、通俗實現措施

我信托大年夜多半法度榜樣員要實現上述功能的法度榜樣,異常迅速地、自大地將采納下面的實今世碼:

class Sphere

{

public static void main(String[] args)

{

EasyReader console = new EasyReader();

System.out.print("Enter the radius: ");

double radius = console.readDouble();

System.out.println("Radius = " + radius);

double volume = 4.0 / 3.0 * Math.PI * radius * radius * radius;

System.out.println("Volume = " + volume);

double surfArea = 4.0 * Math.PI * radius * radius;

System.out.println("Surface area = " + surfArea);

}

}

EasyReader類代碼如下:

import java.io.*;

public class EasyReader

{

protected String myFileName;

protected BufferedReader myInFile;

protected int myErrorFlags = 0;

protected static final int OPENERROR = 0x0001;

protected static final int CLOSEERROR = 0x0002;

protected static final int READERROR = 0x0004;

protected static final int EOF = 0x0100;

/**

* Constructor. Prepares console (System.in) for reading

*/

public EasyReader()

{

myFileName = null;

myErrorFlags = 0;

myInFile = new BufferedReader(

new InputStreamReader(System.in), 128);

}

/**

* Constructor. opens a file for reading

* @param fileName the name or pathname of the file

*/

public EasyReader(String fileName)

{

myFileName = fileName;

myErrorFlags = 0;

try

{

myInFile = new BufferedReader(new FileReader(fileName), 1024);

}

catch (FileNotFoundException e)

{

myErrorFlags |= OPENERROR;

myFileName = null;

}

}

/**

* Closes the file

*/

public void close()

{

if (myFileName == null)

return;

try

{

myInFile.close();

}

catch (IOException e)

{

System.err.println("Error closing " + myFileName + "

");

myErrorFlags |= CLOSEERROR;

}

}

/**

* Checks the status of the file

* @return true if en error occurred opening or reading the file,

* false otherwise

*/

public boolean bad()

{

return myErrorFlags != 0;

}

/**

* Checks the EOF status of the file

* @return true if EOF was encountered in the previous read

* operation, false otherwise

*/

public boolean eof()

{

return (myErrorFlags & EOF) != 0;

}

private boolean ready() throws IOException

{

return myFileName == null || myInFile.ready();

}

/**

* Reads the next character from a file (any character including

* a space or a newline character).

* @return character read or 〈code〉null〈/code〉 character

* (Unicode 0) if trying to read beyond the EOF

*/

public char readChar()

{

char ch = ´u0000´;

try

{

if (ready())

{

ch = (char)myInFile.read();

}

}

catch (IOExceptiowww9778conn e)

{

if (myFileName != null)

System.err.println("Error reading " + myFileName + "

");

myErrorFlags |= READERROR;

}

if (ch == ´u0000´)

myErrorFlags |= EOF;

return ch;

}

/**

* Reads from the current position in the file up to and including

* the next newline character. The newline character is thrown away

* @return the read string (excluding the newline character) or

* null if trying to read beyond the EOF

*/

public String readLine()

{

String s = null;

try

{

s = myInFile.readLine();

}

catch (IOException e)

{

if (myFileName != null)

System.err.println("Error reading " + myFileName + "

");

myErrorFlags |= READERROR;

}

if (s == null)

myErrorFlags |= EOF;

return s;

}

/**

* Skips whitespace and reads the next word (a string of consecutive

* non-whitespace characters (up to but excluding the next space,

* newline, etc.)

* @return the read string or null if trying to read beyond the EOF

*/

public String readWord()

{

StringBuffer buffer = new StringBuffer(128);

char ch = ´ ´;

int count = 0;

String s = null;

try

{

while (ready() && Character.isWhitespace(ch))

ch = (char)myInFile.read();

while (ready() && !Character.isWhitespace(ch))

{

count++;

buffer.append(ch);

myInFile.mark(1);

ch = (char)myInFile.read();

};

if (count 〉 0)

{

myInFile.reset();

s = buffer.toString();

}

else

{

myErrorFlags |= EOF;

}

}

catch (IOException e)

{

if (myFileName != null)

System.err.println("Error reading " + myFileName + "

");

myErrorFlags |= READERROR;

}

return s;

}

/**

* Reads the next integer (without validating its format)

* @return the integer read or 0 if trying to read beyond the EOF

*/

public int readInt()

{

String s = readWord();

if (s != null)

return Integer.parseInt(s);

else

return 0;

}

/**

* Reads the next double (without validating its format)

* @return the number read or 0 if trying to read beyond the EOF

*/

public double readDouble()

{

String s = readWord();

if (s != null)

return Double.parseDouble(s);

// in Java 1, use: return Double.valueOf(s).doubleValue();

else

return 0.0;

}

}

以下是法度榜樣的輸出結果畫面。

screen.width-333)this.width=screen.width-333;">

在上述法度榜樣中,實現了球體外面積和體積的算法,并經由過程EasyReader類輕松實現了節制臺的輸入,然則該法度榜樣只是完成了法度榜樣所要求的功能,并謀略出了卻果。該法度榜樣不是一個好的設計法度榜樣,它既沒采納布局化編程,也沒有發揮Java說話的強大年夜效能,更不是面向工具編程。

實際上,從法度榜樣設計的角度來看,以上法度榜樣是一個異常糟糕的設計法度榜樣。首先,用戶界面與謀略公式混雜在一路。請記著:對付任何編程說話,用戶界面必須與謀略或處置懲罰歷程分分開來。別的,輸出的結果太丟臉,數值包括太多的小數。

二、布局化編程措施

在以下的法度榜樣代碼中,我們添加了2個措施,分手實現球體外面積與體積的謀略,并經由過程響應的類節制數值的輸出款式。

import java.text.DecimalFormat;

class Sphere

{

// Computes the volume of a sphere with radius r.

private static double volume(double r)

{

return 4.0 / 3.0 * Math.PI * r * r * r;

}

// Computes the surface area of a sphere with radius r.

private static double surfaceArea(double r)

{

return 4.0 * Math.PI * r * r;

}

public static void main(String[] args)

{

EasyReader console = new EasyReader();

System.out.print("Enter the radius: ");

double radius = console.readDouble();

DecimalFormat f3 = new DecimalFormat("0.000");

System.out.println(); // displays a blank line

System.out.prinwww9778contln("Radius = " + f3.format(radius));

System.out.println("Volume = " + f3.format(volume(radius)));

System.out.println("Surface area = " + f3.format(surfaceArea(radius)));

System.out.println();

}

}

以下是輸出結果。

screen.width-333)this.width=screen.width-333;">

上述顛末改動后的法度榜樣,其輸出結果看起來漂亮多了,但它仍只是一個勉強可以經由過程的設計法度榜樣。該法度榜樣采納了布局化編程的風格,并不是面向工具編程。首先,謀略歷程仍與用戶界面實現在同一個類中。別的,對付不合的用戶界面,不能表現出面向工具編程的好處,即不能重用這些謀略歷程。

三、面向工具編程措施

要實現上述法度榜樣的面向工具編程,我們必須留意:Java利用法度榜樣中的每一個類或工具,必須零丁實現自己的義務。詳細來說,在本法度榜樣中,一個類定義球體,即模型或數據表示(Model),另一個類便是實現用戶界面(UI)。

別的更緊張的,也是中公法度榜樣員經常漠視的,便是要斟酌到團體的開拓與治理,實現面向工具編程,便是閃開拓組中的每個法度榜樣員能自力開拓不合的類,異常迅速地前進開拓效率。

是以,對付本法度榜樣來說,面向工具編程的設計措施便是將Model與UI分手是實現在不合的類中。其示意圖如下:

screen.width-333)this.width=screen.width-333;">

以下分手是該法度榜樣的UML類圖設計與法度榜樣代碼。有關UML類圖的設計與利用,請查閱作者的另文(《使用UML類圖設計Java利用法度榜樣詳解一、二》)

Sphere的類圖設計為:

screen.width-333)this.width=screen.width-333;">

其代碼為:

class Sphere

{

private double myRadius;

private double myCenterX;

private double myCenterY;

// Constructors:

public Sphere (double x, double y, double r)

{

myCenterX = x;

myCenterY = y;

myRadius = r;

}

// etc...

// Accessors and modifiers:

public double getRadius()

{

return myRadius;

}

public void setRadius(double r)

{

myRadius = r;

}

// etc...

// Other public methods:

public double volume()

{

return 4.0 / 3.0 * Math.PI * myRadius * myRadius * myRadius;

}

public double surfaceArea()

{

return 4.0 * Math.PI * myRadius * myRadius;

}

public String toString()

{

return "Sphere [Center = (" + myCentewww9778conrX + ", " + myCenterY + ") Radius = " + myRadius

+ "]";

}

}

TestSphere的類圖為:

screen.width-333)this.width=screen.width-333;">

著實今世碼為:

import java.text.DecimalFormat;

class TestSphere

{

public static void main(String[] args)

{

EasyReader console = new EasyReader();

System.out.print("Enter the radius: ");

double radius = console.readDouble();

DecimalFormat f3 = new DecimalFormat("0.000");

Sphere balloon = new Sphere(0, 0, radius);

System.out.println();

System.out.println(balloon);

System.out.println("Volume = " + f3.fwww9778conormat(balloon.volume()));

System.out.println("Surface area = " + f3.format(balloon.surfaceArea()));

System.out.println();

}

}

該法度榜樣的種種之間的互相關系可以表示為:

screen.width-333)this.width=screen.width-333;">

該法度榜樣的UML序列圖可以表示如下。有關UML序列圖的設計與利用,請查閱作者的另文(《使用UML序列圖設計Java利用法度榜樣詳解》)

screen.width-333)this.width=screen.width-333;">

以下是法度榜樣的輸出結果。

screen.width-333)this.width=screen.width-333;">

四、設計圖形用戶界面(GUI)

以上法度榜樣使用面向工具編程的設計措施,較好地實現了Java利用法度榜樣。但美中不夠的是,沒有圖形用戶界面供用戶應用。

假如我們要增添設計圖形用戶界面,我們就可以將類Sphere交給一個法度榜樣員去實現,將用戶界面SphereWindow交給別的一個法度榜樣員去開拓,這便是面向工具編程的好處,可以進行團隊開拓,而不是作坊式的傳統的法度榜樣開拓要領。

以下是用戶界面SphereWindow的實今世碼:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.border.*;

import java.text.DecimalFormat;

public class SphereWindow extends JFrame

implements ActionListener

{

private JTextField radiusIn, volumeOut, surfAreaOut;

private Sphere balloon;

private DecimalFormat f3 = new DecimalFormat("0.000");

public SphereWindow()

{

super("Spheres: volume and surface area");

JPanel view = new JPanel();

view.setLayout(new GridLayout(6, 2, 10, 10));

view.setBorder(new EmptyBorder(10, 10, 10, 10));

view.add(new JLabel("Radius = ", SwingConstants.RIGHT));

radiusIn = new JTextField(8);

radiusIn.setBackground(Color.yellow);

radiusIn.addActionListener(this);

view.add(radiusIn);

view.add(new JLabel("Volume = ", SwingConstants.RIGHT));

volumeOut = new JTextField(8);

volumeOut.setEditable(false);

volumeOut.setBackground(Color.white);

view.add(volumeOut);

view.add(new JLabel("Surface area = ", SwingConstants.RIGHT));

surfAreaOut = new JTextField(8);

surfAreaOut.setEditable(false);

surfAreaOut.setBackground(Color.white);

view.add(surfAreaOut);

view.add (new JPanel()); // reserved

Container c = getContentPane();

c.add(view, BorderLayout.CENTER);

balloon = new Sphere(0,0,100);

}

public void actionPerformed(ActionEvent e)

// Called automatically when the user

// strikes Enter on the input field

{

String s = radiusIn.getText();

double r = Double.parseDouble(s);

balloon.setRadius(r);

radiusIn.setText(" " + f3.format(r));

volumeOut.setText(" " + f3.format(balloon.volume()));

surfAreaOut.setText(" " + f3.format(balloon.surfaceArea()));

}

public static void main(String[] args)

{

SphereWindow w = new SphereWindow();

w.setSize(300, 250);

w.addWindowListener(new ExitButtonListener());

w.show();

}

}

ExitButtonListener類的代碼為:

import java.awt.event.*;

public class ExitButtonListener extends WindowAdapter

{

publicwww9778con void windowClosing(WindowEvent e)

{

System.exit(0);

}

}

經由過程實現上述法度榜樣,使用面向工具編程的設計措施,我們將Model(模型,這里是Sphere)與view(視圖,這里是SphereWindow)分分開來。并較好地實現了類的封裝,類的重用(Sphere)。便于團隊開拓,迅速前進開拓效率。

摘自:ZDnet   光陰:2003年9月10日

免責聲明:以上內容源自網絡,版權歸原作者所有,如有侵犯您的原創版權請告知,我們將盡快刪除相關內容。

您可能還會對下面的文章感興趣:

建体彩网
闲来麻将房卡怎么赠送别人 银河棋牌官方网站下载 河南省彩票中心三d 极速时时彩 度假村的别墅能赚钱吗 广东36选7最新开奖查询好彩3 广东11选5玩法 半全场 广东26选5 大都会彩票苹果