some fragment of thinking in java part5

1. Delegation

a relationship that is not directly supported by Java. This is a midway between inheritance and composition, because you place a number object in the class you are building(like composition), but at the same time you expose all the methods from the member object in your new class(like inheritance)

public class SpaceShipDelegation {

  private String name;

  private SpaceShipControls controls = new SpaceShipControls();

  public SpaceShipDelegation(String name){

    this.name = name;

  }

  // Delegated methods:

  public void back(int velocity) {

    controls.back(velocity);

  }

  public void dowm(int velocity) {

    controls.down(velocity);

  }

  public void forward(int velocity) {

    controls.forward(int velocitly);

  }

  ......

  public static void main(String[] args) {

    SpaceShipDelegation protector = new SpaceShipDelegation("NSEA Protector" );

    protector.forward(100);

  }

}

2.  Guaranteening proper cleanup

you must also pay attention to the calling order for the base-class and number object cleanup methods in case one subobject depends on another. In general, you should follow the same form that is imposed by a C++ complier on its destructors: reverse the order of creation.

3. Name hiding

if a Java base class has method name that is overloaded several times, redefineing that method name in the derived-class would not hide any of the base-class versions(unlike C++), Thus overloading works regardless of whether the method was defined at this level or in the base class

class Homer {

  char doh(char c) {

    print("doh(char)");

    return ‘d‘;

  }

  float doh(float f) {

    print("doh(float)");

    return 1.0f;

  }

}

class Milhouse {}

class  Bart extends Homer {

  void doh(Milhouse m) {  // overloading between in inheritance ? I‘m not sure

    print("doh(Milhouse)");

  }

}

public class Hide {

  public static void main(String[] args) {

    Bart b = new Bart();

    b.doh(1);

    b.doh(‘x‘);

    b.doh(1.0f);

    b.doh(new Milhouse());

  }

}/*Output:

doh(float)

doh(char)

doh(float)

doh(Milhouse)

*/

3. Chossing composition vs inheritance

Composition is  generally used when you want the functionality of an existing class inside your new class, but not its interface. That is, you embed an object so that you can 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 rather than the interface from the embedded  object. For this effect, you embed private objects of existing classes inside your new class.

the is-a relationship is expressed with inheritance, and the has-a relationship is expressed with composition.

4. Protected

The protected keyword is a nod to pragmatism. available to anyone who inherits from this class or anyone else in the same package(protected also provides package access)

时间: 2024-08-26 20:30:33

some fragment of thinking in java part5的相关文章

如何在Liferay Custom JSP Fragment项目中加Java代码

先附上大神原文链接 Adding Dependencies to JSP Fragment Bundles 在开发Liferay的过程中,我们常常会利用Module Fragment来修改Liferay原有的JSP页面,在修改原有页面的同时,我们还希望加上自己的逻辑,同时加上一些Java代码,这些要如何做到呢? 先占个坑,以后填,附上代码地址https://github.com/JoyeLuo/jsp-fragment

some fragment of thinking in java part4

1. Every non-primitive methods has a toString() method "source = " +source; //an object of WaterSource the compiler sees you tring to add a String object("source =") to a WaterSource . Because you can only add a "String" to a

some fragment of thinking in java

1.String s; System.out.println("s="); you will get a compile time error because s isn't actually attached to anything, A safer practice, then is always initialize a reference when you creacte it: String s = new "asdf"; 2. When you crea

Android实习札记(5)---Fragment之底部导航栏的实现

Android实习札记(5)---Fragment之底部导航栏的实现 --转载请注明出处:coder-pig 在Part 4我们回顾了一下Fragment的基本概念,在本节中我们就来学习Fragment应用的简单例子吧! 就是使用Fragment来实现简单的底部导航栏,先贴下效果图: 看上去很简单,实现起来也是很简单的哈!那么接着下来就看下实现的流程图吧: 实现流程图: 看完流程图是不是有大概的思路了,那么接着就开始代码的编写吧: 代码实现: ①先写布局,布局的话很简单,一个FrameLayou

Android ViewPager和Fragment实现顶部导航界面滑动效果

在项目中,我们常常需要实现界面滑动切换的效果.例如,微信界面的左右滑动切换效果.那这种效果是怎么实现的?今天我就带大家简单了解ViewPager,并通过实例来实现该效果. 一. ViewPager 官方API 首先我们来看一下ViewPager官方给出的解释,如图: 具体意思如下: Layout 管理器允许用户可以在页面上,左右滑动来翻动页面.你可以考虑实现PagerAdapter接口来显示该效果. ViewPager很多时候会结合Fragment一块使用,这种方法使得管理每个页面的生命周期变得

Android - Fragment(二)加载Fragment

Fragment加载方法 加载方法有两种,在xml文件中注册,或者是在Java代码中加载. xml中注册 例如在fragment_demo.xml中定义 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width=

android之Fragment(官网资料翻译)

Fragment要点 Fragment作为Activity界面的一部分组成出现 可以在一个Activity中同时出现多个Fragment,并且,一个Fragment亦可在多个Activity中使用. 在Activity运行过程中,可以添加.移除或者替换Fragment(add().remove().replace()) Fragment可以响应自己的输入事件,并且有自己的生命周期,当然,它们的生命周期直接被其所属的宿主activity的生命周期影响. 设计哲学 Android在3.0中引入了fr

android项目剖解之ViewPager+Fragment 实现tabhost效果

项目中需要用到底栏导航栏,滑动或者点击会切换上面的视图,如图: 这个效果使用Viewpager+Fragmen实现是主流方案,加入你之前对fragment不太了解,可以先看android之Fragment(官网资料翻译) 整个文件如下: 好了废话少说,先上布局文件:main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://sche

Android基础_学习Fragment时候出现的一些错误[博客园]

2015年6月7日09:47:12 早晨起来把昨天的NewsFragment小小的Project错误找到了 空指针异常 原因:因为之前也遇到这种空指针异常的错误,多数原因都是因为创建了对象或者数组但是没有实例化,只定义了一个对象引用或者是数组引用,然后就去调用对象的方法或者是给数组的元素传递数据了: 这里是因为: 很多控件定义为了全局变量,但是却没有在方法中进行初始化-参见<Java编程思想-实例化的章节> 在给ListView控件绑定对应的Adapter时候,会定义List集合,但是有时候会