zoukankan      html  css  js  c++  java
  • cs108 04 oop design

    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
  • 相关阅读:
    mysql 更改字符集
    修改pip源
    git命令
    virtualwrapper使用
    Python环境搭建
    IntellIJ IDEA 配置 Maven 以及 修改 默认 Repository
    Spring4:JDBC
    Spring3:AOP
    Spring2:bean的使用
    Spring1:Spring简介、环境搭建、源码下载及导入MyEclipse
  • 原文地址:https://www.cnblogs.com/moveofgod/p/3816018.html
Copyright © 2011-2022 走看看