今天的内容 - 齐琦 qi qi · object-oriented programming(oop)! object orientation cope with...

41
+ 今天的内容 Classes and objects – part I Evolution of object model Foundations of the object model Elements of object model abstraction

Upload: others

Post on 20-Jan-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+今天的内容

n  Classes and objects – part I

n  Evolution of object model

n  Foundations of the object model

n  Elements of object model

n  abstraction

Page 2: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+

Class & Objects Part I 齐琦

Page 3: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+

n  Scala: hybrid object-functional language; support both object-oriented & functional programming paradigms

n  Fields n  Vals, vars to store data

n  Methods n  Operations to perform

n  Creating an object/ creating an instance of an object n  Create a val or var of a class

Page 4: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+

n  Container or collection n  Especially useful type of object

n  An object that holds other objects

n  E.g. create a Vector holding Doubles

n  V1.reverse; v1.sorted; v1.max; v1.min

Page 5: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+Creating Classes

n  A class definition includes: n  Class keyword

n  A name for the class

n  An optional body; it contains:

n  Field definitions (vals and vars)

n  Method definitions

n  Code that executed during creation of each object

n  Use new keyword to create an instance of a class

n  Fields can be any type

Page 6: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+Class with methods

n  Methods have access to members of the class: current, scale

n  The val for temp: it prevents reference temp from being reassigned to a new object; not restrict the behavior of the object itself

Page 7: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+Another example: tic-tac-toe game

Page 8: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+Class arguments

n  When create new object, pass information into that object to initialize it

n  Like a method argument list, but placed after the class name

n  “new” requires an argument

n  Initialization of “a” happens before we Enter the class body

n  All definitions(values & methods) are Initialized before rest of the body is executed

n  “a” not accessible outside class body n  Want it to visible, declare it as var / val in Argument list

Page 9: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+

n  Class argument with val, cannot be changed outside the class; those with var can

n  ca2 is a val, you can change value of “a”

n  Val defined ca2, ca3, means you can’t point them at other objects; val not control the inside of the object

Page 10: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+

n  A class can have many arguments

n  Also support any number of arguments using a variable argument list, denoted by a trailing *

Page 11: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+Named & Default arguments

n  When create an instance, you can specify the argument names

n  Default values for arguments in class definition n  Only need specify arguments that different from defaults

n  Work with variable argument lists, but the variable argument list must appear last; also variable argument list itself cannot support default arguments

Page 12: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+Overloading

n  Same name(“overload” that name) for different methods as long as argument lists differ

n  Distinguish by comparing signatures n  Signature: comprised of name, argument

list, return type

n  A method signature also includes info about enclosing class n  Overloading1’s f not clash with

overloading2’s f

Page 13: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+Why overloading useful?

n  Allow to express “variations on a theme” more clearly than using different method names

Page 14: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+Constructors

n  The code that “constructs” a new object

n  Combine: n  Class argument list – initialized before entering class body

n  Class body – statements execute from top to bottom

n  Scala already did these: n  Class arguments – initialization and make them accessible to

other objects

Page 15: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+Customized constructor

n  Coffee constructor completes n  Class body has run

n  All initialization occurred

n  Result field has the result of all operations

Page 16: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+Auxiliary constructors

n  Constructor overloading n  Different ways to create objects of same

class n  Define a method called “this”(a keyword)

n  All auxiliary constructors must first call primary constructor, or another auxiliary constructor n  Primary constructor – this( )

n  Can’t use val or var for auxiliary constructor arguments

n  Result of the final expression in a constructor not returned, but ignored

Page 17: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+Brevity / succinct

n  For comprehension’s use

Page 18: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+Companion Objects

n  For those that “this method or field is about the class, but not about a particular object”

n  Object keyword n  Can’t create instances of an object – there’s only one.

n  Collect methods and fields logically belong together

n  Create a companion object for a class

n  Same name as the class

n  Naming convention n  Instance names – lower case first letter

n  Class and object names – capitalize first letter

n  Creating a case class automatically creates a companion object, that containing apply method (factory method)

Page 19: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+Example

Page 20: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+Evolution of the Object Model

Page 21: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+

