Oracle DBA Forum  

Вернуться   Oracle DBA Forum > Oracle University Official Study Notes (ENG) > Oracle 10g Java Programming > Oracle 10g Java Programming

Ответ
 
Опции темы Опции просмотра
  #1  
Старый 14.01.2010, 01:40
Marley Marley вне форума
Senior Member
 
Регистрация: 19.09.2009
Сообщений: 7,385
По умолчанию 07 Creating Classes and Objects

__________________
Чат форума (требуется аккаунт на github или twitter)
Ответить с цитированием
  #2  
Старый 14.01.2010, 01:40
Marley Marley вне форума
Senior Member
 
Регистрация: 19.09.2009
Сообщений: 7,385
По умолчанию Objectives



Lesson Aim

This lesson shows you how to create a new class definition using Oracle JDeveloper. You define instance variables and methods for the class. The lesson also shows how to create objects using new, basic object initialization and how to manipulate the objects using instance methods.

Class Definitions

Java is an object-oriented programming language. All the data or attributes and behavior of objects are defined within classes. You use classes for everything you do in Java.

Code

Object orientation is about modeling real-world objects or concepts in software. Java forces you to think in an object-oriented way, because every piece of Java code must live in a class definition. There are no global functions defined outside the scope of a class.

Data

Java uses variables to store information about an object. The variables can store primitive values or references to other objects. Variables also are defined only within classes.

Packages

Packages group logically related classes by application and provide access control. Java uses packages to control which classes can be seen and accessed by classes outside of the package.
__________________
Чат форума (требуется аккаунт на github или twitter)

Последний раз редактировалось Marley; 01.06.2010 в 18:35.
Ответить с цитированием
  #3  
Старый 14.01.2010, 01:40
Marley Marley вне форума
Senior Member
 
Регистрация: 19.09.2009
Сообщений: 7,385
По умолчанию Using Java Classes



What Is a Class?

A class is a template or blueprint that is used in creating multiple objects. A class encapsulates all the data and behaviors that make up an object. When you ask Java to create or instantiate a class, Java uses the class definition as a template for building the new object.

A class contains attributes that are assigned to all new objects of that class. Attributes are the information or data that describe, classify, categorize, or quantify an object. Each object of a given class has its own set of the class attributes. For example, an object of the Customer class may have a name, a billing address, and a telephone number attribute.

Data that is stored in an object can be primitive data types, such as integers or characters, or references to objects.

A class also contains methods, or functions, that specify the behavior or actions that an object can perform. For example, a customer may rent a video, make a payment, or change his or her billing address.

Java uses packages to group classes that are logically related. Packages consist of all the classes in a subdirectory. They are also used to control access from programs outside of the package.
__________________
Чат форума (требуется аккаунт на github или twitter)

Последний раз редактировалось Marley; 01.06.2010 в 18:37.
Ответить с цитированием
  #4  
Старый 14.01.2010, 01:41
Marley Marley вне форума
Senior Member
 
Регистрация: 19.09.2009
Сообщений: 7,385
По умолчанию Comparing Classes and Objects



Classes and Objects

A class is a template for building objects of that class. In object-oriented terminology, an object is an instance of a class. Each object of that class has the same data structure and operations. However, the values that are held in this data structure are unique to each object; these values are therefore called instance variables. The operations of an object act on the instance variables in that object.

The class contains variables and methods called members. Those members owned by the class are static members, and those owned by the objects from the class are called instance members.

In the case of the Movie class, each individual movie is an instance of Movie. Gone with the Wind is one distinct instance of Movie while Last Action Hero is another. Each has its own set of variables that are separate and distinct from the variables of any other movie, or object, of the Movie class.

Each new object is identified within Java by a unique object reference. Java distinguishes between objects by using this reference. Objects are uniquely identifiable even if all their properties are the same.
__________________
Чат форума (требуется аккаунт на github или twitter)

Последний раз редактировалось Marley; 01.06.2010 в 18:38.
Ответить с цитированием
  #5  
Старый 14.01.2010, 01:41
Marley Marley вне форума
Senior Member
 
Регистрация: 19.09.2009
Сообщений: 7,385
По умолчанию Creating Objects




Creating New Objects


In Java, you can create objects by using the new operator. The new operator creates an instance of a class and returns the reference of the new object. For example:

Код:
Movie mov1 = new Movie();
This statement creates an instance variable of the Movie type named mov1. It then creates a new instance of Movie by using the new operator and assigns the object reference to the mov1 instance variable. It is important to remember that the new operator returns a reference to the new object that points to the location of that object in memory.
__________________
Чат форума (требуется аккаунт на github или twitter)

Последний раз редактировалось Marley; 01.06.2010 в 18:40.
Ответить с цитированием
  #6  
Старый 14.01.2010, 01:41
Marley Marley вне форума
Senior Member
 
Регистрация: 19.09.2009
Сообщений: 7,385
По умолчанию Using the new Operator




Anatomy of the new Operator

The new operator performs the following three tasks:
  • Allocating memory for the new object. It knows how much memory is required by looking in the class definition to see what instance variables are defined in the class.
  • Calling a constructor to initialize the instance variables in the new object. A constructor is a special method that is supplied by the class.
  • Returning a reference to the newly created object. To refer to this object in the future, you must store this reference in a variable.

Separating Variable Declaration from Object Creation

The declaration of an object reference and the creation of an object are completely independent. In the previous examples, these two parts were combined in a single statement:

