11构造方法和垃圾回收_第1页
11构造方法和垃圾回收_第2页
11构造方法和垃圾回收_第3页
11构造方法和垃圾回收_第4页
11构造方法和垃圾回收_第5页
已阅读5页,还剩39页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、12 Constructors And Garbage Collection 2021-7-181Dong Shao, Nanjing Unviersity Stack and heap When a JVM starts up, it gets a chunk of memory from the underlying OS, and uses it to run your Java program. 2021-7-182Dong Shao, Nanjing Unviersity 2021-7-18Dong Shao, Nanjing Unviersity3 2021-7-18Dong Sh

2、ao, Nanjing Unviersity4 2021-7-18Dong Shao, Nanjing Unviersity5 2021-7-18Dong Shao, Nanjing Unviersity6 Instance variables 2021-7-18Dong Shao, Nanjing Unviersity7 3 steps of object declaration, creation and assignmengt 2021-7-18Dong Shao, Nanjing Unviersity8 2021-7-18Dong Shao, Nanjing Unviersity9 C

3、onstructor The key feature of a constructor is that it runs before the object can be assigned to a reference. The constructor gives you a chance to step into the middle of new. 2021-7-18Dong Shao, Nanjing Unviersity10 No return type for Constructor Initializing the state of a class 2021-7-18Dong Sha

4、o, Nanjing Unviersity11 2021-7-18Dong Shao, Nanjing Unviersity12 2021-7-18Dong Shao, Nanjing Unviersity13 2021-7-18Dong Shao, Nanjing Unviersity14 2021-7-18Dong Shao, Nanjing Unviersity15 2021-7-18Dong Shao, Nanjing Unviersity16 2021-7-18Dong Shao, Nanjing Unviersity17 Inheritance and constructors 2

5、021-7-18Dong Shao, Nanjing Unviersity18 2021-7-18Dong Shao, Nanjing Unviersity19 2021-7-18Dong Shao, Nanjing Unviersity20 How to invoke a superclass constructor? 2021-7-18Dong Shao, Nanjing Unviersity21 2021-7-18Dong Shao, Nanjing Unviersity22 The superclass parts of an objects have to be fully-form

6、ed (completely build) before the subclass parts can be constructed. The call to super() must be the first statement in each constructor. 2021-7-18Dong Shao, Nanjing Unviersity23 Superclass constructor with arguments 2021-7-18Dong Shao, Nanjing Unviersity24 Invoking one overloaded constructor from an

7、other 2021-7-18Dong Shao, Nanjing Unviersity25 2021-7-18Dong Shao, Nanjing Unviersity26 How long does an object live? 2021-7-18Dong Shao, Nanjing Unviersity27 2021-7-18Dong Shao, Nanjing Unviersity28 2021-7-18Dong Shao, Nanjing Unviersity29 2021-7-18Dong Shao, Nanjing Unviersity30 2021-7-18Dong Shao

8、, Nanjing Unviersity31 2021-7-18Dong Shao, Nanjing Unviersity32 2021-7-18Dong Shao, Nanjing Unviersity33 Initialization with inheritance Access Main(), load base class, Load until the root base class Static Initialization in the root base class then the next derived class, and so on All the primitiv

9、es and the object reference are set to null The base-class constructor will be called The instance variables are initialized in textual order The rest of the body of the constructor is executed 2021-7-18Qin LIU, Nanjing University34 class Characteristic private String s; Characteristic(String s) thi

10、s.s = s; System.out.println(Creating Characteristic + s); protected void dispose() System.out.println(finalizing Characteristic + s); class Description private String s; Description(String s) this.s = s; System.out.println(Creating Description + s); protected void dispose() System.out.println(finali

11、zing Description + s); 2021-7-18Qin LIU, Nanjing University35 class LivingCreature private Characteristic p = new Characteristic(is alive); private Description t = new Description(Basic Living Creature); LivingCreature() System.out.println(LivingCreature(); protected void dispose() System.out.printl

12、n(LivingCreature dispose); t.dispose(); p.dispose(); 2021-7-18Qin LIU, Nanjing University36 class Animal extends LivingCreature private Characteristic p= new Characteristic(has heart); private Description t = new Description(Animal not Vegetable); Animal() System.out.println(Animal(); protected void

13、 dispose() System.out.println(Animal dispose); t.dispose(); p.dispose(); super.dispose(); 2021-7-18Qin LIU, Nanjing University37 class Amphibian extends Animal private Characteristic p = new Characteristic(can live in water); private Description t = new Description(Both water and land); Amphibian()

14、System.out.println(Amphibian(); protected void dispose() System.out.println(Amphibian dispose); t.dispose(); p.dispose(); super.dispose(); 2021-7-18Qin LIU, Nanjing University38 public class Frog extends Amphibian private static Test monitor = new Test(); private Characteristic p = new Characteristi

15、c(Croaks); private Description t = new Description(Eats Bugs); public Frog() System.out.println(Frog(); protected void dispose() System.out.println(Frog dispose); t.dispose(); p.dispose(); super.dispose(); public static void main(String args) Frog frog = new Frog(); System.out.println(Bye!); frog.di

16、spose(); 2021-7-18Qin LIU, Nanjing University39 monitor.expect(new String Creating Characteristic is alive, Creating Description Basic Living Creature, LivingCreature(), Creating Characteristic has heart, Creating Description Animal not Vegetable, Animal(), Creating Characteristic can live in water,

17、 Creating Description Both water and land, Amphibian(), Creating Characteristic Croaks, Creating Description Eats Bugs, Frog(), Bye!, Frog dispose, finalizing Description Eats Bugs, finalizing Characteristic Croaks, Amphibian dispose, finalizing Description Both water and land, finalizing Characteri

18、stic can live in water, Animal dispose, finalizing Description Animal not Vegetable, finalizing Characteristic has heart, LivingCreature dispose, finalizing Description Basic Living Creature, finalizing Characteristic is alive ); 2021-7-18Qin LIU, Nanjing University40 abstract class Glyph abstract v

19、oid draw(); Glyph() System.out.println(Glyph() before draw(); draw(); System.out.println(Glyph() after draw(); class RoundGlyph extends Glyph private int radius = 1; RoundGlyph(int r) radius = r; System.out.println( RoundGlyph.RoundGlyph(), radius = + radius); void draw() System.out.println( RoundGlyph.draw(), radius = + radius); 2021-7-18Qin LIU, Nanjing Univers

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论