The trick is to use the classes without soiling the existing code.

  1. composition--simply create objects of your existing class inside the new class. simply reusing the functionality of the code, not its form

  2.inheritance--creates a new class as a type of an existing class. Take the form of the existing class and add code to it without modifying the existing class.

    Inheritance is one of the cornerstones of object-oriented programming, and has additional implications that will be explored in the Polymorphism chapter.

Composition syntax

  references initialized: 

    1. At the point the objects are defined

    2. In the constructor for that class

    3. Right before you actually need to use the object (lazy initialization)

    4. Using instance initialization

  如:

    public class Bath  {

      private String s1 = "Happy"; //Initialization at the point of definition

      private String s2,s3,s4;

      public Bath() {

        s2 = "Happy"; // In the constructor

      }

      { s3 = "Happy"; } // Instance Initialization

      public String toString() {

        // Lazy initialization

        if( s4 == null) {

          s4 = "Happy";

        }

        return s4;

      }

    }

Inheritance syntax

  super.methodName() 调用父类的方法

  Initializing the base class

  When you create an object of the derived class, it contains within it a subobject of the base class. This subobject is the same as if you had created an object of the base class by itself. It's just that from the outside, the subobject of the base class is wrapped within the derived-class object.

  Java automatically inserts calls to the base-class constructor in the derived-class constructor.

  Constructors with arguments

  The call to the base-class constructor with arguments must explicit, and be the first thing you do in the derived-class constructor, etc: super(1)

Delegation

  A third relationship, which is not directly supported by Java, is called delegation. This is midway between inheritance and composition.

  You place a member object in the class you're building (like composition), but at the same time you expost all the methods from the member object in your new class (like inheritance).

Combining composition and inheritance

  Guaranteeing proper cleanup

  If you want something cleaned up for a class, you must explicitly write a special method to do it, and make sure that the client programmer knows that they must call this method.

  On top of this--as described in the Erro Handling with Exceptions chapter-- you must guard against as exception by putting such cleanup in a finally clause.

  Note that in your cleanup method, you must also pay attention to the calling order for the base-class and member-object cleanup methods in case one subobject depends on another. In general, First perform all the cleanup work specific to your class, in the reverse order or creationg. Then call the base-class cleanup method.

  用户可以自己调用对象的finalize方法,但是这种调用是正常的方法调用,和对象的销毁过程无关

  Name hiding

  Overloading works regardless of whether the method was defined in the base-class or the derived-class

  overload 重载

  override  重新、覆盖

  Java SE5 has added the @Override annotation. Whne you mean to override a method, you can choose to add this annotation and the compiler will produce an error message if you accidentally overload instead of overriding.

Choosing composition vs. inheritance

  Composition is generally used when you want the features of an existing class inside your new class, but not its interface. That is, you embed an object so that you can use it to implement features in your new class, but the user of your new class sees the interface you've defined for the new class rathg than the interface from the embedded object. For this effect, you embed private objects of existing classes inside your new class.

  Sometimes it makes sense to allow the class user to directly access the composition of your new class; that is,to make the member objects public. When the user know you're assembling a bunch of parts, it makes the interface easier to understand

  例子: 汽车,包含 门(行为:open)、发动机(行为:start)

     汽车.门.open  (public 门)

     汽车.start  (private 发动机)

  Note keep in mind that in general you should make fields private.

  When you inherit, you take an existing class and make a special version of it. In general, this means that you're taking a general-purpose class and specializing it for a particular need.

  Composition: has-a relationship

  Inheritance: is-a relationship

  Interface: like-a relationship

protected

  This is private as far as the class user is concerned, but available to anyone who inherits from the class or anyone else in the same package.

Upcasting

  The most important aspect of inheritance is not that it provides methods for the new class. It's the relationship expressed between the new class and the base class.

  Upcasting is always safe because you're going from a more specific type to a more general type.

  You can also perform the reverse of upcasting, called downcasting, but this involves a dilemma that will be examined further in the next chapter, and in the Type Information chapter.

  Composition vs. inheritance revisited

  In object-oriented programming, the most likely way that you'll create and use code is by simply packaging data and methods together inti a class, and using object of that class. You'll also use existing class to build new classes with composition. Less frequently, you'll use inheritance.

  One of the clearest ways to determine whether you should use composition or inheritance is to ask whether you'll ever need to upcast from your new class to the base class.If you don't need to upcast, then you should look closely at whether you need inheritance.

The final keyword

  final can be used: for data, methods, and classes.

  final data

  to tell the compiler that a piece of data is constant. A constant is useful for two reasons:

    1. It can be a compile-time constant that won't ever change.

    2. It can be a value initialized at run time that you don't want changed.

  In the case of a compile-time constant, the compiler is allowed to "fold" the constant value into any calculations in which it's used; that is, the calculation can be performed at compile time, eliminating some run-time overhead. In java, these sorts of constants must be primitives and are expressed with the final keyword. A value must be given at the time of definition of such a constant.

  A field that is both static and final has only one piece of storage that cannot be changed.

  When final is used with object references rather than primitives, makes the reference a constant. Once the reference is initialized to an object, it can never be changed to point to another object. Howerver, the object itself can be modified.

  private static final int i = 5; //compile-time

  private final int i = 5; //comile-time

  private static final int i = new Random().nextInt(20); //run-time

  private final int i = new Random().nextInt(20); //run-time

  Making references final seems less useful than making primitives final.

  Blank finals

  public class BlankFinal {

    private final int i = 0; // Initialized final

    private final int j; // Blank final

    public BlankFinal(int jj) {

      j = jj; // Initialize blank final

    }

  }

  You're forced to perform assignments to finals either with an expression at the point of definition of the field or in every constructor.

  final arguments

  Java allows you to make arguments final by declaring them as such in the argument list. This means that inside the method you cannot change what the argument reference points to.

  This feature is primarily used to pass data to anonymous inner classes

  final methods

  There are two reasons for final methods.

    1. to put a "lock" on the method to prevent any inheriting class from changing its meaning.

    2. efficiency.(old, Java SE5之后由编译器和JVM来考虑)

  final and private

  Any private methods in a class are implicitly final. Because you can't access a private method, you can't override it.

  "Overriding" can only occur if something is part of the base-class interface. (即父类和子类中的private方法即时签名一样,也互不影响) If a method is private, it isn't part of the base-class interface.

  final classes

  When you say that an entire class is final, you state that you don't want to inherit from this class or allow anyone else to do so.

  Note that the fields of a final class can be final or not. The same rules apply to final for fields regardless or whether the class is defined as final. However, because it prevents inheritance, all methods in a final class are implicitly final, since there's no way to override them.

  final caution

