java基础复习(类和对象)

  • 构造函数(构造器)

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 instance
Date 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 parameters
public 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 instance
Date 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 fields
public Date start() {
    return (Date) start.clone();
}

public Date end() {
    return (Date) end.clone();
}

以上参考此网址

猜你喜欢

转载自blog.csdn.net/normol/article/details/78881644