Chapter 5 Defining Classes II
5.1 Static Methods and Static Variables
(1)Static Methods
例如:
NB:(1)在静态的方法中,比如main中,调用静态的方法,可以直接使用class_name.method_name() 的形式,但是如果要调用非静态的方法在静态的方法中,那么需要创建一个该非静态方法的类的对象,然后使用该对象对函数进行调用即可。
(2)main函数的位置,可以在创建一个类,比如上文的RoundStuffDemo。也可以放在RoundStuff类中。
--------------------------------------------------------------------------------------------------------------------------
(2)Static Variables
A static variable is a variable that belongs to the class as a whole and not just to one object
However, with a static variable, there is only one copy of the variable, and all the objects can use this one variable.
Thus, a static variable can be used by objects to communicate between the objects.One object can change the static variable, and another object can read that change.
语法:
NB:
As we already noted, you cannot directly access an instance variable within the definition of a static method. However, it is perfectly legal to access a static variable within a static method, because a static variable belongs to the class as a whole.
输出:
--------------------------------------------------------------------------------------------------------------------------
5.2 The Math Class
The class Math provides a number of standard mathematical methods. The class Math is provided automatically and requires no import statement. The Math class is in the java.lang package, so it requires no import statement
常见的函数:(比如使用Math.函数名:因为都是静态函数;如果想使用PI(π),也必须是用Math.PI)
--------------------------------------------------------------------------------------------------------------------------
Wrapper Classes
Wrapper classes provide a class type corresponding to each of the primitive types so that you can have an object of a class type that behaves somewhat like its corresponding value of a primitive type
To convert a value of a primitive type to an “equivalent” value of a class type, you create an object of the corresponding wrapper class using the primitive type value as an argument to the wrapper class constructor
If you want to convert an int value, such as 42 , to an object of type Integer , you can do so as follows:
Integer integerObject = new Integer(42);
--------------------------------------------------------------------------------------------------------------------------
Automatic Boxing and Unboxing
boxing: a primitive type(int)--> a object of(Integer)
比如:Integer numberOfSamuri = 47; Double price = 499.99; Character grade = 'A'; 即可
unboxing:a object of(Integer)--> a primitive type(int)
比如:Integer numberOfSamuri = new Integer(47); int n = numberOfSamuri; <==> Integer numberOfSamuri = new Integer(47);
int n = numberOfSamuri.intValue();
NB:Java automatically applies the appropriate accessor method ( intValue , doubleValue , or charValue in these cases) to obtain the value of the primitive type that is assigned to the variable
--------------------------------------------------------------------------------------------------------------------------
Character 常用的函数:
--------------------------------------------------------------------------------------------------------------------------
5.2 References and Class Parameters
(1)Variables and Memory
A computer has two forms of memory called secondary memory and main memory.
The secondary memory is used to hold files for more or less permanent storage.
The main memory is used by the computer when it is running a program
Main memory consists of a long list of numbered locations called bytes, each containing eight bits; that is, eight 0/1 digits.
The number that identifies a byte is called its address
基本类型的变量和类的对象在存储上的不同?
对于基元类型的变量,变量的值存储在内存中。但是,类类型的变量只存储对象所在的内存地址。
The object named by the variable is stored in some other location in memory, and the variable contains only the memory address of where the object is stored.This memory address is called a reference (to the object).
对于基本类型,比如int,存储的数字有大小的限制,因此占据的内容的大小也有限制,但是对于一个对象来说,因为他存储的不是具体的数字内容,而是这个数字内容的存储地址,这样一来,一个类的对象其实就没有了存储空间大小的限制。
比如:看一个类的对象的地址存储
--------------------------------------------------------------------------------------------------------------------------
(2) class parameters
当类的对象作为传参的对象的时,结果和传递数组相同。因为二者的存储地址都是一个引用地址,因此对传递参数值的改变,也相当于是改变了该对象或者数组中的值,即使不用return的时候。但是如果传递的是int类型的数据,那么必须有return的返回值,否则改变的只是函数中的local variable,调用该函数的参数没有发生改变。
--------------------------------------------------------------------------------------------------------------------------
(3) The Constant null
比如:
if (yourObject == null)
System.out.println("No real object here.");
pitfall:null不是一个对象,所以如果初始化是一个nul值,那么不可以使用这个对象进行函数的使用
因为会报错:“Null Pointer Exception” 。比如下面的代码会报错。
--------------------------------------------------------------------------------------------------------------------------
(4)The new Operator and Anonymous Objects
--------------------------------------------------------------------------------------------------------------------------
(5)import
当我们使用:import java.util.Scanner; 这种语句进行类的导入时,如果我们想使用一个静态的方法,我们需要使用它的类名_方法名(Character.toUpperCase(firstCharacter);但是如果我们使用(import static java.lang.Character.toUpperCase;),同样使用该方法可以用简单的形式(toUpperCase(firstCharacter))
--------------------------------------------------------------------------------------------------------------------------
5.4 Packages and javadoc
(1)import
关于import的理解,可以参考:https://blog.csdn.net/qq_36098284/article/details/100524144
--------------------------------------------------------------------------------------------------------------------------
(2)package
A package name is not an arbitrary identifier(任意的标识符). It is a form of path name to the directory containing the classes in the package.
In order to find the directory for a package, Java needs two things: the name of the package and the value of your CLASSPATH variable.
--------------------------------------------------------------------------------------------------------------------------
(3)the default package
当前目录中的所有类(不属于其他包)都属于名为默认包的未命名包。只要当前目录在类路径上,默认包中的所有类都自动对代码可用。这就是为什么我们总是假设我们定义的所有类都在同一个目录中
--------------------------------------------------------------------------------------------------------------------------
5.5 Introduction to javadoc
(1)介绍
javadoc文件的作用相当于API,就是告诉使用者这个类是做什么的,怎么用,返回什么值。很多时候还会介绍其他信息,比如作者,版本,参数等。
Java comes with a program named javadoc that automatically extracts the interface from a class definition. If your class definition is correctly commented, a programmer using your class need only look at this API (documentation) produced
by javadoc .The documentation produced by javadoc is identical in format to the documentation for the standard Java library classes.
The result of running javadoc on a class is to produce an HTML file with the API (interface) documentation for the class
注释满足的条件才会被提取出形成javadoc文件。
For javadoc (in default mode) to extract a comment(注释), the comment must satisfy two conditions:
1. The comment must immediately precede a public class definition, a public method definition, or other public item.
2. The comment must be a block comment (that is, the /* and */ style of comment), and the opening /* must contain an extra *. So the comment must be marked by /** at the beginning and */ at the end.
--------------------------------------------------------------------------------------------------------------------------
(2)书写规则
该注释中的信息的书写规则是: @ tags.
说明:
1.多个参数,每个参数分别一行
If there are multiple parameters, they should each have their own @param and appear on a separate line
2.如果@deprecated包含在方法注释中,则文档会注意到该方法已被弃用。不推荐使用的方法是正在逐步淘汰的方法。
比如:下面的例子,就是先介绍这个函数的作用,参数和返回值。同时还有该函数的heading
--------------------------------------------------------------------------------------------------------------------------
(3)如何运行
语法:javadoc –d Documentation_Directory Package_Name
NB:1. The Documentation_Directory is the name of the directory in which you want javadoc to place the HTML documents that it produces.
2. If you omit the - d and Documentation_Directory , javadoc will create suitable directories for the documentation.
3.You can link to standard Java documents so that your HTML documents include live links to standard classes and methods.
语法:javadoc –link Link_To_Standard_Docs –d Documentation_Directory Package_Name
4.You need not run javadoc on an entire package. You can run javadoc on a single class file
For example, the following should be run from the directory containing Date.java and will produce documentation for the class Date : javadoc Date.java
You can run javadoc on all classes in a directory with: javadoc *.java
You can add the -d and/or -link options to any of these commands.
For example:javadoc -link http://download.oracle.com/javase/8/docs/api/ -d mydocs *.java
关于javadoc中文详解:https://blog.csdn.net/vbirdbest/article/details/80296136
--------------------------------------------------------------------------------------------------------------------------