第1章--使用对象

1.1 字符类型

char c = 65; // char --> int

char c = '\u0041'; // \u: unicode + (Hex 41--> Dec 65)

char c = 'A';

escape character

'\b' --> backspace the cursor (not delete the last character, just move the cursor)

However, eclipse will ignore it

e.g.System.out.print("abc\bd");

in eclipse console: abcd

in terminal console: abd

'\n' --> 换行; '\r\ --> 回车  --> 起源于打字机:回车-->光标回到第一列;回车-->新的一行

1.2 包裹类型

基础类型 --> 包裹类型

boolean Boolean

char Character  --> .isDigit(char); isUppercase(char); .toLowerCase(char); etc.

int/double Integer/Double  --> Integer.MAX_VALUE

usage:

Integer i = 10;

Integer i = new Integer(10);

1.3 字符串类型

read in String:

in.next() --> separated by space (including \t, \n, etc.)

in.nextLine() --> separated by \n

String comparison:

(initialising two strings a and b)

a.equals(b)  (not by using "==" to compare)

a.compareTo(b); // based on the Unicode

String some other methods:

a.charAt(int index); // get the particular char at the position index

a.substring(int index); // get the sub-string starting from position index (included)

a.substring(int start, int end); // get the sub-string from start(included) to end (excluded)

a.indexOf(char); // find the char in string a and return the position of the first found

// (-1 if char doesn't exist)

a.indexOf(char, int); // find the char, starting from position int (included)

a.startsWith(b); a.endsWith(b) // check if a starts/ends with substring b

a.trim(); // delete the spaces at two ends of the string

a.replace(c1, c2); // replace c1 with c2

a.toLowerCase(); a.toUpperCase()

NB:

instances of String are immutable !!!

1.4 Math类

some methods:

Math.abs(number); // absolute value

Math.round(number); // rounding

Math.random(); // random number between 0 and 1

Math.pow(a, b); // a to the power of b

第2章--类与对象

2.1 用类制造对象

2.2 定义类

UML for classes

2.3 成员变量和成员函数

2.4 对象初始化

initialization:

for local variable: error when using an uninitialised local variable

for object variable: auto initialisation to 0/false/null...

step into constructor:

new object;

invoke constructor automatically;

execute the initialisation code outside the constructor;

execute the code inside the constructor.

constructor overload 重载:

inside constructor: this(args);

NB: if we use this();, it must be the first (several) statement in the constructor

2.5 对象交互

low coupling - 低耦合

problem domain - 问题领域

2.6 访问属性

private: visible inside the class (here is "class" rather than "object")

eg.

public class Fraction {
private int r;
public Fraction func (Fraction b) {
a.r = b.r;
}
}

if the visibility is not set, it becomes to package-visible by default.

compilation unit 编译单元 -- each .java file could be considered as one compilation unit.

each compilation unit has maximum one public class (same name as the name of compilation unit),

(and could have zero to many classes).

2.7 包

2.8 类变量和类函数

static variable: using object or class to access the static variable inside; using static method to access the variable.

static variable will be initialised when the class is loaded (当类被装载时)

第3章--对象集合

3.1 顺序容器

It is the reference of an object that is put in the container rather than a copy of the object.

Changing the object by itself (not via the container access) will also change the object contained by the container.

3.2 对象数组

An array of objects contains the references to those object elements.

need to create each of the element. (before creation: null: NullPointerException).

For each loop:

for (int k: array) {} // each k is a copy of an element in array. -- changing k does not modify the element in array

for (Classname c: array) {} // each c is a copy of the reference to an element -- the changing does something indeed.

for each loop works for container as well.

A container variable can be System.out.print() directly -- thanks to toString();.

3.3 集合容器(set)

set: no duplicated element; no order.

HashSet<type> name = new HashSet<type>();

3.4 散列表(hash)

hash table: key-value pairs

HashMap<type, type> name = new HashMap<type, type>();

if the key in a new key-value pair exists already, it got overwriten.

traverse hashmap:

for( Integer k : map.keySet()) {

e = map.get(k);

}

第4章--继承与多态

4.1 继承

4.2 子类父类关系

父类成员访问属性

在父类中的含义

在子类中的含义

public

对所有人开放

对所有人开放

protected

只有包内其它类、自己和子类可以访问

只有包内其它类、自己和子类可以访问

缺省

只有包内其它类可以访问

如果子类与父类在同一个包内:只有包内其它类可以访问

否则:相当于private,不能访问

private

只有自己可以访问

不能访问

public的成员直接成为子类的public的成员,protected的成员也直接成为子类的protected的成员。Java的protected的意思是包内和子类可访问,所以它比缺省的访问属性要宽一些。

父类的private的成员在子类里仍然是存在的,只是子类中不能直接访问。

