Lecture 1:OOP and UML Class DiagramsOOP with Java

OOP 和 UML 类图 OOP 与 Java

Object-Oriented Programming 面向对象编程

Class Hierarchies 类层次结构

Superclass and subclass

超类和子类

Pillars of Object-Oriented Programming 面向对象编程的支柱

  1. Abstraction

    – Modelling attributes and behaviors of real objects, in specific contexts

    抽象——对真实对象的属性和行为进行建模,具体来说

  1. Encapsulation

    – Hiding parts of an object’s states and behaviors from others, and exposing a limited set of interfaces

    – public, private, and protected

    – Interfaces and abstract classes

    封装

    — 隐藏对象的部分内容和来自他人的状态和行为,并公开一组有限的接口

    – 公共、私有和受保护

    – 接口和抽象类

  2. Inheritance

    – Main benefit: code reuse

    继承

    — 主要好处:代码重用

  3. Polymorphism

    – Performing an action in many forms

    – A mechanism for detecting the real class of an object and call its implementation

    多态性

    多种形式执行一个动作

    一种检测对象的真实类并调用其实现方法的机制

OOP with Java: Declaring Classes and Creating Objects Java 的 OOP:声明类和创建对象

  • Class declaration

    类声明

  • Creating objects

    创建对象

    • Declaration, instantiation, initialization

      声明、实例化、初始化

    • The reference returned by the new operator does not have to be assigned to a variable

      new 运算符返回的引用不必分配给变量

OOP with Java: Access Control Java 的 OOP:访问控制

  • At the top level

    在顶层

    – public, or package-private (no explicit modifier)

    公共或包私有(无显式修饰符)
  • At the member level

    在成员级别

    – public, private, protected, or package-private (no explicit modifier)

    public、private、protected 或 package-private(无显式修饰符)

OOP with Java: Inheritance Java 中的 OOP:继承

  • Classes can be derived from other classes, inheriting fields and methods

    类可以从其他类派生,继承字段和方法

  • Definitions

    定义

    – Subclass (derived class/extended class/child class)

    – Superclass (base class/parent class)

    – 子类(派生类/扩展类/子类)

    – 超类(基类/父类)

  • Every class has one and only one direct superclass (single inheritance)

    每个类都有一个且仅有一个直接超类(单继承)

    – Excepting Object, which has no superclass

    除了Object,它没有超类

  • A subclass inherits all the members (fields, methods, and nested classes) from its superclass

    子类继承其超类的所有成员(字段、方法和嵌套类)

OOP with Java: What You Can Do in a Subclass Java 的 OOP:在子类中可以做什么

Use the inherited members as is, replace them, hide them, or supplement them

按原样使用继承的成员、替换它们、隐藏它们或补充它们

  • Declare a field in the subclass with the same name as the one in the superclass, thus hiding it (NOT recommended)

    – 在子类中声明一个与超类中的字段同名的字段,从而隐藏它(不推荐)
  • Write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it

    – 在子类中编写一个新的实例方法,其签名与超类中的字段相同,从而覆盖它
  • Write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it

    –在子类中编写一个新的静态方法,该方法与超类中的静态方法具有相同的签名,从而隐藏它
  • Write a subclass constructor that invokes the constructor of the superclass

    – 编写一个调用超类构造函数的子类构造函数

How about private members in a superclass?

OOP with Java: Abstract and Final Methods/Classes Java 的 OOP:抽象和最终方法/类

  • An abstract class is a class declared abstract: it may or may not include abstract methods

    抽象类是声明为抽象的类:它可能包含也可能不包含抽象方法
  • An abstract method is a method declared without an implementation

    抽象方法是声明但没有实现的方法
  • Final methods and classes

    最终方法和类

    Methods called from constructors should generally be declared final

    – 从构造函数调用的方法通常应声明为final

