Студопедия

КАТЕГОРИИ:

АстрономияБиологияГеографияДругие языкиДругоеИнформатикаИсторияКультураЛитератураЛогикаМатематикаМедицинаМеханикаОбразованиеОхрана трудаПедагогикаПолитикаПравоПсихологияРиторикаСоциологияСпортСтроительствоТехнологияФизикаФилософияФинансыХимияЧерчениеЭкологияЭкономикаЭлектроника


OBJECT-ORIENTED PROGRAMMING




The abbreviation “OO”, which stands for object oriented, is used to describe a programming paradigm as well as a variety of computer programming languages.

Objects and classes

The object-oriented paradigm is based on the idea that the solution for a problem can be visualized in terms of objects that interact with each other. In the context of this paradigm, an object is a unit of data that represents an abstract or a real world entity, such as a person, place, or thing. For example, an object can represent a $10.99 small pepperoni pizza. Another one can represent a pizza delivery guy named Jack Flash. Yet another object can be a customer living at 22 Pointe Rd.

The real world contains lots of pizzas, customers, and delivery guys. These objects can be defined in a general way by using classes. Whereas an object is a single instance of an entity, a class is a template for a group of objects with similar characteristics. For example, a Pizza class defines a group of gooey Italian snacks that are made in a variety of sizes, crafted into rectangular or round shapes, and sold for various prices. A class can produce any number of unique objects.

When taking the object-oriented approach to a problem, one of the first steps is to identify the objects that pertain to a solution. As you might expect, the solution to the pizza problem requires some pizza objects. Certain characteristics of pizzas provide information necessary to solve the problem. This information – the price, size, and shape of a pizza – provides the structure for the Pizza class. A class is defined by attributes and methods. A class attribute defines the characteristics of a set of objects.

Each class attribute typically has a name, scope and data type. One class attribute of the Pizza class might be named “pizzaPrice”. Its scope can be defined as public or private. A public attribute is available for use by any routine in the program. A private attributecan be accessed only from the routine in which it is defined. The pizzaPrice attribute’s data type can be defined as “double”, which means that it can be any decimal number. OO programmers often use UML (Unified Modeling Language) diagrams to plan the classes for a program. Although a programmer completes the overall program plan before coding, jump ahead to take a quick look at the Java code for the attributes in the Pizza class. The first line of code defines the name of the class. Each subsequent line defines the scope, data type, and name of an attribute. The curly brackets simply define the start and end of the class.

Class Pizza

{

public string pizzaShape;

public double pizzaPrice;

public double pizzaSize;

}

Inheritance

The object-oriented paradigm endows classes with quite a bit of flexibility. For the pizza program, objects and classes make it easy to compare round pizzas to rectangular pizzas rather than just to square pizzas.

Suppose you want to compare a 10-inch round pizza to a rectangular pizza that has a length of 11 inches and a width of 8 inches. The Pizza class holds only one measurement for each pizza—pizzaSize. This single attribute won't work for rectangular pizzas, which might have a different length and width. Should you modify the class definition to add attributes for pizzaLength and pizzaWidth? No, because these attributes are necessary only for rectangular pizzas, not for round pizzas. An OO feature called “inheritance” provides flexibility to deal with objects’ unique characteristics.

In object-oriented jargon, inheritance refers to passing certain characteristics from one class to other classes. For example, to solve the pizza problem, a programmer might decide to add a RoundPizza class and a RectanglePizza class. These two new classes can inherit attributes from the Pizza class, such as pizzaShape and pizzaPrice. You can then add specialized characteristics to the new classes. The RectanglePizza class can have attributes for length and width, and the RoundPizza class can have an attribute for diameter.

The process of producing new classes with inherited attributes creates a superclass and subclasses. A superclass, such as Pizza, is any class from which attributes can be inherited. A subclass (or “derived class”), such as RoundPizza or RectanglePizza, is any class that inherits attributes from a superclass. The set of superclasses and subclasses that are related to each other is referred to as a class hierarchy. Java uses the “extends” command to link a subclass to a superclass. The statement class RectanglePizza extends Pizza means “create a class called RectanglePizza that’s derived from the superclass called Pizza”.

class RectanglePizza extends Pizza

{

double pizzaLength;

double pizzaWidth;

}

Methods and messages

An OO program can use objects in a variety of ways. A basic way to use objects is to manipulate them with methods. A methodis a segment of code that defines an action. The names of methods usually end in a set of parentheses, such as compare() or getArea().

A method can perform a variety of tasks, such as collecting input, performing calculations, making comparisons, executing decisions, and producing output. For example, the pizza program can use a method named compare () to compare the square-inch prices of two pizzas and display a message indicating the best pizza.

A method begins with a line that names the method and can include a description of its scope and data type. The scope—public or private—specifies which parts of the program can access the method. The data type specifies the kind of data, if any, that the method produces. The initial line of code is followed by one or more lines that specify the calculation, comparison, or routine that the method performs.

A method is activated by a message, which is included as a line of program code, sometimes referred to as a “call”. In the object-oriented world, objects often interact to solve a problem by sending and receiving messages. For example, a pizza object might receive a message asking for the pizza’s area or price per square inch.

Polymorphism, sometimes called “overloading”, is the ability to redefine a method in a subclass. It allows programmers to create a single, generic name for a procedure that behaves in unique ways for different classes. Polymorphism provides OO programs with easy extensibility and can help simplify program code.

 

Comprehension check. Choose the ending for each sentence out of the two or three given.

1. The statement “class RectanglePizza extends Pizza” means

a) create a class called Pizza that is derived from the superclass called RectanglePizza.

b) create a class called RectanglePizza that is derived from the superclass called Pizza.

2) Inheritance refers to passing certain characteristics from one

a) method to another method.

b) class to other classes.

c) class to a superclass.

3) A public attribute

a) can be accessed only from the routine in which it is defined.

b) is available for use by any routine in the program.

c) can't be created in a class which contains private attributes.

4) A class attribute

a) defines the characteristics of a set of objects.

b) is available for use by any routine in the program only if it is private.

5) The process of producing new classes with inherited attributes creates

a) only a superclass. b) a superclass and subclasses.

6) In the object-oriented world, objects

a) don't interact.

b) often interact to solve a problem by sending and receiving messages.

 


Поделиться:

Дата добавления: 2015-09-13; просмотров: 113; Мы поможем в написании вашей работы!; Нарушение авторских прав





lektsii.com - Лекции.Ком - 2014-2024 год. (0.007 сек.) Все материалы представленные на сайте исключительно с целью ознакомления читателями и не преследуют коммерческих целей или нарушение авторских прав
Главная страница Случайная страница Контакты