不可以在子类中重新定义继承得到的成员的访问属性。

the order of initialisations:

1. parent's definition initialisations

2. parent's consturctor

3. child's definition initialisations

4. back to child's constructor

If there is no explict invoking of the super() in the child's constructor, it will automatically call the parent's constructor which has no argument passed in.

如果我们试图重新定义一个在父类中已经存在的成员变量,那么我们是在定义一个与父类的成员变量完全无关的变量,在子类中我们可以访问这个定义在子类中的变量,在父类的方法中访问父类的那个。尽管它们同名但是互不影响。

public class Item {
private String title;
public void setTitle(String s) {
this.title = s;
}
} public class CD extends Item {
private String title;
public CD(String title) {
this.title = title; // cd-title
setTitle("aaa"); // item-title
}
}

4.3 多态变量和向上造型

class type cast:

Item item = new Item(..);
CD cd = new CD(...); // class CD extends Item
item = cd; // works fine
CD cd1= (CD)item; // type cast
Item item = new Item(...);
CD cd = (CD)item; // compiling ok, but exception is thrown when executing

cast here (造型) is different from the cast in the example (类型转换): double a = (double)b;

向上造型是默认的,也总是安全的

4.4 多态

子类中的方法取代了父类的方法--override,父类的这个方法被“覆盖”起来而看不见了。而当通过父类的对象调用这个方法时,实际上执行的仍然是父类中的这个方法。注意我们这里说的是对象而不是变量,因为一个类型为父类的变量有可能实际指向的是一个子类的对象。

当通过对象变量调用一个方法时,究竟应该调用哪个方法,这件事情叫做绑定。绑定表明了调用一个方法的时候,我们使用的是哪个方法。绑定有两种:一种是早绑定,又称静态绑定,这种绑定在编译的时候就确定了,根据变量的声明类型来决定;另一种是晚绑定,即动态绑定。动态绑定在运行的时候根据变量当时实际所指的对象的类型动态决定调用的方法。

Java缺省使用动态绑定。

4.5 类型系统

单根结构:Object class

toString() can have super.toString() inside;

equals(): whether two references refer to the same object

@Override
public boolean equals(Object obj) {
CD cd = (CD)obj;
return artist.equals(cd.artist);
}

章后习题

1. 给定以下代码:

  1. public class Test {
    private int i = f();
    private int j = 10;
    private int f() {
    return j;
    }
    public static void main(String args[]) {
    System.out.println((new Test()).i);
    }
    }

以下哪句是正确的?

  • A. 由于在main中访问了Test的私有的变量而不能编译
  • B. 通过编译,打印出10
  • C. 由于成员变量定义初始化时的顺序错误而不能编译
  • D. 通过编译,打印出0

2. 给定以下代码:

 public static void main(String[] args) {
String s1 = "Hello";
String s2 = "Hello";
String s3 = "Hellp";
s3 = s3.replace('p', 'o'); System.out.println(s1==s2);
System.out.println(s1==s3);
}

3. 给定以下代码:

private class Base{

    Base(){
int i = 100;
System.out.println(i);
}
} public class PriBase extends Base{ static int i = 200; public static void main(String argv[]){
PriBase p = new PriBase();
System.out.println(i);
}
}

4. 给定以下代码:

interface I {
void setValue(int val);
int getValue();
}

以下哪段代码能编译?

A.
abstract class C implements I {
int value;
public void setValue(int val) { value = val; }
public int getValue() { return value; }
}

B.

interface A implements I {
void increment();
}
 
C.
class A extends I {
int value;
void setValue(int val) { value = val; }
int getValue() { return value; }
}
 
D.
class A extends I {
void increment();
}

5. 有以下代码:

public class Equivalence {
public static void main(String[] args) {
Integer n1 = new Integer(47);
Integer n2 = new Integer(47);
System.out.println(n1 == n2);
}
}

答案:

1. D

2. true, false

3. Illegal modifier for the class Base; only public, abstract & final are permitted

4. A

5. false


