首页 > 编程语言 >学习高校课程-软件设计模式-OOP 和 UML 类图 OOP 与 Java(lec1)

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

时间:2024-09-13 16:13:34浏览次数:1  
标签:Java methods 子类 类图 接口 OOP class

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

标签:Java,methods,子类,类图,接口,OOP,class
From: https://www.cnblogs.com/Mephostopheles/p/18411950

相关文章

  • java中cloneable接口的作用是什么
    在Java中,Cloneable接口是一个标记接口,它没有任何方法。它的存在是为了告诉编译器,该类可以被克隆(复制)。通过实现Cloneable接口,表示该类的实例可以通过调用clone()方法进行复制。clone()方法是Object类中的一个protected方法,它用于创建并返回一个与原始对象相同的副本。需要注意的......
  • 游戏客户端中的网络通信:使用Java实现实时数据交换| 网络通信|Java|游戏客户端
    在现代游戏开发中,Java作为一种强大的编程语言,广泛应用于游戏客户端的开发。Java的跨平台特性、丰富的类库以及强大的社区支持使其成为开发高性能、可维护的游戏客户端的理想选择。从简单的2D游戏到复杂的3D游戏,Java能够提供稳健的解决方案来应对各种挑战。本篇文章将深入探讨如何利......
  • 基于JAVA办公自动化系统的计算机毕设
    摘  要极光办公自动化系统是针对南开创元信息技术有限公司开发的,专门用于企业内部员工信息交流的软件,其开发过程主要包括前端应用程序的开发和后端数据库的建立两个方面。对于前者要求应用程序功能完备操作简单,对于后者要求建立起数据一致性、完整性和安全性好的数据库。本论......
  • Java【异常处理】
    机制概述JVM用方法调用栈来跟踪一系列方法的调用过程,位于栈顶的是正在执行的方法,当一个方法抛出异常时,根据代码处理异常。异常常见类型 检查性异常:最具代表的检查性异常是用户错误或问题引起的异常,这些异常在编译时强制要求程序员处理。运行时异常: 这些异常在编译时不强......
  • java父类、子类构造函数调用过程
    java父类、子类构造函数调用过程由此看出java类初始化时构造函数调用顺序:初始化对象的存储空间为零或null值;按顺序分别调用父类成员变量和实例成员变量的初始化表达式;调用父类构造函数;(如果实用super()方法指定具体的某个父类构造函数则使用指定的那个父类构造函数)按顺序分别......
  • Java21的虚拟线程来了
    1、什么是平台线程?平台线程是作为操作系统(OS)线程的薄包装器实现的。平台线程在其底层OS线程上运行Java代码,并且平台线程在其整个生命周期内捕获其OS线程。因此,可用的平台线程数量受限于OS线程的数量。平台线程通常具有较大的线程堆栈和由操作系统维护的其他资源。它们......
  • Hadoop(七)集群搭建过程中遇到的问题及解决方法
    遇到的问题及解决方法1、Hadoop启动正常,但是进不了web端hadoop102:9870解决方法:查看自己的hosts文件(C:\Windows\System32\drivers\etc),发现没有配置相关网点,添加如下内容(不需要在前面加'#'):192.168.10.100hadoop100192.168.10.101hadoop101192.168.10.102hadoop102192.168.1......
  • Hadoop(六)生产集群搭建(三)
    完全分布式运行模式一、群起集群1、配置workers[user@hadoop102hadoop]$vim/opt/module/hadoop-3.1.3/etc/hadoop/workers在文件中添加如下内容:hadoop102hadoop103hadoop1042、启动集群(1)如果集群是第一次启动,需要在hadoop102节点格式化NameNode[user@hadoop102had......
  • JAVA时间转换总结
    JAVA时间转换总结 1.格式化时间Date~2022-03-2403:30:13SimpleDateFormatformat=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");StringdateStr=format.format(newDate());2.格式化时间2022-03-24T03:30:13.000000~2022-03-2403:30:13......
  • java基础之继承
     1.一个子类只能有一个直接父类(一个父类可以多个子类)2.private修饰符和void不能继承、不同包不能继承代码如下: 父类packagebegan;//定义一个父类publicclassPet01{//属性publicStringname;//方法publicvoidrun(Stringname){Sy......