gpt4 book ai didi

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

转载 作者:撒哈拉 更新时间:2024-09-13 16:19:21 59 4
gpt4 key购买 nike

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)的文章就讲到这里了,如果你想了解更多关于学习高校课程-软件设计模式-OOP和UML类图OOP与Java(lec1)的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

59 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com