码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-12-15 20:12:49      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:placed   name   line   分享   rod   models   err   nts   public   

       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.

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

标签:placed   name   line   分享   rod   models   err   nts   public   

原文地址:http://www.cnblogs.com/hahaccy/p/8044591.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!