面向对象程序设计(Object Oriented Design)

OOD的流程:

需求分析——>系统/程序设计——>实现这个设计——>测试

Class Design

  1. Identify classes for the system.

  2. Describe attributes and methods in each class.

  3. Establish relationships among classes.

  4. Create classes.

Identity classes and objects

  A fundamental part of object-oriented software design is determining the classes that will contribute to the program.

  One way to identify potential classes is to identify the objects discussed in the program requirements.

  Objects are generally nouns.

  Obviously, not all nouns are objects, some of them can be attributes of objects.

  A class: a group of objects with similar attributes and behaviours. A plural in the specification may indicates that you need a class for those items, e.g., products, students, users… Some attributes are too complex to be represented by primitive values.

   In such cases, we need to create classes to represent them as well.

Static variables and methods

  Static variable (class variable): a common field shared by all objects of the class.

  Static method (class method): can be invoked through the class name.

  We don’t have to instantiate an object of the class in order to invoke the method. E.g.: Math.sqrt(27);

finals

  1)final class The class can NOT be used as a superclass(leaves)

    public final class BClass{ ….. }

  2)final method A final method cannot be overridden

  3)Final variable: a final variable can only be initialized once, either via an initializer or an assignment statement  If the variable is a reference, this means that the variable cannot be re-bound to reference another object. But the object that it references is still mutable, if it was originally mutable.

        

Relationships among Classes

  Association关联

  Aggregation聚合

  Composition组合

  Inheritance继承

1)Association

  Association represents a general relationship that describes an activity between two classes.

   Often the methods of one class will invoke the methods of the other class, i.e., a “uses” relationship.

   Generally, if class A uses class B, then one more methods of class B will be invoked by class A (from one or more methods in class A).

2)Aggregation and Composition

  Some objects are made up of other objects, e.g., a car is made up of its engine, its chassis, its wheels, and several other parts.

We can say that a car is an aggregation, or it is composed of other objects.Aggregation is sometimes described as a has-a relationship, e.g., a car has a chassis.

Aggregation is a special type of association. That is, a class that is defined in part by another class is dependent on that class. The methods of the aggregate object generally invoke the methods of the objects from which it is composed.

  Composition is a special aggregation.

  It defines a “1 to 1” has a relationship

Example:

Composition : Engine is part-of Car, and if you believe it is completely encapsulated by car, not reusable, or switchable, the relationship between them is Composition.

public class Car {

  private final Engine engine;

   Car(String engNumber) {

  engine = new Engine(engNumber);

  }

  void move() {

  engine.work();

  }

}

Aggregation : If you think the engine can exist without a car, and it can be swapped, and the outside world can still have a reference to the engine, then you can define the relationship as an aggregation:

public class Car {

   private Engine engine;

   Car(String engNumber) {

  engine = new Engine(engNumber);

   }

  void move() {

   engine.work();

  }

}

Association: Driver just makes use of a key. A key is not necessary to be a member of a driver.

public class Key{

   public void startEngine() {

   // use key to start the engine of a car

   }

  }

  public class Driver{

    public void drive(Key key) {

      key.startEngine();

  }

}

        

Inheritance:

  Inheritance models the is-an-extension-of relationship between two classes.

          

Field initialisation:

  When creating an object of a subclass: Initialize them by invoking a superclass constructor with the appropriate parameters: Use the key word: super() or super(arg1, arg2, …)

  Placed in the first line

   Choose proper parameters

  If the subclass constructor skips calling the superclass , then Java automatically calls the no-parameter one

   Note: Initialise superclass fields before subclass starts to initialize its part of the object

继承构造器

Reference Passing(引用传递)

  SuperClass supObj=new SuperClass(…);

  SubClass subObj=new SubClass(…);

  supObj=subObj;

因为存在SpecialSubclassMember 所以supObj可以访问自己和sub的内存,而sub只能访问自己的。

          

Method Overriding

  Subclass to provide a specific implementation of a method that is already provided by its superclasses

  To override a method: The method has the same name with the method of the superclass Same arguments as well

  public class SuperClass {

  public int methodA (int i, String s) {

  bodyA

  }

  }

   public class SubClass extends SuperClass {

    public int methodA (int i, String s) {

      bodyB

     }

    }

  If we call methodA from an instance of SuperClass bodyA runs;

   if we call methodA from an instance of SubClass bodyB runs In SubClass, we can access bodyA with: super.methodA(inti, String s) Static methods cannot be overwritten

Is a vs. Has a

  Model an is-a relationship with inheritance

  If every instance of Class2 is also a Class1,

  then Class2 is-a Class1 Class2 is the subclass of Class1

  Class1 is the superclass of Class2

  In Java, this is expressed as Class2 extends Class1:

  public class Class2 extends Class1 { ... }

   In Java: the is-a relationship is denoted by keyword extends a subclass can ONLY extend ONE superclass

          

Inheritance vs. Aggregation

Inheritance: … is a …

  E.g., a Dog is Animal, i.e., Dog is an extension of Animala student is people

Aggregation: … has a …

   E.g., a person has a name, i.e., use aggregation to model the relationship between Person and Name.

Interfaces vs. Abstract Classes

  Both interfaces and abstract classes can be used to generalize common features.

  Abstract class defines strong is-an-extension-of relationship.

  It clearly describes a parent-child relationship Interfaces are weaker (… like a …).

  Interfaces are more flexible than abstract classes, because a subclass can extend only one superclass, but implement any number of interfaces.

  Interfaces cannot contain concrete methods.

  You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface.

  So you can use the interface or its companion class whichever is more convenient.

