J04_初始化与清除
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
以返回值区分方法重载
能否考虑通过返回值来区分重载呢?例:
void f() {} int f() {}
如果使用如下调用:
f(); – 此时Java如何才能判断该调用哪一个f()呢?
2019/1/15
11
Java (SSXDU 2010)
默认构造器:“无参”构造器
如果你写的类中没有构造器,则编译器自动创建一个默 认构造器
2019/1/15
5
Java (SSXDU 2010)
方法重载(overloading)
void wash (Shirt s) { // … void wash (Car c) { // … void wash (Dog d) { // …
唯一的参数组合来区别重载的方法。
2019/1/15
和类名相同,首字母大写。
2019/1/15
3
Java (SSXDU 2010)
构造器
class Rock2 { Rock2(int i) { //构造器 System.out.println("Creating Rock number “ + i); } } public class SimpleConstructor2 { public static void main(String args[]) { for(int i = 0; i < 10; i++) new Rock2(i); } }
2019/1/15 7
Java (SSXDU 2010)
方法重载 – 普通方法
void info() { System.out.println("Tree is “ + height + " feet tall"); } void info(String s) { System.out.println(s + ": Tree is " + height + " feet tall"); } }
2019/1/15
4
Java (SSXDU 2010)
细微区别
从上下文中推断意思 “Wash the shirt” (洗衬衫) “Wash the car” (洗车) “Wash the dog” (洗狗) 而非 “shirtWash the shirt” (以洗衬衫的方式洗衬衫) “carWash the car” (以洗车的方式洗车) “dogWash the dog” (以洗狗的方式洗狗)
6
Java (SSXDU 2010)
方法重载 – 构造器
一个词,多个意思: overloaded
class Tree { int height; Tree() { System.out.println("Planting a seedling"); height = 0; } Tree(int i) { System.out.println( "Creating new Tree that is " + i + " feet tall"); height = i; }
2019/1/15 9
Java (SSXDU 2010)
涉及基本类型的重载
基本类型能从一个“较小”的类型自动提升至 一个“较大”的类型 如果传入的实际参数大于重载方法的形式参数, 就得通过转型来执行窄化转换。
– 如果不这样做,编译器就会报错。 – 窄化转换
2019/1/15
10
Java (SSXDU 2010)
– 理论上讲:释放GC所不能释放的内存
– 并不可靠:不保证一定会发生
必须实现“清理 ”
– 必须写特定的“清理”方法
2019/1/15
18
Java (SSXDU 2010)
终结条件
使用finalize()来发现对象中存在没有被适 当清理的部分。
class Book { boolean checkedOut = false; Book(boolean checkOut) { checkedOut = checkOut; } void checkIn() { checkedOut = false; } public void finalize() { if(checkedOut) System.out.println("Error: checked out"); //super.finalize(); //通常你也将做此调用 } finalize()只能存在于程序员 }
2019/1/15 13
Java (SSXDU 2010)
this:指定一个成员
如果在创建标识符的时候没有指定 可能不是好的实践,但是我自己这么用…
2019/1/15
ቤተ መጻሕፍቲ ባይዱ
14
Java (SSXDU 2010)
在构造器内部调用构造器
public class Flower { int petalCount = 0; String s = new String("null"); Flower(int petals) { petalCount = petals; System.out.println( "Constructor w/ int arg only, petalCount= " + petalCount); } Flower(String ss) { System.out.println( "Constructor w/ String arg only, s=" + ss); s = ss; }
2019/1/15
很难用到的一些晦涩用法里面。
19
Java (SSXDU 2010)
终结条件
public class TerminationCondition { public static void main(String[] args) { Book novel = new Book(true); // Proper cleanup: novel.checkIn(); // Drop the reference, forget to clean up: new Book(true); // Force garbage collection & finalization: System.gc();//强制进行终结动作 } }
初始化和清理
qyhuo@
Java (SSXDU 2010)
初始化和清理
Java用构造器确保正确的初始化, 使用垃圾收集器进行清理。
2019/1/15
2
Java (SSXDU 2010)
用构造器确保初始化
initialize() – 必须记住去调用。
}
2019/1/15
default constructor (no args) petalCount = 47 s = hi
16
Java (SSXDU 2010)
static的含义
没有this的方法 static方法内部不能调用非static的方法 可以在没有任何对象的前提下,仅仅通过类本 身来调用static方法
– static方法的主要目的
如果发现自己使用大量的static方法, 那么应当重新考虑你的设计策略。
2019/1/15
17
Java (SSXDU 2010)
清理:终结处理和垃圾回收
垃圾回收器的重要事实
– 垃圾回收不等于“析构” – 你的对象可能不被垃圾回收 – 垃圾回收只与内存有关
finalize()的用途何在?
2019/1/15
如果已经定义一个构造器(不管 是否有参数),编译器就不会帮 你自动创建默认构造器。
12
Java (SSXDU 2010)
this关键字:引用当前对象
public class Leaf { int i = 0; Leaf increment() { i++; return this; } void print() { System.out.println("i = " + i); } public static void main(String[] args) { Leaf x = new Leaf(); x.increment().increment().increment() .print(); } }
本例的终结条件是:所有的Book对象在被当作垃 圾回收前都应该签入。但是在main()方法中由 于程序员失误,一本书未签入。 输出:
2019/1/15
Error: checked out 20
Java (SSXDU 2010)
成员初始化
void f() { int i; i++; // Error -- i not initialized }
产生编译错误 对于类的数据成员是基本类型,如果不指定的 值的话,就赋予默认值。
– 类的每个基本类型数据成员都会有一个初始值。
class Data { int i = 999; long l; // Defaults to zero // ... }
2019/1/15
21
Java (SSXDU 2010)
指定初始化
在定义类成员变量的地方为其赋值
class Measurement { Depth d = new Depth(); boolean b = true; // . . .
class InitialValues { boolean b = true; char c = 'x'; byte B = 47; short s = 0xff; int i = 999; long l = 1; float f = 3.14f; double d = 3.14159; //. . .
2019/1/15
15
Java (SSXDU 2010)
Flower(String s, int petals) { this(petals); //! this(s); //不可以调用两个! this.s = s; //“this”的另一种使用 System.out.println("String & int args"); } Flower() { this("hi", 47); System.out.println("default constructor (no args)"); } void print() { //! this(11); //不能在非构造器方法中! 编译错误 System.out.println( "petalCount = " + petalCount + " s = "+ s); } public static void main(String[] args) { Flower x = new Flower(); x.print(); Constructor w/ int arg only, petalCount= 47 } String & int args
构造器
class Rock { Rock() { //构造器 System.out.println("Creating Rock"); } } public class SimpleConstructor { public static void main(String args[]) { for(int i = 0; i < 10; i++) new Rock(); } }
– 无形式参数—作用是传建一个默认对象 class Bird { int i; } public class DefaultConstructor { public static void main(String[] args) { Bird nc = new Bird(); //默认!
} } ///:~
2019/1/15
8
Java (SSXDU 2010)
方法重载 – 续
public class Overloading { public static void main(String[] args) { for(int i = 0; i < 5; i++) { Tree t = new Tree(i = pRand(10)); (); ("overloaded method"); } // Overloaded constructor: new Tree(); } }
如果没有为d指定初始值就 尝试使用它,就会出现运行 时错误,告诉你产生了一个 异常。