OOP with Java: Interfaces Java 的 OOP:接口

  • Interfaces are contracts

    接口是契约
  • A reference type, containing only constants, method signatures,default methods, static methods, and nested types

    引用类型,仅包含常量、方法签名、默认方法、静态方法和嵌套类型
  • Cannot be instantiated

    无法实例化

    – They can only be implemented by classes or extended by other interfaces

    – 它们只能由类实现或由其他接口扩展
  • Consisting of modifiers, keyword, interface name, a comma-separated list of parent interfaces (if any), and the interface body

    由修饰符、关键字、接口名称、以逗号分隔的父接口列表(如果有)和接口主体组成
  • Interface body can contain abstract methods, default methods,and static methods

    接口体可以包含抽象方法、默认方法和静态方法

OOP with Java: Implementing and Using Interfaces 使用 Java 进行 OOP:实现和使用接口

  • Include an implements clause in the class declaration

    在类声明中包含 implements 子句

    – Your class can implement more than one interface

    – 你的类可以实现多个接口
  • If you define a reference variable whose type is an interface,any object you assign to it must be an instance of a class that implements the interface

    如果定义类型为接口的引用变量,则分配给它的任何对象都必须是实现该接口的类的实例

OOP with Java: Abstract Classes vs. Interfaces Java 中的 OOP:抽象类与接口

  • Consider using abstract classes when

    考虑使用抽象类

    – You want to share code among several closely related classes

    – 您希望在几个紧密相关的类之间共享代码

    – You expect that classes extending the abstract class have many common methods or fields, or require access modifiers other than public

    – 您希望扩展抽象类的类具有许多通用方法或字段,或者需要除 public 之外的访问修饰符

    – You want to declare non-static or non-final fields

    — 您想要声明非静态或非最终字段

  • Consider using interfaces when

    考虑使用接口

    – You expect that unrelated classes would implement your interface

    – 您希望不相关的类实现您的接口

    – You want to specify the behavior of a particular data type, but not concerned about who implements its behavior

    – 您想要指定特定数据类型的行为,但不关心谁实现其行为

    – You want to take advantage of multiple inheritance

    – 您想要利用多重继承

学习高校课程-软件设计模式-OOP 和 UML 类图 OOP 与 Java(lec1)的更多相关文章

  1. 《大话设计模式》学习笔记0:设计模式的原则 && UML类图

    一.单一职责原则:就一个类而言,应该仅有一个引起它变化的原因. 如果一个类承担的职责过多,就等于把这些职责耦合在一起,一个职责的变化可能会削弱或者抑制这个类完成其他职责的能力.这种耦合会导致脆弱的设计 ...

  2. 设计模式——1.概述&UML类图和时序图

    声明:本博客设计模式相关文章均整理和修改自网络,原文地址:图说设计模式 学习设计模式的3个层次—— 1.熟悉所有设计模式: 2.能够用代码实现: 3.运用到工作的项目中. 设计模式指导软件开发,学习设 ...

  3. 设计模式学习笔记(详细) - 七大原则、UML类图、23种设计模式

    目录 设计模式七大原则 UML类图 设计模式分类 单例模式 工厂设计模式 简单工厂模式 工厂方法模式(使用抽象类,多个is-a) 抽象工厂模式(使用接口,多个like-a) 原型模式 建造者模式 适配 ...

  4. 设计模式-UML类图基础

    设计模式之序-UML类图那点事儿 打14年年底就像写那么一个系列,用于讲设计模式的,代码基于JAVA语言,最早接触设计模式是大一还是大二来着,那时候网上有人给推荐书,其中就有设计模式,当时给我推荐的书 ...

  5. 设计模式之序章-UML类图那点事儿

    设计模式之序-UML类图那点事儿 序 打14年年底就像写那么一个系列,用于讲设计模式的,代码基于JAVA语言,最早接触设计模式是大一还是大二来着,那时候网上有人给推荐书,其中就有设计模式,当时给我推荐 ...

  6. 从零开始单排学设计模式「UML类图」定级赛

    阅读本文大概需要 3.5 分钟. 本篇是设计模式系列的开篇,虽然之前也写过相应的文章,但是因为种种原因后来断掉了,而且发现之前写的内容也很渣,不够系统. 所以现在打算重写,加上距离现在也有一段时间了, ...

  7. (转)面向对象——UML类图设计

    背景:一直以来,对UMl类图的画法不甚理解,但是随着学习的深入,发现熟练掌握UML类图,能够更好理解代码间的逻辑性,而这也是程序设计的基础所在,所以很有必要把UML好好掌握. UML类图新手入门级介绍 ...

  8. 全面认识UML类图元素

    本节和大家一起学习一下UML类图元素,类图能出色地表示继承与合成关系.为了将UML类图作为一种高效的沟通工具使用,开发者必须理解如何将类图上出现的元素转换到Java中.请看本节详细介绍. 全面认识UM ...

  9. C# UML类图及类之间的几种关系

    今天晚上看了大话设计模式中的UML类图这一节感觉受益匪浅,好多年不能理解的类之间的关系理解了. 一.UML类图的表示方法 1.类的表示方法 A类用一个矩形框分三层表示,第一层是类名,斜体类名表示抽象类 ...

  10. 设计模式学习起点 UML类图笔记

    UML类图笔记 大学开设的软件设计课程一般都会学习UML类图,大部分关于设计模式的描述都是使用的UML类图,可以说类图的表示是学习设计模式的起点.UML定义类之间的关系主要有六种:泛化关系.实现关系. ...