Initialization and class loading

  In general, you can say that "class code is loaded at the point of first use." This is usually when the first object of that class is constructed, but loading also occurs when a static field or static method is accessed.

  The point of first use is also where the static initialization takes place. All the static objects and the static code block will be initialized in textual order at the point of loading. The statics are initialized only once.

  Initialization with inheritance

Summary

  Both inheritance and composition allow you to create a new type from existing types. Composition reuses existing types as part of the underlying implementation of the new type, and inheritance reuses the interface.

  With Inheritance, the derived class has the base-class interface, so it can be upcast to the base which is critical for polymorphism.

  When you set out to design a system, it's important to realize that program development is an incremental process, just like human learning. It relies on experimentation; you can do as much analysis as you want, but you still won't know all the answers when you set out on a project. You'll have much more success-and more immediate feedback-if you start out to "grow" your project as an organic, evolutionary creature, rather than constructing it all at once like a glass-box skyscraper.

  

  

Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(八)之Reusing Classes的更多相关文章

  1. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十)之Inner Classes

    The inner class is a valuable feature because it allows you to group classes that logically belong t ...

  2. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(八)之Polymorphism

    Polymorphism is the third essential feature of an object-oriented programming language,after data ab ...

  3. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(七)之Access Control

    Access control ( or implementation hiding) is about "not getting it right the first time." ...

  4. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(六)之Initialization & Cleanup

    Two of these safety issues are initialization and cleanup. initialization -> bug cleanup -> ru ...

  5. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十三)之Strings

    Immutable Strings Objects of the String class are immutable. If you examine the JDK documentation fo ...

  6. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(二)之Introduction to Objects

    The genesis of the computer revolution was a machine. The genesis of out programming languages thus ...

  7. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十四)之Type Information

    Runtime type information (RTTI) allow you to discover and use type information while a program is ru ...

  8. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十二)之Error Handling with Exceptions

    The ideal time to catch an error is at compile time, before you even try to run the program. However ...

  9. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十一)之Holding Your Objects

    To solve the general programming problem, you need to create any number of objects, anytime, anywher ...

随机推荐

  1. iOS 原生库(AVFoundation)实现二维码扫描,封装的工具类,不依赖第三方库,可高度自定义扫描动画及界面(Swift 4.0)

    Create QRScanner.swift file // // QRScanner.swift // NativeQR // // Created by Harvey on 2017/10/24. ...

  2. django自动生成接口文档

    我们在实际项目中,会需要将我们的一些接口的信息返回给前端,便于前后端的交互,在实际使用中,这种自动生成接口文档的模块很多,我主要是用REST framework自动生成接口文档,这个需要用到的是cor ...

  3. 第十六周Java实验作业

    实验十六  线程技术 实验时间 2017-12-8 1.实验目的与要求 (1) 掌握线程概念: 多线程是进程执行过程中产生的多条执行线索,线程是比进程执行更小的单位. 线程不能独立存在,必须存在于进程 ...

  4. demo08-js条件运算符

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. shell编程之脚本参数$@,$*,$#,$$,$?的含义

    #首先按顺序解释各个参数的含义 1.$0  表示脚本的文件名, 具体的路径信息和执行命令时的相对位置有关,例如 sakura@mi-OptiPlex-7050:~/sh$ sh args.sh arg ...

  6. coding++ :JS对日期的神操作封装版

    格式化日期: /** * 格式化日期 * @param fmt 例如:yyyy-MM-dd 等 * @returns {*} * @constructor */ Date.prototype.Form ...

  7. 【Ubuntu】常用命令汇总,整理ing

    Ubuntu 常用命令(在此页面中Ctrl+F即可快速查找) 在Ubuntu系统使用过程中,会不断地接触到命令行操作,下面对一些常用的命令进行汇总,方便查找. 1.文件操作 1.1 文件复制拷贝 cp ...

  8. 详解Springboot中自定义SpringMVC配置

    详解Springboot中自定义SpringMVC配置 WebMvcConfigurer接口 ​ 这个接口可以自定义拦截器,例如跨域设置.类型转化器等等.可以说此接口为开发者提前想到了很多拦截层面的需 ...

  9. U - Inviting Friends HDU - 3244( LIS 最长升序子序列——变形 )

    序列变换 Problem Description 我们有一个数列A1,A2-An,你现在要求修改数量最少的元素,使得这个数列严格递增.其中无论是修改前还是修改后,每个元素都必须是整数. 请输出最少需要 ...

  10. Java并发基础09. 多个线程间共享数据问题

    先看一个多线程间共享数据的问题: 设计四个线程,其中两个线程每次对data增加1,另外两个线程每次对data减少1. 从问题来看,很明显涉及到了线程间通数据的共享,四个线程共享一个 data,共同操作 ...