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();}
以上参考此网址
- java基础复习(类和对象)
- java基础复习之二——类和对象
- java复习-类和对象(一)
- java复习-类和对象(二)
- 复习JAVA面相对象(类和对象)
- JAVA复习3 Java类和对象
- java复习系列之对象和类
- java复习——类和对象
- java基础复习(抽象类和接口)
- java基础复习 面向对象,String类,正则表达式
- Java基础复习---对象构造(构造器)
- 复习 类和对象
- java基础(复习)
- Java基础复习:面向对象练习1
- JAVA基础和面向对象的复习
- Java基础复习---面向对象程序设计
- 复习Java基础之对象转型
- JAVA基础复习六-面向对象
- 数据结构实验之排序七:选课名单
- 函数、排序、线性数据结构
- Arduino寻迹与识别红绿灯
- java面向对象
- 2017 ccpc 秦皇岛C Crusaders Quest
- java基础复习(类和对象)
- selenium得到一个动态页面
- Java多线程下的网络通信编程--图片传输
- 2017 ccpc 秦皇岛E String of CCPC
- Scala学习
- 2017 ccpc秦皇岛L One-Dimensional Maze
- [Leetcode] Union Find
- 【Android】源码分析
- C#如何以管理员身份运行程序