PHP код:
  Movie mov1 = new Movie(); 
However, you can achieve the same effect with two separate statements, as follows:

PHP код:
  Movie mov1;         // Declare an object reference,
                      // capable of referring to a Movie.  
  
mov1 = new Movie(); // Create Movie object, and return the
                      // reference to the mov1 variable. 
__________________
Чат форума (требуется аккаунт на github или twitter)

Последний раз редактировалось Marley; 23.08.2010 в 07:51.
Ответить с цитированием
  #7  
Старый 14.01.2010, 01:41
Marley Marley вне форума
Senior Member
 
Регистрация: 19.09.2009
Сообщений: 7,385
По умолчанию Comparing Primitives and Objects




Primitive Variables and Object Variables

Primitive variables are treated very differently from object variables in Java. It is important to understand what are the differences.

Primitive Variables

When you declare a primitive variable, Java allocates a chunk of memory to hold a variable of the specified primitive type. If you define a primitive variable as an instance or a class variable, then the variable is initialized to 0 if it is a number, false if it is a Boolean, or \0 if it is a char.

Primitive variables hold their values directlyfor example, if you declare an int variable and assign it the value 3, then the value is stored directly in the four bytes of the int.

Object Variables

When you declare an object variable, you also receive a chunk of memory, but this memory is only large enough to hold a reference to an object. You may find it useful to think of a reference as a pointer to an object.

As mentioned previously, declaring an object variable does not create an object of the specified class. Consequently, an object instance variable is initialized to null to indicate that it does not yet refer to any object. Use the assignment operator to make an object variable refer to an object. The assignment can be to an existing object or a new object by using the new operator.
__________________
Чат форума (требуется аккаунт на github или twitter)

Последний раз редактировалось Marley; 23.08.2010 в 07:53.
Ответить с цитированием
  #8  
Старый 14.01.2010, 01:41
Marley Marley вне форума
Senior Member
 
Регистрация: 19.09.2009
Сообщений: 7,385
По умолчанию Using the null Reference




More About null

Consider the following statement:

PHP код:
  Movie mov1 null
This declares a variable called mov1 with a special reference called null. This indicates that the reference does not refer to a real object yet.

Checking Object References Against null

null is a keyword in the Java language. You can use it with the equality operator to check whether an object reference has been initialized or not:

PHP код:
  if (mov1 == null) {
    
// The mov1 variable has not been initialized, 
    // so do something to initialize it  
  


Discarding an Object

When you have finished using an object, you can set its object reference to null. This indicates that the variable no longer refers to the object. When there are no more live references to an object, the object will be marked for garbage collection.

The Java Virtual Machine (JVM) automatically decrements the number of active references to an object whenever an object is dereferenced, goes out of scope, or the stored reference is replaced by another reference.
__________________
Чат форума (требуется аккаунт на github или twitter)

Последний раз редактировалось Marley; 23.08.2010 в 08:08.
Ответить с цитированием
  #9  
Старый 14.01.2010, 01:41
Marley Marley вне форума
Senior Member
 
Регистрация: 19.09.2009
Сообщений: 7,385
По умолчанию Assigning References



Different Object References Can Refer to the Same Object

As previously mentioned, when you declare an object reference variable, it is initialized to null. Before you can use that variable, you must initialize it; typically, you initialize it with a reference to a new object by using the following syntax:

PHP код:
  Movie mov1 = new Movie(); 
However, it is also possible to initialize an object reference to an existing object as follows:

PHP код:
  Movie mov2 mov1
This is perfectly legal Java syntax, but there is still only one Movie object. When you assign one object reference to another object reference, you end up with two references to the same object, not a copy of the other object.


The Object Can Be Accessed Through Either Reference

You can access the same object through either reference; however, there is still only one object.

You can change either reference to refer to a different object without affecting the other reference. However, if you really need a separate object, rather than multiple references to a single object, then you must create a new object.
__________________
Чат форума (требуется аккаунт на github или twitter)

Последний раз редактировалось Marley; 23.08.2010 в 08:10.
Ответить с цитированием
  #10  
Старый 14.01.2010, 01:41
Marley Marley вне форума
Senior Member
 
Регистрация: 19.09.2009
Сообщений: 7,385
По умолчанию Declaring Instance Variables



Declaring Instance Variables

Instance variables must be declared within the class definition. In this example, these instance variables have been declared as public to allow users of the class to access them directly. Although this is not a good practice, it is done here to keep the example simple.

Declaring instance variables as public is a violation of encapsulation; after you have seen how to write instance methods, the instance variables will be made private to prevent them from being manipulated directly by users of the class.

What Does an Object Look Like?

The example creates two instances of the Movie class. To put it in another way, two Movie objects have been created: the first Movie object is referenced by mov1, and the second Movie object is referenced by mov2.

Although these two Movie objects are completely independent, they have an identical data structure. In other words, each object has its own separate and distinct copies of the instance variables that you declared in the Movie class.
__________________
Чат форума (требуется аккаунт на github или twitter)

Последний раз редактировалось Marley; 23.08.2010 в 08:20.
Ответить с цитированием
Ответ
Опции темы
Опции просмотра

Ваши права в разделе
Вы не можете создавать новые темы
Вы не можете отвечать в темах
Вы не можете прикреплять вложения
Вы не можете редактировать свои сообщения

BB коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.

Быстрый переход


Текущее время: 06:57. Часовой пояс GMT +3.


Powered by vBulletin®