目录

  • 1 Java语言概述
    • 1.1 Java语言的特点
    • 1.2 Java虚拟机
    • 1.3 Java开发环境
    • 1.4 编译执行和解释执行
    • 1.5 第一个java程序1
    • 1.6 第一个java程序2
    • 1.7 第一个java程序3
    • 1.8 章节测验
  • 2 Java基础语法
    • 2.1 基本数据类型
    • 2.2 赋值语句
    • 2.3 表达式
    • 2.4 运算符
    • 2.5 类型转换
    • 2.6 章节测验
  • 3 类与对象
    • 3.1 面向对象的概念
    • 3.2 类和对象的概念
    • 3.3 构造方法
    • 3.4 章节测验
  • 4 继承与多态
    • 4.1 继承
    • 4.2 多态
    • 4.3 final关键字
    • 4.4 static关键字
    • 4.5 抽象类
    • 4.6 接口
    • 4.7 内部类
    • 4.8 章节测验
  • 5 数组和字符串
    • 5.1 数组
    • 5.2 字符串
    • 5.3 章节测验
  • 6 常用类与接口
    • 6.1 课件
    • 6.2 重要知识点
  • 7 异常处理
    • 7.1 课件
    • 7.2 重要知识点
  • 8 文件和流
    • 8.1 课件
    • 8.2 重要知识点
  • 9 Elements of Programming
    • 9.1 You First Program
      • 9.1.1 Exercise
      • 9.1.2 Program
    • 9.2 Built-in Types of Data
      • 9.2.1 Exercise
      • 9.2.2 Program
    • 9.3 Conditionals and Loops
      • 9.3.1 Exercise
      • 9.3.2 Program
    • 9.4 Arrays
      • 9.4.1 Exercise
      • 9.4.2 Program
    • 9.5 Input and Output
      • 9.5.1 Exercise
      • 9.5.2 Program
    • 9.6 Case Study: Random Web Surfer
  • 10 Functions
    • 10.1 Static Methods
      • 10.1.1 Exercise
      • 10.1.2 Program
    • 10.2 Libraries and Clients
      • 10.2.1 Exercise
      • 10.2.2 Program
    • 10.3 Recursion
      • 10.3.1 Exercise
      • 10.3.2 Program
    • 10.4 Case Study: Percolation
  • 11 Object-Oriented Programming
    • 11.1 Using Data Types
    • 11.2 Creating Data Types
    • 11.3 Designing Data Types
    • 11.4 Case Study: N-Body Simulation
  • 12 参考资料
    • 12.1 主要参考书
    • 12.2 慕课资源
Libraries and Clients

Each program that you have written consists of Java code that resides in a single .java file. For large programs, keeping all the code in a single file is restrictive and unnecessary. (不能把所有代码都放在一个文件中,也不可能) Fortunately, it is very easy in Java to refer to a method in one file that is defined in another. This ability has two important consequences on our style of programming:

  • It allows us to extend the Java language by developing libraries of static methods for use by any other program, keeping each library in its own file. (静态方法库)

  • It enables modular programming, where we divide a program up into static methods, grouped together in some logical way. (模块化编程)


Using static methods in other programs.

To refer(引用) to a static method in one class that is defined in another, you must make both classes accessible to Java (for example, by putting them both in the same directory in your computer). Then, to call a method, prepend its class name and a period separator. For example, SAT.java calls the cdf() method in Gaussian.java, which calls the pdf() method,  which calls the exp() and sqrt() methods in Math.

We describe several details about the process.

  • The public keyword. The public modifier identifies the method as available for use by any other program. You can also identify methods as private, but you have no reason to do so at this point.


  • Each module is a class. We use the term module to refer to all the code that we keep in a single file. By convention, each module is a Java class that is kept in a file with the same name of the class but has a .java extension. In this chapter, each class is merely a set of static methods.


  • The .class file. When you compile the program, the Java compiler makes a file with the class name followed by a .class extension that has the code of your program in a language more suited to your computer.


  • Compile when necessary. When you compile a program, Java typically compiles everything that needs to be compiled in order to run the program. For example, when you type javac SAT.java, the compiler will also check whether you modified Gaussian.java since the last time it was compiled. If so, it will also compile Gaussian.


  • Multiple main() methods. Both SAT.java and Gaussian.java have their own main() method. When you type java followed by a class name, Java transfers control to the machine code corresponding to the main() method defined in that class. (有多个main函数执行哪个?)


Libraries.

 We refer to a module whose methods are primarily intended for use by many other programs as a library.

  • Clients. We use the term client to refer to a program that calls a given library method.

  • APIs. Programmers normally think in terms of a contract between the client and the implementation that is a clear specification of what the method is to do.

  • Implementations. We use the term implementation to describe the Java code that implements the methods in an API. (结合上图理解三个名词)


As an example, Gaussian.java is an implementation of the following API:

高斯分布概率密度函数(PDF)和累积分布函数(CDF)

https://blog.csdn.net/renwudao24/article/details/44465407


Random numbers.

 StdRandom.java is a library for generating random numbers from various distributions.

设置随机数种子、均匀分布、高斯分布、置乱(运行并阅读代码)


Input and output for arrays.

 StdArrayIO.java is a library for reading arrays of primitive types from standard input and printing them to standard output.


Iterated function systems.

An Iterated function system (IFS迭代函数系统) is a general way to produce fractals(分形) like the Sierpinski triangle or Barnsley fern. As a first example, consider the following simple process: Start by plotting a point at one of the vertices of an equilateral triangle. Then pick one of the three vertices(顶点) at random and plot a new point halfway between the point just plotted and that vertex. Continue performing the same operation.

Sierpinski.java simulates this process. Below are snapshots after 1,000, 10,000, and 100,000 steps.





Standard statistics.

 StdStats.java is a library for statistical calculations and basic visualizations.