Java应用基础微专业-进阶篇的更多相关文章

  1. Java应用基础微专业-工程篇

    第1章-命令行 1.1 命令行基础 ls -a: list all files (including hidden files) .DS_Store: files detailed informati ...

  2. Java应用基础微专业-设计篇

    第1章--抽象与接口 1.1 抽象 An abstract class can be created without abstract methods, the purpose of doing th ...

  3. Java应用基础微专业-入门篇

    第1章--用程序来做计算 1.1 第一个Java程序 Mac version: Preference -> General -> Keys -> Search "Conte ...

  4. 总结删除文件或文件夹的7种方法-JAVA IO基础总结第4篇

    本文是Java IO总结系列篇的第4篇,前篇的访问地址如下: 总结java中创建并写文件的5种方式-JAVA IO基础总结第一篇 总结java从文件中读取数据的6种方法-JAVA IO基础总结第二篇 ...

  5. 总结java中文件拷贝剪切的5种方式-JAVA IO基础总结第五篇

    本文是Java IO总结系列篇的第5篇,前篇的访问地址如下: 总结java中创建并写文件的5种方式-JAVA IO基础总结第一篇 总结java从文件中读取数据的6种方法-JAVA IO基础总结第二篇 ...

  6. Java IDE 编辑器 --- IntelliJ IDEA 进阶篇 生成 hibernate 实体与映射文件

    原文:转:Java IDE 编辑器 --- IntelliJ IDEA 进阶篇 生成 hibernate 实体与映射文件 2011-04-30 12:50 很多人不知道怎么用 IntelliJ IDE ...

  7. 归纳从文件中读取数据的六种方法-JAVA IO基础总结第2篇

    在上一篇文章中,我为大家介绍了<5种创建文件并写入文件数据的方法>,本节我们为大家来介绍6种从文件中读取数据的方法. 另外为了方便大家理解,我为这一篇文章录制了对应的视频:总结java从文 ...

  8. 总结java创建文件夹的4种方法及其优缺点-JAVA IO基础总结第三篇

    本文是Java IO总结系列篇的第3篇,前篇的访问地址如下: 总结java中创建并写文件的5种方式-JAVA IO基础总结第一篇 总结java从文件中读取数据的6种方法-JAVA IO基础总结第二篇 ...

  9. Python基础—面向对象(进阶篇)

    通过上一篇博客我们已经对面向对象有所了解,下面我们先回顾一下上篇文章介绍的内容: 上篇博客地址:http://www.cnblogs.com/phennry/p/5606718.html 面向对象是一 ...

随机推荐

  1. 【Node.Js】npm国内被墙的解决方法

    移动网就是坑,有VPN也上不去,真操蛋~先吐槽一下@中国移动 折腾了一晚上,总是报连接错误,导致我npm安装不上,查了半天资料,找到个靠谱的,粘贴过来备用. 原文地址:http://snoopyxdy ...

  2. Jquery Mobile通过超链接跳转后CSS样式不起作用的解决办法

    Jquery Mobile中的超链接默认是采用AJAX跳转的,ajax获取到页面的内容之后,就直接替换当前页面的内容了,它只是单纯的获取页面的HTML代码,并不会再去下载引用的CSS代码和JS代码,因 ...

  3. waring L16: uncalled segement ----keil

    1.keil中出现waring:uncalled segement 2.waring L16:这个应该是一个waring等级 3.  转载自 wpb3dm 在Keil C中,如果没有显式调用到定义过的 ...

  4. OpenID Connect Core 1.0(二)ID Token

    2.ID Token(ID Token) OpenID Connect主要是对OAuth 2.0 能够使得终端用户通过ID Token的数据结构进行验证.当客户端和潜在的其他请求声明,ID Token ...

  5. JAVA给你讲它的故事

    计算机语言如果你将它当做一个产品,就像我们平时用的电视机.剃须刀.电脑.手机等, 他的发展也是有规律的. 任何一个产品的发展规律都是:向着人更加容易使用.功能越来越强大的方向发展. 那么,我们的计算机 ...

  6. BFC的特性及使用场景

    BFC(Block Formatting Context)块级格式化上下文,是Web页面 CSS 视觉渲染的一部分,用于决定块盒子的布局及浮动相互影响范围的一个区域. BFC的特性: 1. 属于同一个 ...

  7. 阿里云Docker镜像仓库(Docker Registry)

    镜像仓库申请地址: https://cr.console.aliyun.com/cn-shanghai/instances/repositories   一.创建命名空间 例如daniel-hub   ...

  8. 关于CoreLocation定位服务的简单使用

    在我们发微博,发表空间内容,以及在朋友圈发表动态的时候,会发现有一个位置信息的控件.iOS中是如何定位我们的位置信息的呢?基于此写一个小Demo,供大家参考使用. 在iOS中,用于定位时需要我们导入以 ...

  9. react-router-dom实现全局路由登陆拦截

    相比与vue的路由集中式管理,能够很好的进行统一的路由操作,react的路由看起来更乱,想要进行像vue的全局路由管理不是那么得心应手.在我们的项目中,有很多页面是需要登陆权限验证的,最好的方式就是能 ...

  10. MongoDB如何释放空闲空间?

    当我们从MongoDB中删除文档或集合时,MongoDB并不会将已经占用了的磁盘空间释放,它会一直维护已经占用了磁盘空间的数据文件,尽管数据文件中可能存在大大小小的空记录列表(empty record ...