计算与软件工程I12 数字和静态
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
24
2020/11/27
Dong Shao, Nanjing Unviersity
25
Wrapping a primitive
2020/11/27
Dong Shao, Nanjing Unviersity
26
2020/11/27
Dong Shao, Nanjing Unviersity
27
Autoboxing
2020/11/27
Dong Shao, Nanjing Unviersity
21
2020/11/27
Dong Shao, Nanjing Unviersity
22
2020/11/27
Dong Shao, Nanjing Unviersity
23
Math methods
2020/11/27
Dong Shao, Nanjing Unviersity
2020/11/27
Dong Shao, Nanjing Unviersity
2
2020/11/27
Dong Shao, Nanjing Unviersity
3
Static methods
• Java is object-oriented, but there is a special case where there is no need to have an instance of the class.
2020/11/27
Dong Shao, Nanjing Unviersity
20
final 方法
• 使用final 方法的原因有两个。
– 第一个原因是把方法锁定,以预防任 何继承类修改它的意义。这是出于设 计的考虑:你想要确保在继承中方法 行为保持不变,并且不会被重载。
– 使用final方法的第二个原因是效率。 如果你将一个方法指明为final,就是 同意编译器将针对该方法的所有调用 都转为内嵌(inline)调用。
– Static variables in a class are initialized before any object of that class can be created.
– Static variables in a class are initialized before any static method of the class runs.
2020/11/27
Dong Shao, Nanjing Unviersity
19
final 参数
• class Gizmo { • public void spin() {} •} • public class FinalArguments { • void with(final Gizmo g) { • //! g = new Gizmo(); // Illegal -- g is final •} • void without(Gizmo g) { • g = new Gizmo(); // OK -- g not final • g.spin(); •} • // void f(final int i) { i++; } // Can't change • // You can only read from a final primitive: • int g(final int i) { return i + 1; } • public static void main(String[] args) { • FinalArguments bf = new FinalArguments(); • bf.without(null); • bf.with(null); •} • } ///:~
其他 public 类,就必须使用关键字 import 来使得 mypackage 中的名称可以被使用。另一个选择是给出完整 的名称:
• mypackage.MyClass m = new mypackage.MyClass(); • 关键字 import 可使之更加简洁: • import mypackage.*; • // . . . • MyClass m = new MyClass();
2020/11/27
Dong Shao, Nanjing Unviersity
41
11
2020/11/27
Dong Shao, Nanjing Unviersity
12
A single copy for static variable
2020/11/27
Dong Shao, Nanjing Unviersity
13
Initializing a static variable
• The keyword static let a method run without any instance of the class.
• A static method means “behavior not dependent on an instance variable, so no instance/object is required. Just the class.”
2020/11/27
Dong Shao, Nanjing Unviersity
16
2020/11/27
Dong Shao, Nanjing Unviersity
17
2020/11/27
Dong Shao, Nanjing Unviersity
18
空白final
• class Poppet { • private int i; • Poppet(int ii) { i = ii; } •} • public class BlankFinal { • private final int i = 0; // Initialized final • private final int j; // Blank final • private final Poppet p; // Blank final reference • // Blank finals MUST be initialized in the constructor: • public BlankFinal() { • j = 1; // Initialize blank final • p = new Poppet(1); // Initialize blank final reference •} • public BlankFinal(int x) { • j = x; // Initialize blank final • p = new Poppet(x); // Initialize blank final reference •} • public static void main(String[] args) { • new BlankFinal(); • new BlankFinal(47); •} • } ///:~
Dong Shao, Nanjing Unviersity
14
2020/11/27
Dong Shao, Nanjing Unviersity
15
Static final variables are constants
• A variable marked final means that – once initialized - it can never change.
2020/11/27
Dong Shao, Nanjing Unviersity
28
2020/11/27
Dong Shao, Nanjing Unviersity
29
Autoboxing works almost everywhere
2020/11/27
Dong Shao, Nanjing Unviersity
2020/11/27
• Static variables are initialized when a class is loaded.
• A class is loaded because the JVM decides it's time to load it.
• And there are two guarantees about static initialization:
33
Turing a primitive number into a String
2020/11/27
Dong Shao, Nanjing Unviersity
34
Date
2020/11/27
Dong Shao, Nanjing Unviersity
35
2020/11/27
Dong Shao, Nanjing Unviersity
36
2020/11/27
Dong Shao, Nanjing Unviersity
37
2020/11/27
Dong Shao, Nanjing Unviersity
38
static import
• Saving typing • Harder to read • Naming conflicts
2020/11/27
Dong Shao, Nanjing Unviersity
39
包
• 定义:
– 包(package)是类的容器,用来保存 划分的类名空间
• 声明
– package pkg1[.pkg2[.pkg3]];
2020/11/27
Dong Shao, Nanjing Unviersity
• Marking the constructor private. • You are free to combine static and
non-static methods in a class.
2020/11/27
Dong Shao, Nanjing Unviersity
7
2020/11/27
30
2020/11/27
Dong Shao, Nanjing Unviersity
31
2020/11/27
Dong Shao, Nanjing Unviersity
32
Wrappers have static utility mehtods
2020/11/27
Dong Shao, Nanjing Unviersity
40
包的使用
• 假设文件的名称是 MyClass.java,这就意味着在该文件中 有且只有一个 public类,该类的名称必须是 MyClass(注 意大小写):
• package mypackage; • public class MyClass { • // . . . • 现在,如果有人想用 MyClass 或者是mypackage 中的任何
2020/11wk.baidu.com27
Numbers and statics
Dong Shao, Nanjing Unviersity
1
Math methods
• There’s no global thing in Java. • Math methods never use instance
variables.
Dong Shao, Nanjing Unviersity
8
2020/11/27
Dong Shao, Nanjing Unviersity
9
2020/11/27
Dong Shao, Nanjing Unviersity
10
2020/11/27
Dong Shao, Nanjing Unviersity
2020/11/27
Dong Shao, Nanjing Unviersity
4
2020/11/27
Dong Shao, Nanjing Unviersity
5
2020/11/27
Dong Shao, Nanjing Unviersity
6
Static methods
• Often, a class with static methods is not meant to be instantiated.