Java 程序剖析
Anatomy of a Java program
以下class中每个表达式的术语是什么:
例如:
class Test {
int a=0;
void method(boolean boo){
String b="";
try
{
new Thread().sleep(1000);
}
catch(InterruptedException e){}
JOptionPane.showMessageDialog(null,"test");
BufferedImage image=ImageIO.read(new File("C:\file.png"));
}
}
据我所知a
是一个字段,boo
是一个参数,b
和 image
是 局部变量 .
用于
的术语是什么
new Thread().sleep()
JOptionPane.showMessageDialog()
ImageIO.read()
new File()
InterruptedException
new Thread()
is a constructor call. You are immediately calling sleep
on the instance. This is odd, since sleep
is a static method, so you can just do Thread.sleep(1000)
用于静态调用(确保捕获并处理 InterruptedException
.
JOptionPane.showMessageDialog()
is a static method call as is ImageIO.read()
. new File()
是 returns 一个 File
对象的构造函数调用。
关于命名约定的简单说明:字段名称(不是 static final
常量)、变量名称和方法名称应为 camelCase
,而 class 名称应为 PascalCase
.
你可能还需要注意程序的顶部,你会发现String、Thread(你平时可能看不到这两个)、JOptionPane、ImageIO和BufferedImage是从某处导入的。
为了更好地编程,您需要遵循命名约定请看这个:http://www.oracle.com/technetwork/java/codeconventions-135099.html
另外请在您的 class 变量和方法前添加修饰符
new Thread().sleep()
:
这是一个由两部分组成的表达式。第一部分,即 new Thread()
实际上在 Java Language Specification (JLS) 中记录为不合格的 class 实例创建表达式:
Class instance creation expressions have two forms:
Unqualified class instance creation expressions begin with the
keyword new.
An unqualified class instance creation expression may be used to
create an instance of a class, regardless of whether the class is a
top level (§7.6), member (§8.5, §9.5), local (§14.3), or anonymous
class (§15.9.5).
当您说 new Thread()
时,您基本上创建了 Thread
class 的实例。语句的第二部分是 .sleep()
,称为方法调用。
new File()
:
这也是一个不合格的 class 实例创建表达式,就像表达式 new Thread()
一样,但是创建 File
class 的实例而不是 Thread
class.
JOptionPane.showMessageDialog()
:
如果您查看 JOptionPane.showMessageDialog method, you will see that the method is a static
method. Also, the JLS 的源代码,会解释如何访问静态方法:
A class method is always invoked without reference to a particular object.
JLS 间接表示的是,您可以访问 class 之外的 static
方法,其中使用 class 的名称定义它。
这里要理解的另一个重要事实是有明确定义的 Java 命名约定。
- Class 名称以大写字母开头,所有后续单词均大写。因此,当您看到
XyzAbc
形式的任何文本时,假设它是 class
或 interface
。例如,JOptionPane
和 Image
是 class 名称。
- 同样,对于方法名称也有一个约定,称为驼峰式大小写。比如,只要看到
doSomething
、getSomething
、setSomething
、showMessageDialog()
..这样的文字,就应该知道这是一个方法。
将所有这些理解放在一起,我们可以推断出 JOptionPane.showMessageDialog()
从 JOptionPane
调用了一个 static
方法。
InterruptedException
:
如果您了解上面解释的命名约定,您现在应该知道 InterruptedException
是 class
。它与 Java
中任何其他 class 的不同之处在于,它可以使用 throw
子句抛出,然后使用 try-catch
语句捕获。您可以在 Oracle 文档中阅读有关异常处理的更多信息。
以下class中每个表达式的术语是什么:
例如:
class Test {
int a=0;
void method(boolean boo){
String b="";
try
{
new Thread().sleep(1000);
}
catch(InterruptedException e){}
JOptionPane.showMessageDialog(null,"test");
BufferedImage image=ImageIO.read(new File("C:\file.png"));
}
}
据我所知a
是一个字段,boo
是一个参数,b
和 image
是 局部变量 .
用于
的术语是什么new Thread().sleep()
JOptionPane.showMessageDialog()
ImageIO.read()
new File()
InterruptedException
new Thread()
is a constructor call. You are immediately calling sleep
on the instance. This is odd, since sleep
is a static method, so you can just do Thread.sleep(1000)
用于静态调用(确保捕获并处理 InterruptedException
.
JOptionPane.showMessageDialog()
is a static method call as is ImageIO.read()
. new File()
是 returns 一个 File
对象的构造函数调用。
关于命名约定的简单说明:字段名称(不是 static final
常量)、变量名称和方法名称应为 camelCase
,而 class 名称应为 PascalCase
.
你可能还需要注意程序的顶部,你会发现String、Thread(你平时可能看不到这两个)、JOptionPane、ImageIO和BufferedImage是从某处导入的。
为了更好地编程,您需要遵循命名约定请看这个:http://www.oracle.com/technetwork/java/codeconventions-135099.html
另外请在您的 class 变量和方法前添加修饰符
new Thread().sleep()
:
这是一个由两部分组成的表达式。第一部分,即 new Thread()
实际上在 Java Language Specification (JLS) 中记录为不合格的 class 实例创建表达式:
Class instance creation expressions have two forms:
Unqualified class instance creation expressions begin with the keyword new.
An unqualified class instance creation expression may be used to create an instance of a class, regardless of whether the class is a top level (§7.6), member (§8.5, §9.5), local (§14.3), or anonymous class (§15.9.5).
当您说 new Thread()
时,您基本上创建了 Thread
class 的实例。语句的第二部分是 .sleep()
,称为方法调用。
new File()
:
这也是一个不合格的 class 实例创建表达式,就像表达式 new Thread()
一样,但是创建 File
class 的实例而不是 Thread
class.
JOptionPane.showMessageDialog()
:
如果您查看 JOptionPane.showMessageDialog method, you will see that the method is a static
method. Also, the JLS 的源代码,会解释如何访问静态方法:
A class method is always invoked without reference to a particular object.
JLS 间接表示的是,您可以访问 class 之外的 static
方法,其中使用 class 的名称定义它。
这里要理解的另一个重要事实是有明确定义的 Java 命名约定。
- Class 名称以大写字母开头,所有后续单词均大写。因此,当您看到
XyzAbc
形式的任何文本时,假设它是class
或interface
。例如,JOptionPane
和Image
是 class 名称。 - 同样,对于方法名称也有一个约定,称为驼峰式大小写。比如,只要看到
doSomething
、getSomething
、setSomething
、showMessageDialog()
..这样的文字,就应该知道这是一个方法。
将所有这些理解放在一起,我们可以推断出 JOptionPane.showMessageDialog()
从 JOptionPane
调用了一个 static
方法。
InterruptedException
:
如果您了解上面解释的命名约定,您现在应该知道 InterruptedException
是 class
。它与 Java
中任何其他 class 的不同之处在于,它可以使用 throw
子句抛出,然后使用 try-catch
语句捕获。您可以在 Oracle 文档中阅读有关异常处理的更多信息。