下载此文档

j04 初始化与清除.ppt


文档分类:汽车/机械/制造 | 页数:约33页 举报非法文档有奖
1/33
下载提示
  • 1.该资料是网友上传的,本站提供全文预览,预览什么样,下载就什么样。
  • 2.下载该文档所得收入归上传者、原创者。
  • 3.下载的文档,不会出现我们的网址水印。
1/33 下载此文档
文档列表 文档介绍
初始化和清理
******@.
2018/8/10
2
初始化和清理
Java用构造器确保正确的初始化,使用垃圾收集器进行清理。
2018/8/10
3
用构造器确保初始化
initialize() –必须记住去调用。
构造器
class Rock {
Rock() { //构造器
("Creating Rock");
}
}
public class SimpleConstructor {
public static void main(String args[]) {
for(int i = 0; i < 10; i++)
new Rock();
}
}
和类名相同,首字母大写。
2018/8/10
4
构造器
class Rock2 {
Rock2(int i) { //构造器
("Creating Rock number “+ i);
}
}
public class SimpleConstructor2 {
public static void main(String args[]) {
for(int i = 0; i < 10; i++)
new Rock2(i);
}
}
2018/8/10
5
细微区别
从上下文中推断意思
“Wash the shirt”(洗衬衫)
“Wash the car”(洗车)
“Wash the dog”(洗狗)
而非
“shirtWash the shirt”(以洗衬衫的方式洗衬衫)
“carWash the car”(以洗车的方式洗车)
“dogWash the dog”(以洗狗的方式洗狗)
2018/8/10
6
方法重载(overloading)
void wash (Shirt s) { // …
void wash (Car c) { // …
void wash (Dog d) { // …
唯一的参数组合来区别重载的方法。
2018/8/10
7
class Tree {
int height;
Tree() {
("Planting a seedling");
height = 0;
}
Tree(int i) {
(
"Creating new Tree that is "
+ i + " feet tall");
height = i;
}
方法重载–构造器
一个词,多个意思: overloaded
2018/8/10
8
方法重载–普通方法
void info() {
("Tree is “
+ height + " feet tall");
}
void info(String s) {
(s + ": Tree is "
+ height + " feet tall");
}
}
2018/8/10
9
方法重载–续
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();
}
}
2018/8/10
10
涉及基本类型的重载
基本类型能从一个“较小”的类型自动提升至一个“较大”的类型
如果传入的实际参数大于重载方法的形式参数,就得通过转型来执行窄化转换。
如果不这样做,编译器就会报错。
窄化转换

j04 初始化与清除 来自淘豆网www.taodocs.com转载请标明出处.

相关文档 更多>>
非法内容举报中心
文档信息
  • 页数33
  • 收藏数0 收藏
  • 顶次数0
  • 上传人373116296
  • 文件大小835 KB
  • 时间2018-08-10