`
hanlsheng
  • 浏览: 24767 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论

Initialization & Cleanup

    博客分类:
  • java
阅读更多

Initialization & Cleanup

1.       In java, the class designer can guarantee initialization of every object by providing a constructor. If a class has a constructor, Java automatically calls that constructor when an object is created, before users can even get their hands on it.

2.       The name of the constructor is the same as the name of the class. It make sense that such a method will be called automatically during initialization.

3.       The name of the constructor must match the name of the class exactly.

4.       Constructor arguments provide you with a way to provide parameters for the initialization of an object.

5.       the constructor is an unusual type of method because it has no return value. This is distinctyly different from a void return value, in which the method returns nothing but you still have the option to mike it return something else.

Method overloading

1.       One of the important features in any programming language is the use of names. When you create an object , you give a name to a region of storage. A method is a name for an action..

2.       Method overloading is essential to allow the same method name to be used with different argument types.

Distinguishing overloaded methods

1.       Each overloaded method must take a unique list of argument types.

2.       Even differences in the ordering of arguments are sufficient to distinguish two methods/

3.       If your argument is wider, then you must perform a narrowing conversion with a cast. If you don’t do this, the compiler will issue an error message.

Default constructors

1.       If you create a class that has no constructors , the compiler will automatically create a default constructor for you.

The this keyword

1.       Suppose you’re inside a method and you’d like to get the reference to the current object. Since that reference is passed secretly by the compiler, there’s no identifier for it. However, for this purpose there’s a keyword: this. The this keyword- which can be used only inside a non-static method – produces the reference to the object that the method has been called for.

2.       It’s often used in return statements when you want to return the reference to the current object;

3.       The this keyword is also useful for passing the current object to another method..

Calling constructors from constructors

1.       Normally, when you say this, it is in the sense of “this object” or “ the current object” and by itself it produces the reference to the current object . In a constructor, the this keyword takes on a different meaning when you give it an argument list.

The meaning of static

1.       With the this keyword in mind, you can more fully understand what it means to make a method static. It means that there is no this for that particular method. You cannot call non-static methods from inside static methods. In fact, that’s primarily what a static method is for. It’s as if you’re creating the quivalent of a global method. However, global methods are not permitted in java, and putting the static method inside a class allows it access to other static methods and to static fields.

Cleanup: finalization and garbage collection

1.       Programmers know about the importance of initialization, but often forget the importance of cleanup.

2.       When the garbage collector is ready to release the storage used for your object, it will first call finalize(), and only on the next garbage-collection pass will it reclaim the object’s memory. So if you choose to use finalize(), it gives you the ability to perform some important cleanup at the time of garbage collection.

 

分享到:
评论

相关推荐

    Think in C++ 英文版(含卷一、卷二)

    6: Initialization & Cleanup 7: Function Overloading & Default Arguments 8: Constants 9: Inline Functions 10: Name Control 11: References & the Copy-Constructor 12: Operator Overloading 13: Dynamic ...

    TIJ4 Initialization Cleanup

    TIJ4 Initialization Cleanup

    Thinking_In_C#英文正式版

    第5章“Initialization & Cleanup”(初始化与清理)涉及对象的构造函数和析构函数,以及资源清理的重要性。第6章“Coupling and Cohesion”(耦合与内聚)则探讨了设计模块化程序的指导原则,这是编写可维护代码的...

    Think in C# .Net

    5. **第 5 章:Initialization & Cleanup** —— 阐述了对象初始化和清理的相关知识,包括构造函数、析构函数等。 6. **第 6 章:Coupling and Cohesion** —— 分析了模块之间的耦合性和内聚性,解释了良好的软件...

    think in C sharp

    初始化与清理 (Initialization & Cleanup) - **第5章:初始化与清理**(151页)讨论了对象生命周期管理,包括构造函数、析构函数等。 #### 6. 耦合与内聚 (Coupling and Cohesion) - **第6章:耦合与内聚**(215页...

    Thinking in CSharp

    - **第五章:初始化与清理**(Initialization & Cleanup):本章着重于C#中的对象生命周期管理,包括如何初始化对象以及在对象不再需要时如何进行适当的清理工作,这对于避免内存泄漏等问题至关重要。 - **第六章:...

    thinking in Java guide solutio

    Initialization & Cleanup (初始化与清理) - **初始化过程**:对象创建后如何进行初始化。 - **析构函数**:虽然Java没有析构函数,但可以通过finalizer实现类似的功能。 - **静态初始化块与非静态初始化块**:分别...

    Thinking in C++ VolumeI.pdf

    初始化与清理 (Initialization & Cleanup) 本章详细介绍了对象的初始化和清理过程: - **构造函数与析构函数**:解释了构造函数和析构函数的作用及使用方法。 - **内存管理**:讨论了内存分配和释放的重要性,以及...

    ucln_cmn.rar_UP

    它可能会包含函数声明,如`void ucln_cmn_init()`(初始化清理机制)和`void ucln_cmn_cleanup()`(执行清理操作)。此外,可能还会有枚举类型定义来标识不同类型的清理事件,或者是宏定义以控制调试信息的开关。 2...

    Defer:C的Defer陈述式

    int x __attribute__((cleanup(cleanup))) = 42; // ... return 0; } ``` 虽然这些方法可以实现类似`defer`的功能,但它们并没有内置的异常处理支持。在C++中,可以使用RAII(Resource Acquisition Is ...

    nls_cp863.rar_translation

    5. **Initialization and Cleanup**:初始化和清理函数,用于加载和卸载转换模块。 6. **Testing and Debugging**:可能包含一些测试用例和调试辅助函数,确保转换的准确性和兼容性。 在实际应用中,这样的转换库...

    C语言函数大全.CHM

    C语言函数大全,CHM,中文版。 C语言各种函数具体描述,有例证. 函数名: bar 功 能: 画一个二维条形图 用 法: void far bar(int left, int top, int right, int .../* clean up */ closegraph(); return 0; }

    python code patterns

    ### Initialization and Cleanup (初始化与清理) #### Initialization (初始化) - **构造函数**:使用`__init__`方法完成对象创建时的初始化工作。 - **属性设置**:在构造函数中为对象设置初始状态。 - **其他初始...

Global site tag (gtag.js) - Google Analytics