随机推荐

  1. 疑难杂症(已解决) | 为什么出现python中tkinter创建界面需要一闪而过才进入主窗口?

    一.具体问题 如图所示,我编写了一个主窗口的程序(如下所示,有兴趣的可以自己复制后运行),发现需要先进入第一个窗口再进入主界面,这不符合逻辑. 代码区域(完整代码): from tkinter imp ...

  2. PN转232网关模块接扫码枪与CPU通讯

    在现代物流.汽车生产线等领域,广泛使用条码扫码枪快速扫描产品条码,提高工作效率.为了保证条码扫码枪与CPU之间的准确通信,PN转232网关模块成为关键部件.本文将深入研究PN转232网关模块(BT-P ...

  3. 题解:AT_abc360_c [ABC360C] Move It

    背景 机房大佬掉大分了,乐悲. 题意 给你几个箱子和每个箱子里装有的东西 \(a\) 和对应的重量 \(w\),现在要让每个箱子里都装有一个东西,每次可以移动任意一个箱子中的任意一个东西,代价为它的重 ...

  4. WCF异常System.ServiceModel.ProtocolException问题处理

    现象: 最近遇到了WCF 服务无法调用的错误,异常如下. System.ServiceModel.ProtocolException, System.ServiceModel, Version=4.0 ...

  5. 如何用 WinDbg 调试Linux上的 .NET程序

    一:背景 1. 讲故事 最新版本 1.2402.24001.0 的WinDbg真的让人很兴奋,可以将自己伪装成 GDB 来和远程的 GDBServer 打通来实现对 Linux 上 .NET程序进行调 ...

  6. springboot3整合高版本spring data neo4j

    本博客适用于springboo data neo4j 7.2.6版本,详情阅读官网https://docs.spring.io/spring-data/neo4j/reference/7.2/intr ...

  7. canvas实现截图功能

    开篇 最近在做一个图片截图的功能. 因为工作时间很紧张, 当时是使用的是一个截图插件. 周末两天无所事事,来写一个简单版本的截图功能. 因为写的比较简单,如果写的不好,求大佬轻一点喷 读取图片并获取图 ...

  8. 14、Spring之基于注解的声明式事务

    14.1.概述 14.1.1.编程式事务 事务功能的相关操作全部通过自己编写代码来实现: Connection conn = ...; try { // 开启事务:关闭事务的自动提交 conn.set ...

  9. 【C3】04 工作原理

    我们已经知道了CSS是做什么的以及怎么写简单的样式这样基础的CSS, 接下来我将了解到浏览器如何获取CSS.HTML和将他们加载成网页. 前置知识: 基础计算机知识.基本软件安装.简单文件知识.HTM ...

  10. 【SpringMVC】11 拦截器

    拦截器是AOP具体的应用 只能使用SpringMVC自己的组件有效 之拦截访问控制器方法的请求, 如果访问的是jsp.html.css.img.js这一类的静态资源,则不会拦截 演示: 编写一个拦截器 ...