java基础复习(类和对象)

来源:互联网 发布:金庸武侠知乎 编辑:程序博客网 时间:2024/05/22 00:48
  • 构造函数(构造器)

1、this() super()都必须是构造函数里的第一句声明
若同时出现,那么原则是:
参数少的构造器用this调用参数多的,在参数最多的构造函数里调用 super

  • 静态变量、静态方法、常量

static:
被所有的实例共享

final:
不能改变

static variable:
(其实更准确的而讲,static 只能作用于field,因为variable是位于方法体内的,而variable不能被声明为static)

1)It is a variable which belongs to the class and not to object (instance).
2)Static variables are initialized only once, at the start of the execution. These variables will be initialized first, before the initialization of any instance variables.
3)A single copy to be shared by all instances of the class.
A static variable can be accessed directly by the class name and doesn’t need any object.
3)Syntax:Class.variable

static method:
1)It is a method which belongs to the class and not to the object (instance).
静态方法不能访问非静态数据域
2)A static method can access only static data. It can not access non-static data (instance variables) unless it has/creates an instance of the class.
3)A static method can call only other static methods and can not call a non-static method from it unless it has/creates an instance of the class.
4)A static method can be accessed directly by the class name and doesn’t need any object.
5)Syntax:Class.methodName()
6)A static method cannot refer to this or super keywords in anyway.

final static fields:
are global constants

可改变(mutable)与不可改变(immutable)对象
immutable:例如String
mutable:例如Date

不可改变需满足以下条件:
所有数据域都是私有的
没有对数据域提供公共的set方法
没有一个方法会返回一个指向可变数据域的引用

对于最后一点“没有一个方法会返回一个指向可变数据域的引用”
举例:

public final class Period {    private final Date start;    private final Date end;    /**     * @param  start the beginning of the period.     * @param  end the end of the period; must not precede start.     * @throws IllegalArgumentException if start is after end.     * @throws NullPointerException if start or end is null.     */    public Period(Date start, Date end) {        if (start.compareTo(end) > 0)            throw new IllegalArgumentException(start + " after "                                               + end);        this.start = start;        this.end   = end;    }    public Date start() {        return start;    }    public Date end() {        return end;    }    ...  // Remainder omitted}

乍一眼看觉得很正常
接下来:

// Attack the internals of a Period instanceDate start = new Date();Date end = new Date();Period p = new Period(start, end);end.setYear(78);  // Modifies internals of p!

于是,应该做出相应的防御措施

// Repaired constructor - makes defensive copies of parameterspublic Period(Date start, Date end) {    this.start = new Date(start.getTime());    this.end   = new Date(end.getTime());    if (this.start.compareTo(this.end) > 0)      throw new IllegalArgumentException(start +" after "+ end);}

但是,做了上述修改后,还会有漏洞:

// Second attack on the internals of a Period instanceDate start = new Date();Date end = new Date();Period p = new Period(start, end);p.end().setYear(78);  // Modifies internals of p!

进一步修改:

//Repaired accessors - make defensive copies of internal fieldspublic Date start() {    return (Date) start.clone();}public Date end() {    return (Date) end.clone();}

以上参考此网址

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 8岁儿童嘴唇起泡怎么办 宝宝嘴皮上火起泡了怎么办 上嘴唇起泡肿了怎么办 上嘴唇突然肿了怎么办? 醒来上嘴唇肿了怎么办 嘴巴突然肿了怎么办呢 下嘴唇肿起来了怎么办 上嘴唇肿了起泡怎么办 上火下嘴唇肿了怎么办 上火嘴唇都肿了怎么办 嘴唇起泡后肿了怎么办 嘴唇上有白点颗粒状怎么办 嘴唇缺了一块红怎么办 人得钩端螺旋体怎么办 脖子上有鸡皮肤怎么办 不结婚老了以后怎么办 丁克族老了怎么办知乎 2个月宝宝咳嗽怎么办 干活累的手疼怎么办 脸上长白色的癣怎么办 全身起红斑很痒怎么办 宝宝脖子红烂了怎么办 背上长红斑很痒怎么办 身上起风疙瘩很痒怎么办 身上起小包很痒怎么办 浑身起红包很痒怎么办 手太粗糙怎么办小窍门 小腿长疙瘩很痒怎么办 腿过敏起红疙瘩怎么办 肚子上起红疙瘩很痒怎么办 小蚂蚁咬了肿了怎么办 锦鲤鱼尾巴烂了怎么办 泰迪身上长白毛怎么办 鱼身上有红斑了怎么办 新买锦鲤不吃食怎么办 鱼身上有红血丝怎么办 大腿内侧有红色条纹怎么办 腿上出现红血丝怎么办 孕妇有脚气,很痒怎么办 孕晚期脚气很痒怎么办 孕期有脚气很痒怎么办