220 likes | 445 Vues
In Alice, we have seen that we have: Objects (skater, snowman, snowwoman, camera) Methods (move, turn, roll, move toward) Properties (color, opacity) In addition, although not as obvious, Alice has classes. What About Alice?.
E N D
In Alice, we have seen that we have: Objects (skater, snowman, snowwoman, camera) Methods (move, turn, roll, move toward) Properties (color, opacity) In addition, although not as obvious, Alice has classes. What About Alice?
Snowman is both a class and an object. As a class, it refers to a generic snowman. The class allows us to create many snowmen (snowman, snowman1, snowman2, etc.). Each snowman may be customized, e.g., the hats of the snowwomen were different colors, and the snowwomen were in different locations. Changing the value of a property of one snowwoman, doesn't change the value for another snowwoman. Classes and Objects
The snowman class says what methods and property a snowman can have. It is a blueprint for making actual snowmen. Classes (cont'd)
Objects, in Alice, are created by dragging and dropping the prototype from the Gallery, or from copying an existing object, using the copy button. Although two objects can be made from the same blueprint, they can be customized differently. Objects
The class specifies what properties its objects can have. Each object has its own set of values. For example, color is a property of all snowwomen, but each snowwoman may have a different color. Properties
The class also specifies the methods. We can add new methods, modify existing methods, and call methods from other methods. One object can invoke the method of another object. Methods
In Java, a class is a blueprint for objects. A class describes the fields (properties) and methods (methods, functions) that the objects have. Each object has its own values for each field. Fields are also called instance variables. Fields and methods together are called members of the class, and are sometimes called components of the class. Java Classes
A class is created by a class definition. The form is: class Snowman { <field1> <field2> ... <method1> <method2> ... } Class Definition
In Java, classes are types. We can create objects of that type and give them names, just as we can create ints and give them names. We can name a object using a variable. The statement: Snowman snowman1; declares the variable snowman1 to be of type Snowman, but it does not create a Snowman object! Classes as Types
To create an object, we must use the new operator. The operator new creates and returns a new object of the given class. The statement: Snowman snowman1 = new Snowman(); declares snowman1 to be an object of type Snowman, creates a new Snowman object, and initializes snowman1 to have this new object as its value. Creating Objects
We've seen this statement before: Scanner sc = new Scanner(System.in); declared sc to be a variable of type Scanner, created a new Scanner object (based on reading in from the keyboard) and initialized sc to be this new object. Example
An object of a class is called an instance of that class, and creating the object is called instantiation. Instance
This might be the start of a Snowman class: public class Snowman { private Color color; private int opacity; private double xPosition; private double yPosition; public void move() { ... } public void turn() { ... } } Example Snowman
“ A class is basically a structure with member functions as well as member data. Classes are central to the programming methodology known as object-oriented programming.” Classes
In procedural programming, we break down a program in terms of functions. Top-down design principles start with the problem at hand and finds a solution by breaking down the problem into steps that can be used to solve it. We begin with the major steps, and then break those down into smaller steps. This process is called stepwise-refinement. Procedural Programming
The result is a program in which functions play the major role. Data is merely passed to and from functions. Data may be structured but is secondary to the flow of control of the program. This is the procedural programming paradigm. Result
In object-oriented design, instead of thinking in terms of functions, we think in terms of objects. Objects are collections of related data along with the functions that apply to that data. Objects occur naturally in many problems, and so by thinking about what objects are needed to solve a problem, we often can create better solutions. Object-Oriented Design
Let's take a simple example: A clock. What is the data associated with a clock? What operations do we normally perform on that data? OOD - Example
A second example is a bank account. Let's take a simple form of a bank account – a checking account with interest. What is the data? balance, APR What are the operations? deposit, withdrawal, check balance, modify APR, check APR OOD – Another Example
Write a class declaration for a bank account class. A bank account should have a balance and an APR. In your main method, create three bank accounts. Exercise
In object-oriented design the problem is decomposed into different objects that are inherent in the problem. The program relies on the interaction between the objects. The objects can call each others' methods. This is called message passing. A message is the name (and parameters) of the method to be called. Different types of objects may have methods with the same name, so which method is actually run depends on the object and the message. Object-Oriented Design