Лекции.Орг


Поиск:




Категории:

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

 

 

 

 


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 attribute can 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 method is 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.

 

Vocabulary practice

1. Which word does not belong to the group?

a) class, object, function, method;

b) overloading, inheritance, polymorphism, lambda expression;

c) static, private, protected, public;

d) Smalltalk, Prolog, C++, Java;

e) Boolean, String, Multivalued, Double;

f) overclass, class, subclass, superclass.

 

2. Fill in the missing words choosing from the variants given.

1. … allows programmers to create a single, generic name for a procedure that behaves in unique ways for different classes.

a) inheritance b) paradigm c) polymorphism

2. The OO paradigm defines a(an) … as a unit of data that represents an abstract or real-world entity.

a) method b) object c) class d) attribute

3. Which type of diagrams are often used to plan the OO classes for a program?

a) HTML b) UML c) XML d) Flowchart

4. Which of the following items is not a typical feature of an object-oriented language?

a) polymorphism b) inheritance c) classes d) relationships

5. In the context of OO, a class attribute ….

a) defines the characteristics of a set of objects

b) defines the behavior of an object

c) is used to determine if an object exists

d) is another name for an object

6. The set of superclasses and subclasses related to each other is referred to as ….

a) a class hierarchy b) a set of independent classes

c) a set of classes with a common parent

 

3. Transform the following sentences without any change in meaning. Use the prompts as they are given (words in brackets, parts of sentences).

1. For the pizza program, objects and classes make it easy to compare round pizzas to rectangular pizzas rather than just to square pizzas (comparison).

2. A method begins with a line that names the method and can indicate a description of its scope and data type (describe).

3. The abbreviation “OO” is used to describe a programming paradigm as well as a variety of computer programming languages (description).

4. In the object-oriented world, objects interact to solve a problem by sending and receiving messages (interaction).

5. When taking the object-oriented approach to a problem, one of the first steps is to identify the objects that pertain to a solution (identification).

6. A basic way to use objects is to manipulate them with methods (manipulation).

 

4. Fill in the gaps in the text.

The object-oriented paradigm is based on the idea that the solution to a problem can be visualized in terms of objects that _ 1 _ with each other. An object is a single instance of an entity. Programmers can use a _ 2 _ as a template for a group of objects with similar characteristics. Classes can be derived from other classes through a process called _ 3_. The set of superclasses and subclasses that are related to each other is referred to as a class _4 _. OO programmers often use _ 5 _ Modeling Language diagrams to plan the classes for a program. Objects interact to solve problems by exchanging _ 6 _ which initiate an action, process, or procedure. OO programmers can create _ 7 _ to define what happens once an action is initiated.

 

Speaking. Discuss the following questions.

 

1. What is the basic focus of the object-oriented paradigm?

2. What’s the difference between an object and a class?

3. How do I define the classes I need to solve a problem?

4. How do I code a class when writing a program?

5. How flexible are classes for defining different types of objects?

6. What is inheritance?

7. How do I code a subclass?

8. How does an OO program use objects?

9. What can a method do?

10. What does a method look like when it has been coded in Java?

 

Text D

Reading. Read the text and try to guess the meaning of the words in bold. Check your variants in the dictionary.

 





Поделиться с друзьями:


Дата добавления: 2015-10-27; Мы поможем в написании ваших работ!; просмотров: 617 | Нарушение авторских прав


Поиск на сайте:

Лучшие изречения:

Есть только один способ избежать критики: ничего не делайте, ничего не говорите и будьте никем. © Аристотель
==> читать все изречения...

2220 - | 2173 -


© 2015-2024 lektsii.org - Контакты - Последнее добавление

Ген: 0.01 с.