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 another String, it says, "I‘ll turn source into a String by calling toString()!" After doing this it can combine the two Strings and pass the resulting String to System.out.println(). Anytime you want to allow this behavior with a class you create, you need only write a toString method.

2. Four ways to initialize the references

①. at hte point the objects are defined. This means that they will always be initialized before the constructor is called.

②. in the constructor for that class.

③. right before you actually need to use the object. This is often called lazy initialization.

④. using instance initialization.

class Soap {

  private String s;

  Soap() {

    print("Soap()");

    s = "Constructed";

  }

  pulbic String toString() { return s; }

}

public class Bath {

  private String //Initializing at the point of definition:

    s1 = "Happy",

    s2 = "Happy",

    s3, s4;

  private Soap castille;

  private int i;

  private float toy;

  public Bath() {

    print("Inside Bath()");

    s3 = "Joy";

    toy = 3.14f;

    castille = new Soap();

  }

  // Instance initialization:

  { i = 47; }

  public String toString() {

    if(s4 = null) // Lazy initialization:

      s4 = "Joy";

    return

      "s1 = " + s1 + "\n" +

      "s2 = " + s2 + "\n" +

      "s3 = " + s3 + "\n" +

      "s4 = " + s4 + "\n" +

      "i = " + i + "\n" +

      "toy = " + toy + "\n" +

      "castille = " + castille;     

  }

  public static void main(String[] args) {

    Bath b = new Bath();

    print(b);

  }

}/* output:

Inside Bath();

Soap()

s1 = Happy

s2 = Happy

s3 = Joy

s4 = Joy

i = 47

toy = 3.14

castille = Constructed 

*/

3. Inheritance syntax

unless you explicitly inherit from some other class, you implictly inherit from Java‘s standard root class Object

key word: extends

as a general rule make all fields private and all methods public

it‘s just from the outside, the subobject of the base class is wrapped within the derived-class object. it‘s essential that base-class subobject be initializied correctly and there is only one way to guarantee this: perform the initialization in the constructor by calling the base-class constructor. Java automatically inserts calls to the base-class to the base-class constructor in the derived-class constructor.

class Art {

  Art() { print("Art constructor"); }

}

classs Drawing extends Art {

  Drawing() { print("Drawing constructor"); }

}

public class Cartoon extends Drawing {

  public Cartoon() { print("Cartoon constructor"); }

  public static void main(String[] args) {

    Cartoon x = new Cartoon();

  }

}/*

Art constructor

Drawing constructor

Cartoon constructor

*/

that the construction happens from the base "outward", so the base class is initialized before the drived-class construction can access it.

it there‘s no default base-class constructor, or if you want to call a base-class constructor that has arguments, you must explicitly write a call to the base-class constructor using the super key word and the appropriate argument list, In addition, the call to the base-class constructor must be the first thing you do in the derived-class constructor

时间: 2024-07-30 10:19:02

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

如何在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

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

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 meth

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集合,但是有时候会