博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cs108 04 oop design
阅读量:7235 次
发布时间:2019-06-29

本文共 2170 字,大约阅读时间需要 7 分钟。

oop design 分为以下几个方面:

- encapsulation and modularity(封装和模块化)

- API/Client interface design(API 接口给调用类者)

- Inheritance hierarchy and interfaces(继承和继承的层次关系)

这里先介绍一个modularity模块化:

一个大的系统是很复杂的, 可能包含了很多复杂的部分, 如果编码中的一个部分可以独立出来而被其他部分所使用, 例如之前的cs106中的画火车的例子, 程序经过软件设计工程分解成小任务后, 不是直接开始编码, 而是看看有哪些部分可以独立出来, 这个独立出来的部分可以被很多子任务所使用(例如画车厢), 火车的车头, 车身和车尾都能利用上, 这个就是模块化 modularity – keep components as separate and independent from each other as possible. 同时, 将哪些部分独立编码进行模块化是需要很多经验的.

Standard Model OOP class Design(标准类设计)

  • Easy for the Client, 做到用户不用读文档, 就能很好的使用你提供的API.
  • Data private
  • one or more constructors(最好多个构造方法)
  • public getters, 给用户提供一些获得部分data的get方法.
  • public setters, 这个可以增加逻辑判断, 看用户修改的部分数据是否合理.
  • Methods as Transactions(参考了数据库中的transactions, 也就是说一个方法执行前是一个状态, 执行后是一个状态, 如果执行过程中出现问题, 那么要退回到执行前的状态并返回一个message)
  • Pirvate utility, You may decompose out a method for your own use within your class – declare it private unless there’s a good reason to expose it for client use.

继承

oop中经常出现的错误就是到处都是继承.

In java, no matter what code is being executed, the receiver object never forgets its class.

Student s; 这个声明的含义是什么 ?

s points to an object that responds to all the messages that Sutdents respond to

s points to a Student, or a subclass of Student

继承的语法与特点

subclass problems:

1. construct the part of the object that is inherited, using the superclass

   在构造函数的第一行, 执行 super(arguments)

2. construct the part of the object due to the class itself.

   在构造函数的第一行, 调用自己的另外一个构造函数

覆盖 即 重写

To override a method, a subclass just defines a method with exactly the same prototype – same name and arguments. With Java 5, you can provide an @Override annotation just before the method, and the compiler will check for you that it matches some superclass method.

断言方法

一般使用 is 或 has 开头的返回 boolean 的方法

IS-a : 子类是一个父类, has-a: 一个类中有另一个类作为此类的一部分

instance of : 一个 object 判断是否是一个类的实例, 例如: Student s = new Student(); s instanceof Student

Object class 提供哪些方法:

  • toString()
  • equals(Object other)
  • int hashCOde() allows an object to be a key in a HashMap, HashCode should be fast to compute. if a class does not implements hashCode() then it cannot be a key in a Hashmap

转载于:https://www.cnblogs.com/moveofgod/p/3816018.html

你可能感兴趣的文章
KindEditor编辑器在j2ee项目中的运用及常用问题回顾
查看>>
vue使用的经验总结
查看>>
Win7系统优化十大技巧
查看>>
uio浅析
查看>>
PHP中$_FILES的使用及注意事项
查看>>
Linux命令之which
查看>>
蓦然回头看JAVA线程变量 ThreadLocal
查看>>
Centos 6.5 装机后安装设置,含脚本
查看>>
mysql self join的实现--left join 和inner join
查看>>
http请求
查看>>
国学与科学:如何培养中国人的科学创新思考技术
查看>>
MergeSort (归并排序)
查看>>
1-CLR线程池的作用与原理浅析
查看>>
近似pi
查看>>
[Unity3D]程序退出进程及资源问题
查看>>
mysql导出表结构不导出数据
查看>>
zabbix安装出现的问题
查看>>
华为eNSP GRE
查看>>
赚钱的这些年(上)苦逼
查看>>
我的友情链接
查看>>