n  Two sweeping trends

n  Complexity in software system prompted applied research in software engineering n  Decomposition

n  Abstraction

n  Hierarchy

n  Needs more expressive programming languages

Page 22: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+Generations of programming languages

Page 23: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+

n  First-generation n  Primarily for scientific and engineering apps, vocabulary entirely

mathematics; write math formulas, freeing from assembly or machine code.

n  Second-generation n  Machine gets powerful, business application

n  Emphasis on algorithmic abstractions; tell machine what to do

n  Third n  Transistors advent; integrated circuit technology; hardware cost

dropped

n  Demands of data manipulation; Support for data abstraction

Page 24: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+

n  70s n  Thousand of different program languages;

n  Larger programs highlighted inadequacies of earlier languages

n  Few survived; but many concepts introduced adopted by successors

n  Object-oriented (from 80s, 90s) n  Object-oriented decomposition of software

n  Main streams: Java, C++, etc.

n  Emergence of frameworks (e.g. J2EE, .NET)

Page 25: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+

Subprograms as an abstraction mechanism

Page 26: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+

Modular structure Most lacked support for data abstraction and strong typing, some errors can only detected during execution of program.

Page 27: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+For object-oriented

n  Data abstraction important to master complexity of problem.

n  Physical building block is module(a logical collection of classes and objects instead of subprograms)

Page 28: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+For object-oriented

n  Data and operations are united, that fundamental logical building blocks are no longer algorithms, but classes and objects

n  Little or no global data

Page 29: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+Foundations of Object Model

Page 30: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+Object-oriented programming(OOP)

n  Object orientation cope with complexity inherent in many different systems n  Not just to programming languages, user interface design,

databases, computer architectures

n  OOP

n  Uses objects as building blocks

n  Each object is an instance of some class

n  Classes relates to one another via inheritance

Page 31: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+What’s object-oriented?

n  Cardelli and Wegner say:

Page 32: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+Object-oriented design(OOD)

n  Leads to an object-oriented decomposition

n  Uses different notations to express different models of logical (class and object structure), and physical (module and process architecture) design of a system

Page 33: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+Object-oriented analysis(OOA)

OOA

OOD

OOP

OOA serves OOD; OOD as blueprints for implementing system using OOP methods

Page 34: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+Elements of the Object Model

Page 35: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+Programming style

n  No single one best for all kinds applications. n  Knowledge base

n  Computation-intense operation

n  Broadest set of applications

Page 36: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+Elements of object model

n  Conceptual framework for object-oriented, is the object model

n  Four major elements of this model( a model without any one of these is not object-oriented) n  Abstraction n  Encapsulation n  Modularity n  Hierarchy

n  Three minor elements: (useful but not essential) n  Typing n  Concurrency n  Persistence

Page 37: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+Meaning of Abstraction

n  Define abstraction:

n  Focus on outside view of an object, separate object’s essential behavior from its implementation

n  Decide right set of abstractions for a given domain, is central problem in OOD

Page 38: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+Spectrum of abstraction

n  From most to least useful:

Page 39: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+

n  A client is any object that uses resources of another object (known as server). n  Characterize behavior of an object by considering services it

provides to other objects

n  Force to concentrate on outside view of an object, which defines a contract on which other objects may depend, and which must be carried out by inside view

n  Protocol: entire set of operations that contributes to the contract, with legal ordering of their invoking n  Denotes ways that object may act and react, thus constitutes entire

outside view of the abstraction.

n  Terms: operation, method, member function virtually mean same thing.

Page 40: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+Examples of Abstraction

n  Farm, maintaining proper greenhouse environment

n  A key abstraction is about a sensor n  A temperature sensor: an object that measures temperature at a

location

n  What are responsibilities of a temp sensor? Answers yield different design decisions

Page 41: 今天的内容 - 齐琦 Qi QI · Object-oriented programming(OOP)! Object orientation cope with complexity inherent in many different systems ! Not just to programming languages,

+

n  No objects stands alone; every object collaborates with others to achieve some behavior.

n  Design decisions about how they cooperate, define boundaries of each abstraction and the responsibilities and protocol of each object.