+今天的内容
n Classes and objects – part I
n Evolution of object model
n Foundations of the object model
n Elements of object model
n abstraction
+
Class & Objects Part I 齐琦
+
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
+
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
+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
+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
+Another example: tic-tac-toe game
+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
+
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
+
n A class can have many arguments
n Also support any number of arguments using a variable argument list, denoted by a trailing *
+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
+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
+Why overloading useful?
n Allow to express “variations on a theme” more clearly than using different method names
+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
+Customized constructor
n Coffee constructor completes n Class body has run
n All initialization occurred
n Result field has the result of all operations
+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
+Brevity / succinct
n For comprehension’s use
+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)
+Example
+Evolution of the Object Model
+
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
+Generations of 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
+
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)
+
Subprograms as an abstraction mechanism
+
Modular structure Most lacked support for data abstraction and strong typing, some errors can only detected during execution of program.
+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)
+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
+Foundations of Object Model
+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
+What’s object-oriented?
n Cardelli and Wegner say:
+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
+Object-oriented analysis(OOA)
OOA
OOD
OOP
OOA serves OOD; OOD as blueprints for implementing system using OOP methods
+Elements of the Object Model
+Programming style
n No single one best for all kinds applications. n Knowledge base
n Computation-intense operation
n Broadest set of applications
+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
+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
+Spectrum of abstraction
n From most to least useful:
+
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.
+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
+
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.