学习高校课程-软件设计模式-OOP 和 UML 类图 OOP 与 Java(lec1)

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
    – 您想要利用多重继承

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/796616.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

路由器刷机踩坑

ssh/telnet 路由器一般是用23端口,也就是telnet。具体如何开启ssh看型号。 breed breed是一个bootloader,在功能上类似于手机的Rec,用于提供一个界面刷入不同分区的镜像(至少不用敲命令行了)。 注意 breed不能用于刷入原厂固件,否则会砖(我就是这么干的)。 这是恩山论坛…

Pbootcms网站挂马解决方案(详细说明)

当你的PbootCMS网站被挂马时,应该立即采取措施来清除恶意代码,并加强系统的安全性,防止未来的攻击。以下是一个详细的解决方案: 1. 备份现有数据 在开始任何修复工作之前,首先备份你的网站数据,包括数据库和文件系统。这一步骤是为了防止在修复过程中丢失重要数据。 2. 检…

SVN在MacOS下报E230001错误

#macos #rider for Mac #SVN #E230001svn为什么会报E230001错误呢?根据详细错误信息Server SSL certificate verification failed: certificate issued知道这是https证书有问题,不用管它证书了,这里介绍一种简单的方法。 首先,打开终端(terminal,mac os/linux一般都是带…

面试-JS Web API - 存储

cookie HTML5存储(localStorage 和 sessionStorage)cookie cookie本身用于浏览器和server通讯的,被借用到本地存储来。可以用document.cookie来修改。同一个变量会覆盖,不同变量会追加。localStorage 和 sessionStorage// 保存数据到 localStorage localStorage.getItem(a,10…

MySQL8的新特性

一 字典数据与资源管理1.1 数据字典 以前MySQL都是采用元数据文件、非事务性表结构或者是存储引擎特有的方式来存储字典数据,这些字典数据通常都是以数据对象为主,比如说最常见的表结构信息等。 在MySQL8.0中,这些字典数据都被移动到拥有InnoDB存储引擎的事务性表中进行存储…

通义灵码获得国产 AI 编码工具最高成绩丨阿里云云原生 8 月产品月报

《阿里云云原生每月动态》,从趋势热点、产品新功能、服务客户、开源与开发者动态等方面,为企业提供数字化的路径与指南。云原生月度动态 云原生是企业数字创新的最短路径。 《阿里云云原生每月动态》,从趋势热点、产品新功能、服务客户、开源与开发者动态等方面,为企业提供…

执行yarn命令的时候报错: error Error: certificate has expired

完整错误:原因是:SSL证书过期 执行命令关闭验证: yarn config set strict-ssl false

《抽象代数》系列之群论入门

一、重要性 1.1 领域意义 群论是数学的一个分支,主要研究代数结构中的群、环、域等。尽管它看似抽象,但在编程领域,群论有着广泛的应用和深刻的意义。算法设计与优化:群论在算法设计中发挥着重要作用。例如,在密码学中,群论被用于设计安全的加密算法,如椭圆曲线密码学,…

【转】Chrome 的无头模式升级了:推出 --headless=new

Chrome的无头模式变得更好用了!本文概要介绍了近期的工程工作,让 Headless 更接近Chrome的常规“Headful”模式,让Headless对开发者更有用。 背景 早在 2017 年,Chrome 59 便引入了所谓的无头模式,可让您在没有任何可见界面的无人值守环境中运行浏览器。从本质上讲,就是在…

Hadoop(六)生产集群搭建(三)

完全分布式运行模式 一、群起集群 1、配置workers [user@hadoop102 hadoop]$ vim /opt/module/hadoop-3.1.3/etc/hadoop/workers在文件中添加如下内容: hadoop102 hadoop103 hadoop1042、启动集群 (1)如果集群是第一次启动,需要在hadoop102节点格式化NameNode [user@hadoop…

延迟退休!我们又可以愉快的打工了

延迟退休! 我们又可以愉快的打工了希望可以活到办理退休那天🐸本文来自博客园,作者:bigroc,转载请注明原文链接:https://www.cnblogs.com/bigroc/p/18412267blog:http://www.bigroc.cn博客园:https://www.cnblogs.com/bigroc

面试官:线程池遇到未处理的异常会崩溃吗?

首先,这个问题考察的是你对线程池 execute 方法和 submit 方法的理解,在 Java 线程池的使用中,我们可以通过 execute 方法或 submit 方法给线程池添加任务,但如果线程池中的程序在执行时,遇到了未处理的异常会怎么呢?接下来我们一起来看。 1.execute方法 execute 方法用于…