欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java: the Complete of reference 9 edtion——chapter 2

程序员文章站 2022-03-22 23:37:10
...

目录

chapter 2:An Overview of Java

2.1 Object-Oriented Programming//面对对象编程

2.2 A First Simple Program//第一个简单程序

2.3 A Second Short Program//

2.4 Two Control Statements//两种控制语句

2.5Using Blocks of Code//块代码

2.6 Lexical Issues//词汇

2.7The Java Class Libraries//Java的类库


2.1 Object-Oriented Programming//面对对象编程

2.1.1:面向对象编程(OOP)是Java的核心.事实上,所有Java程序至少在某种程度上都是面向对象的.Oop对于Java来说是不可或缺的,所以最好理解它的基本在你开始编写简单的Java程序之前的原则。因此,本章首先讨论OOP的理论方面。

To manage increasing complexity, the second approach, called object-oriented programming,
was conceived. Object-oriented programming organizes a program around its data (that is,
objects) and a set of well-defined interfaces to that data. An object-oriented program can
be characterized as data controlling access to code. As you will see, by switching the controlling
entity to data, you can achieve several organizational benefits.

面对对象设计的基本过程:

  1. 分析实际需要解决的问题

  2. 从中提取出需要设计的问题

  3. 然后编写这些对象所对应的类

  4. 然后通过集成这些对象的功能解决需要求解的问题

即:问题——抽出对象——建立对应类——集成功能——解决问题

2.1.2:抽象

面向对象编程的一个基本要素是抽象.人类通过抽象来管理复杂性。例如,人们不认为一辆车是一套数以万计的不可分割的东西。所有零件。他们认为它是一个定义明确的对象,它有自己独特的行为。这种抽象允许人们使用汽车去杂货店,而不会被复杂的东西淹没。汽车的零件。他们可以忽略发动机、变速器和制动系统的工作细节。相反,它们可以*地将对象作为一个整体使用。

Object-oriented concepts form the heart of Java just as they form the basis for human
understanding. It is important that you understand how these concepts translate into
programs. As you will see, object-oriented programming is a powerful and natural paradigm
for creating programs that survive the inevitable changes accompanying the life cycle of any
major software project, including conception, growth, and aging. For example, once you
have well-defined objects and clean, reliable interfaces to those objects, you can gracefully
decommission or replace parts of an older system without fear

2.1.3:The Three OOP Principles(面对对象编程的三大特性)

Encapsulation  封装、Inheritance  继承、Polymorphism  多态

2.2 A First Simple Program//第一个简单程序

 This is a simple Java program.
 Call this file "Example.java".
*/
class Example {
 // Your program begins with a call to main().
 public static void main(String args[]) {
 System.out.println("This is a simple Java program.");
 }
}

2.3 A Second Short Program//

/*
 Here is another short example.
 Call this file "Example2.java".
*/
class Example2 {
 public static void main(String args []) {
 int num; // this declares a variable called num
 num = 100; // this assigns num the value 100
 System.out.println("This is num: " + num);
 num = num * 2;
 System.out.print("The value of num * 2 is ");
 System.out.println(num);
 }
}

2.4 Two Control Statements//两种控制语句

IF语句:The Java if statement works much like the IF statement in any other language. Further, it is
syntactically identical to the if statements in C, C++, and C#. Its simplest form is shown here:

if (condition)    statement   ;// if 简单语句

Here, condition is a Boolean expression. If condition is true, then the statement is executed.
If condition is false, then the statement is bypassed. Here is an example:
if(num < 100) System.out.println("num is less than 100");

The for 循环:The simplest form of the for loop is shown here:

for  (initialization 初始 ; condition 判断条件 ; iteration 重复的语句)    statement;  // for 简单语句

In its most common form, the initialization portion of the loop sets a loop control
variable to an initial value
. The condition is a Boolean expression that tests the loop control
variable. If the outcome of that test is true, the for loop continues to iterate. If it is false,
the loop terminates. The iteration expression determines how the loop control variable is
changed each time the loop iterates
.

2.5Using Blocks of Code//块代码

Java allows two or more statements to be grouped into blocks of code, also called code blocks.
This is done by enclosing the statements between opening and closing curly braces.

(Java允许将两个或多个语句分组为代码块,也称为代码块。这是通过附上开始和结束大括号之间的陈述来完成的。)

例如:

/*
 Demonstrate a block of code.
 Call this file "BlockTest.java"
*/
class BlockTest {
 public static void main(String args[]) {
 int x, y;
 y = 20;
 // the target of this loop is a block
 for(x = 0; x<10; x++) {
 System.out.println("This is x: " + x);
 System.out.println("This is y: " + y);
 y = y - 2;
 }
 }
}

2.6 Lexical Issues//词汇

现在您已经看到了几个简短的Java程序,现在是更正式地描述Java的原子元素的时候了。Java程序是空白、标识符、文字、注释的集合,运算符、分隔符和关键字。下一章将描述这些运算符。接下来将对其他部分进行描述。

空格whitespace :Java is a free-form language.In Java, whitespace is a space, tab, or newline.

标识符Identifiers:标识符可以是大写和小写字母、数字或下划线和美元符号字符的任何描述序列。(美元符号字符不打算用于一般用途。) 它们不得以数字开头,以免与数字文字混淆。同样,Java是区分大小写的。

书写的常量Literals:Java中的常量值是通过使用它的文字表示创建的。

注释Comments:如前所述,Java定义了三种类型的注释。你已经看到了两条:单行/  /和多行。第三种类型称为文档注释。

单行注释://         ;  文档注释:/**         */

分隔符Separators( 括号){  花括号}[ 方括号 ];分号 ,  逗号. 点操作符

关键字keywordJava: the Complete of reference 9 edtion——chapter 2

2.7The Java Class Libraries//Java的类库

 In Part II, several class libraries are described in detail.