Java面向对象的一些相关概念及设计实例
1 面向对象的相关概念
1.1 面向对象
面向对象(Object
Oriented)是软件开发方法,一种编程范式。面向对象的概念和应用已超越了程序设计和软件开发,扩展到如数据库系统、交互式界面、应用结构、应用平台、分布式系统、网络管理结构、CAD技术、人工智能等领域。面向对象是一种对现实世界理解和抽象的方法,是一种新兴的程序设计方法,或者是一种新的程序设计规范(paradigm),其基本思想是使用对象、类、继承、封装、多态等基本概念来进行程序设计。从现实世界中客观存在的事物(即对象)出发来构造软件系统,并且在系统构造中尽可能运用人类的自然思维方式。
1.2 面向对象的相关概念
- 对象:
对象是指具体的某一个事物,即在现实生活中能够看得见摸得着的事物。在面向对象程序设计中,对象所指的是计算机系统中的某一个成分。在面向对象程序设计中,对象包含两个含义,其中一个是数据,另外一个是动作。对象则是数据和动作的结合体。对象不仅能够进行操作,同时还能够及时记录下操作结果。类的实例化可生成对象,一个对象的生命周期包括三个阶段:生成、使用、消除。
- 方法:
方法是指对象能够进行的操作,即函数。方法是类中的定义函数,其具体的作用就是对对象进行描述操作。
- 类:
类是具有相同特性(数据元素)和行为(功能)的对象的抽象。因此,对象的抽象是类,类的具体化就是对象,也可以说类的实例是对象,类实际上就是一种数据类型。在我的理解中,类就像是一个带有内置函数的C语言中的结构体。
- 接口:
在Java语言中,仅支持单一继承,即一个子类只能有一个直接的父类,然而在日常生活中,多继承的问题在所难免
如电子书既是一种图书,同时又是一种多媒体,这种继承关系要求子类继承多个父类。这样就可能导致子类具有多种多样的方法和实现形式。所以Java
不允许多继承,为了解决多继承的问题,Java 引入了接口的概念。 定义一个接口不能使用
class 关键字,而是使用 interface 关健字。
1.3 面向对象的核心特性
- 继承性:
继承简单地说就是一种层次模型,这种层次模型能够被重用。层次结构的上层具有通用性,但是下层结构则具有特殊性。在继承的过程中类则可以从最顶层的部分继承一些方法和变量。类除了可以继承以外同时还能够进行修改或者添加。通过这样的方式能够有效提高工作效率。
- 封装性:
封装是将数据和代码捆绑到一起,对象的某些数据和代码可以是私有的(private),不能被外界访问,以此实现对数据和代码不同级别的访问权限。防止了程序相互依赖性而带来的变动影响,面向对象的封装比传统语言的封装更为清晰、更为有力。有效实现了两个目标:对数据和行为的包装和信息隐藏。
- 多态性:
多态是指不同事物具有不同表现形式的能力。多态机制使具有不同内部结构的对象可以共享相同的外部接口,通过这种方式减少代码的复杂度。一个接口,多种方式。
2 面向对象的设计实例
设计实例即Lab2中Graph接口以及其具体实现的子类。
2.1 Graph接口
/**
* A mutable weighted directed graph with labeled vertices.
* Vertices have distinct labels of an immutable type {@code L} when compared
* using the {@link Object#equals(Object) equals} method.
* Edges are directed and have a positive weight of type {@code int}.
*
* <p>PS2 instructions: this is a required ADT interface.
* You MUST NOT change the specifications or add additional methods.
*
* @param <L> type of vertex labels in this graph, must be immutable
*/
public interface Graph<L> {
/**
* Create an empty graph.
*
* @param <L> type of vertex labels in the graph, must be immutable
* @return a new empty weighted directed graph
*/
public static <L> Graph<L> empty() {
return new ConcreteEdgesGraph<L>();
}
/**
* Add a vertex to this graph.
*
* @param vertex label for the new vertex
* @return true if this graph did not already include a vertex with the
* given label; otherwise false (and this graph is not modified)
*/
public boolean add(L vertex);
/**
* Add, change, or remove a weighted directed edge in this graph.
* If weight is nonzero, add an edge or update the weight of that edge;
* vertices with the given labels are added to the graph if they do not
* already exist.
* If weight is zero, remove the edge if it exists (the graph is not
* otherwise modified).
*
* @param source label of the source vertex
* @param target label of the target vertex
* @param weight nonnegative weight of the edge
* @return the previous weight of the edge, or zero if there was no such
* edge
*/
public int set(L source, L target, int weight);
/**
* Remove a vertex from this graph; any edges to or from the vertex are
* also removed.
*
* @param vertex label of the vertex to remove
* @return true if this graph included a vertex with the given label;
* otherwise false (and this graph is not modified)
*/
public boolean remove(L vertex);
/**
* Get all the vertices in this graph.
*
* @return the set of labels of vertices in this graph
*/
public Set<L> vertices();
/**
* Get the source vertices with directed edges to a target vertex and the
* weights of those edges.
*
* @param target a label
* @return a map where the key set is the set of labels of vertices such
* that this graph includes an edge from that vertex to target, and
* the value for each key is the (nonzero) weight of the edge from
* the key to target
*/
public Map<L, Integer> sources(L target);
/**
* Get the target vertices with directed edges from a source vertex and the
* weights of those edges.
*
* @param source a label
* @return a map where the key set is the set of labels of vertices such
* that this graph includes an edge from source to that vertex, and
* the value for each key is the (nonzero) weight of the edge from
* source to the key
*/
public Map<L, Integer> targets(L source);
}
2.2 具体实现
-
AF、RI、Safety from rep exposure
-
CheckRep:
-
ConcreteEdgesGraph中需要实现的方法(将方法全部实现即可):
-
ConcreteEdgesGraph:构造方法,不需要初始化变量;
-
checkRep:检查表示不变性;
-
add:添加一个顶点到顶点集vertices,不能重复加入;
-
set:输入起点source、终点target和边权值weight,若起点或终点不在vertices内,则调用add将其加入。若weight
< 0,则返回-1;当weight = 0时,若边存在,则删去该边;当weight >
0时,若该边不存在,则将该边直接加入edges,否则应删去原边后再将新的边和权值加入edges。改变了原边权值时,返回原边权值,边不存在则返回0; -
remote:去除某一点以及与它有关的所有边;
-
vertices:返回顶点集vertices;
-
sources:输入终点,返回所有指向该点的起点及边的权值;
-
targets:输入起点,返回该点指向的所有终点及边的权值;
-
toString:字符串形式输出图,每一条边的输出格式都与Edge类中toString方法中的返回形式相同。
原边后再将新的边和权值加入edges。改变了原边权值时,返回原边权值,边不存在则返回0; -
remote:去除某一点以及与它有关的所有边;
-
vertices:返回顶点集vertices;
-
sources:输入终点,返回所有指向该点的起点及边的权值;
-
targets:输入起点,返回该点指向的所有终点及边的权值;
-
toString:字符串形式输出图,每一条边的输出格式都与Edge类中toString方法中的返回形式相同。