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

The return Statement

Procedural programming | Access labels | Classification of data types in the programming language Java. | Floating-point numbers | Generics in the programming language Java. | Continue Statement | Declaring Pointers |


Читайте также:
  1. A brief survey of control statements.
  2. A Decide which of these statements are true (T) or false (F).
  3. A. Decide whether each of the following statements is true or false. 1 страница
  4. A. Decide whether each of the following statements is true or false. 2 страница
  5. A. Decide whether each of the following statements is true or false. 3 страница
  6. A. Decide whether each of the following statements is true or false. 4 страница
  7. ACCOUNTING AND FINANCIAL STATEMENTS

The last of Java's branching statements is the return statement.we use return to exit from the current method. The flow of control returns to the statement that follows the original method call. The return statement has two forms: one that returns a value and one that doesn't. To return a value, simply put the value (or an expression that calculates the value) after the return keyword: return ++count; The data type of the value returned by return must match the type of the method's declared return value. When a method is declared void, use the form of return that doesn't return a value: return;

9. Declaration of classes in the programming language Java. Classes A class is the template or blueprint from which objects are made. Thinking about classesas cookie cutters. Objects are the cookies themselves. When we construct an object froma class, we are said to have created an instance of the class. When we extend an existing class, the new class has all the properties and methods of the class that you extend. You supply new methods and data fields that apply to your new class only. The concept of extending a class to obtain another class is called inheritance. class declarations define new reference types and describe how they are implemented. A named class may be declared abstract and must be declared abstract if it is incompletely implemented; such a class cannot be instantiated, but can be extended by subclasses. A class may be declared final in

which case it cannot have subclasses. If a class is declared public, then it can be

referred to from other packages. Each class except Object is an extension of a single existing class and may implement interfaces. The body of a class declares members instance and static initializes, and constructors. The scope of a member is the entire declaration of the class to which the member belongs. Field, method, member class, member interface, and constructor declarations may include the access modifiers public, protected, or private. The members of a class include both declared and inherited members. Newly declared fields can hide fields declared in a superclass or superinterface. Newly declared class members and interface members can hide class or interface members declared in a superclass or superinterface. Newly declared methods can hide, implement, or override methods declared in a superclass or superinterface. Class Declaration CLASSES Field declarations describe class variables, which are incarnated once,

and instance variables, which are freshly incarnated for each instance of the class.

A field may be declared final in which case it can be assigned to only

once. Any field declaration may include an initializer. Member class declarations describe nested classes that are members of the surrounding class. Member classes may be static, in which case they have no access to the instance variables of the surrounding class; or they may be inner classes. Member interface declarations describe nested interfaces that are members of the surrounding class.Method declarations describe code that may be invoked by method invocation expressions. A class method is invoked relative to the class type; an instance method is invoked with respect to some particular object that is an instance of the class type. A method whose declaration does not indicate how it is implemented must be declared abstract. A method may be declared final in which case it cannot be hidden or overridden. A method may be implemented by platform-dependent native code. A synchronized method automatically locks an object before executing its body and automatically unlocks the object on return, as if by use of a synchronized statement thus allowing its activities

 

10. Constructors and destructors in the programming language C++. Classes can have complicated internal structures, so object initialization and clean-up of a class is much more complicated then for any other data structures. Constructors and destructors are special member functions of classes that are used to construct and destroy class objects. The construction can be for example: initialization for objects or memory allocation. The destruction may involve de-allocation of memory or other clean-up for objects.Constructors and destructor’s are declared within a class declaration (as like any other member function). A constructor or a destructor can be defined in-line or external to the class declaration. We may declare some default arguments when we make a constructor. There are some restrictions that apply to constructors and destructors:

§ Constructors and destructors cannot have a return type (not even void).

§ Pointers and references cannot be used on constructors and destructor’s (It is not possible to get there address)

§ Constructors and destructor’s cannot be declared static, const or volatile.

§ Constructors cannot be declared with the keyword virtual.

Constructors are called automatically by the compiler when defining class objects. The destructor’s are called when a class object goes out of scope.

CAdd(int two) { cout << "A constructor is called" << endl; one=two; }

declares the constructor of the class CAdd. The name of the constructor is the same as the name of the class

CAdd() { cout << "A default constructor is called " << endl; }

declares a default constructor. the compiler will call the constructor whose parameters match the arguments used in the function call

~CAdd() { cout << "Destructing " << one << endl; }

 

declares a destructor to deallocate the objects.At the end of the program objects are deallocated in the reverse order in which the constructors are called.

 


Дата добавления: 2015-11-16; просмотров: 68 | Нарушение авторских прав


<== предыдущая страница | следующая страница ==>
Object oriented programming| Creation and using one-dimensional and multi-dimensional arrays in the programming language Java.

mybiblioteka.su - 2015-2024 год. (0.007 сек.)