首页 > 编程知识 正文

java多态,java面向对象编程总结

时间:2023-05-06 08:51:16 阅读:116740 作者:3802

Java是面向对象的语言,是实现面向对象编程的强大工具。 但是,如何在编程中真正发挥最大的性能呢? 本文详细介绍了如何通过实际的Java程序开发过程使用面向对象实现Java编程。

实现的Java APP应用程序将在用户键入球体半径时显示球体的体积和表面积。 在阅读以下内容之前,请自己思考一下如何设计Java APP应用程序。

一、普通实现方法

我相信大多数程序员实现上述功能的程序都会非常快速、自信地采用以下实现代码。

class Sphere

{

publicstaticvoidmain (字符串[ ] args ) )。

{

easyreaderconsole=neweasyreader (;

系统. out.print (entertheradius : );

double radius=console.read double (;

system.out.println (radius=' radius );

双卷=4.0/3.0 * math.pi * radius * radius * radius;

system.out.println (volume=' volume );

doublesurfarea=4.0 * math.pi * radius * radius;

system.out.println (surface area=' surf area );

}

}

EasyReader类代码如下:

import java.io.*;

公共类任务读取器

{

保护性字符串myfilename;

protectedbufferedreadermyinfile;

保护性int my error flags=0;

protectedstaticfinalintopenerror=0x 0001;

protectedstaticfinalintcloseerror=0x 0002;

protectedstaticfinalintreaderror=0x 0004;

protectedstaticfinalinteof=0x 0100;

//*

* constructor.prepares console (system.in ) for reading

*/

公共easy reader (

{

myFileName=null;

my错误标志=0;

myInFile=new BufferedReader (

newinputstreamreader(system.in ),128;

}

//*

* constructor.opensafileforreading

* @ paramfilenamethenameorpathnameofthefile

*/

publiceasyreader (字符串文件名)。

{

myFileName=fileName;

my错误标志=0;

特里

{

myinfile=newbufferedreader (new filereader ) filename ) 1024;

}

文件基础知识(catch ) )。

{

我的错误标志|=openerror;

myFileName=null;

}

}

//*

* Closes the file

*/

公共语音关闭(

{

if (myFileName==null )

返回;

特里

{

myInFile.close (;

}

catch(ioexceptione ) )。

{

系统. err.println (error closing ' myfilename )。

();

我的错误标志|=close error;

}

}

//*

* 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 null character

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

*/

public char readChar()

{

char ch = 'u0000';

try

{

if (ready())

{

ch = (char)myInFile.read();

}

}

catch (IOException 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 bldsg 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 bldsg 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.独特的母鸡(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 bldsg 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 bldsg 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;

}

}

以下是程序的输出结果画面。

在上述程序中,实现了球体表面积和体积的算法,并通过EasyReader类轻松实现了控制台的输入,但是该程序只是完成了程序所要求的功能,并计算出了结果。该程序不是一个好的设计程序,它既没采用结构化编程,也没有发挥Java语言的强大效能,更不是面向对象编程。

实际上,从程序设计的角度来看,以上程序是一个非常糟糕的设计程序。首先,用户界面与计算公式混合在一起。请记住:对于任何编程语言,用户界面必须与计算或处理过程分离开来。另外,输出的结果太难看,数值包括太多的小数。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。