时间: 2024-11-12 13:25:09

面向对象程序设计(Object Oriented Design)的相关文章

Go Object Oriented Design

Go hastypes and valuesrather than classes and objects. So can a language without classes or objects be object-oriented? While Go may not fit the typical mold of an OOP language, it does  provide many of the same features, albeit in a slightly differe

面向对象编程Object Oriented Programming(OOP)

把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. 面向过程的程序设计把计算机程序视为一系列的命令集合,即一组函数的顺序执行.为了简化程序设计,面向过程把函数继续切分为子函数,即把大块函数通过切割成小块函数来降低系统的复杂度. 面向过程的程序设计把计算机程序视为一系列的命令集合,即一组函数的顺序执行.为了简化程序设计,面向过程把函数继续切分为子函数,即把大块函数通过切割成小块函数来降低系统的复杂度. 一:封装(类内的事) 假设我们要处理学生的成绩表,为了表示一个学生的成绩,面向过程的

面向对象编程(Object Oriented Programming,OOP)

类是一个通用的概念,Java.C++.C#.PHP等很多编程语言中都有类,都可以通过类创建对象.可以将类看做是结构体的升级版,C语言的晚辈们看到了C语言的不足,尝试加以改善,继承了结构体的思想,并进行了升级,让程序员在开发或扩展大中型项目时更加容易. 因为Java.C++等语言都支持类和对象,所以使用这些语言编写程序也被称为面向对象编程,这些语言也被称为面向对象的编程语言.C语言因为不支持类和对象的概念,被称为面向过程的编程语言. 实际上,面向对象只是面向过程的升级. 在C语言中,可以将完成某个

C++面向对象程序设计之类和对象的特性

类和对象的属性 注意:本文为书籍摘要版,适合有一定程序基础的人阅读. 2.1 面向对象程序设计方法概述 2.1.1 什么是面向对象的程序设计 1.对象 客观世界中的任何一个事物都可以看成一个对象. 如果我们把一个班级作为一个对象时有两个要素:一个是班级的静态特征,如班级学生的身高体重(可认为不变)等不变的特征,我们称为“属性”(即数据):二是班级的动态特征,如学生吃饭,上课,睡觉等,我们称为“行为”.如果想要在外部控制班级学生的动作的话,可以从外界给班级发一个信号,比如打铃就是告诉学生该上课了,

php面向对象程序设计

面向对象程序设计(Object Oriented Programming,OOP)是一种计算机编程架构,OOP的一条基本原则是:计算机程序是有单个能够起到子程序作用的单元或对象组合而成的,为了实现郑铁运算,每个对象读能够接收信息.处理数据和向其他对象发送信息.OOP达到了软件工程的三个目标:重用性.灵活性和扩展性,使其变成的代码更加简洁.易于维护,并且具有更强的可重用性.面向对象移至是软件开发领域比较热门的话题,首先,面向对象符合人类看待事物的一般规律.其次,采用面向对象的设计方式可以使系统各部

设计模式李建忠(面向对象程序设计部分)

面向对象 面向对象(Object Oriented,OO)是当前计算机界关心的重点,它是90年代软件开发方法的主流.面向对象的概念和应用已超越了程序设计和软件开发,扩展到很宽的范围.如数据库系统.交互式界面.应用结构.应用平台.分布式系统.网络管理结构.CAD技术.人工智能等领域.       谈到面向对象,这方面的文章非常多.但是,明确地给出对象的定义或说明对象的定义的非常少——至少我现在还没有发现.其初,“面向对象”是专指在程序设计中采用封装.继承.抽象等设计方法.可是,这个定义显然不能再适

JAVA 1.8 理解面向对象程序设计

1. break语句:经常用在循环语句中,用于跳出整个循环,执行循环后面的代码. 2. continue语句:经常用在循环语句中,用于跳出当前的这个循环(或者是跳出本次循环),开始下一次循环的执行. 3. break与continue可以搭配标签使用,在实际开发中,根本没有人会将break与continue搭配标签来使用. 4. 面向对象程序设计(Object Oriented Programming,OOP:Object Oriented Design, OOD).什么是面向对象?在面向对象程

day29-day34 面向对象程序设计

什么是面向对象的程序设计及为什么要有它 面向过程的程序设计的核心是过程(流水线式思维),过程即解决问题的步骤,面向过程的设计就好比精心设计好一条流水线,考虑周全什么时候处理什么东西. 优点是:极大的降低了程序的复杂度 缺点是:一套流水线或者流程就是用来解决一个问题,生产汽水的流水线无法生产汽车,即便是能,也得是大改,改一个组件,牵一发而动全身. 应用场景:一旦完成基本很少改变的场景,著名的例子有Linux內核,git,以及Apache HTTP Server等. 面向对象的程序设计的核心是对象(

第7章 面向对象程序设计

第7章 面向对象程序设计 7.1 面向对象概述 面向对象(Object Oriented)的英文缩写是OO,它是一种设计思想.从20世纪60年代提出面向对象的概念到现在,它已经发展成为一种比较成熟的编辑思想,并且逐步成为目前软件开发领域的主流计技术.如我们经常听说的面向对象编程(Object Oriented Programming,即OOP)就是主要针对大型软件设计而提出的,它可以使软件设计更加灵活,并且能更好地进行代码复用. 面向对象中的对象(Object),通常是指客观世界中存在的对象,具