An accessor method of a class accesses and modifies the data members of the class.

Class -- a blueprint for objects. A class is a user-defined type that describes and defines objects of the same type. A class contains a collection of data and method definitions.

An object is a single instance of a class. You can create many objects from the same class type.

Small example: Circle.java

Creating Objects

Objects are created from a class by using the new operator, and they must be attached to a reference variable. Two steps:
  1. Declare the object reference variable
  2. Create the object with the new operator and attach it to the reference variable

Format

  ClassName objectReference;
  objectReference = new ClassName[];

[or combined into one statement]

  ClassName objectReference = new ClassName[];
Examples:
  Circle myCircle;		// where Circle is a class
  myCircle = new Circle[];

  Dog fido = new Dog[];		// where Dog is a class
Caution: Since the name used to refer to an object is a reference variable, and not the object itself, it is important to note that any assignments done on such a variable are just on the reference. For example, if we create two objects, and then assign their variables together:
  Circle c1 = new Circle[];
  Circle c2 = new Circle[];

  c1 = c2;
... the last statement [c1 = c2] does not copy circle c2 into c1. Instead, it copies the reference varaible c2 to c1, which means that both reference variables are now referring to the same object [the second one, c2].

Using Objects

Once an object is created, access the object's internal methods and data with the dot-operator. Format:
  objectReference.data
  objectReference.method[arguments]   // a call to a method
Example:
  Circle c1 = new Circle[];
  c1.radius = 10;		// access radius instance variable

  // compute and print the area with the findArea method
  System.out.print["Area = " + c1.findArea[]];

Protection levels in a class [Visibility Modifiers]

We can declare members of a class to be public or private.
  • public - can be accessed from inside or outside of the object.
  • private - can only be used by the object itself.

The public members of a class make up the interface for an object [i.e. what the outside builder of the object can use]. The user of an object is some other portion of code [other classes, functions, main program].

Although there is no set rule on what is made public and what is made private, the standard practice is to protect the data of a class by making it private. Provide access to the data through public methods, which can maintain control over the state of the object.

Reasons for data hiding:

  • Makes interface simpler for user.
  • Principle of least priveledge [need-to-know]
  • More secure. Less chance for misuse [accidental or malicious].
  • Class implementation easy to change without affecting other modules that use it.

Constructors

A constructor is a special member function of a class whose purpose is usually to initialize the members of an object.

A constructor is easy to recognize because:

  • It has the same name as the class
  • It has no return type
Constructors can have parameters. A constructor without any parameters is known as a default constructor.

A constructor is automatically invoked when an object is created with new.

  c1 = new Circle[];    // invokes default constructor
  c2 = new Circle[9.0]  // invokes a constructor with one parameter
The usual purpose of a constructor is to perform any initializations on the object when it is created [i.e. primarily the instance variables]

Accessors and Mutators

Since it's a good idea to keep internal data of an object private, we often need methods in the class interface to allow the user of objects to modify or access the internally stored data, in a controlled way.

An accessor method is a function that returns a copy of an internal variable or computed value. A common practice is to name these with the word get.

A mutator method is a function that modifies the value of an internal data variable in some way. The simplest form of mutator function is one that sets a variable directly to a new value -- a common practice is to name these with the word set.

Class -- a blueprint for objects. A class is a user-defined type that describes and defines objects of the same type. A class contains a collection of data and method definitions.

An object is a single instance of a class. You can create many objects from the same class type.

Small example: circle.py

Creating Objects

Objects are created from a class by using the name of the class and are then attached to a reference variable. Two steps:
def main[]:
    my_circle = Circle[]        # Create a class instance: object
    area = my_circle.findArea[] # Using the object
Caution: Since the name used to refer to an object is a reference variable, and not the object itself, it is important to note that any assignments done on such a variable are just on the reference. For example, if we create two objects, and then assign their variables together:
c1 = Circle[]  
c2 = Circle[]

c1 = c2;
... the last statement [c1 = c2] does not copy circle c2 into c1. Instead, it copies the reference varaible c2 to c1, which means that both reference variables are now referring to the same object [the second one, c2].

Using Objects

Once an object is created, access the object's internal methods and data with the dot-operator. Format:
objectReference.data
objectReference.method[arguments]   # a call to a method
Example:
c1 = Circle[]
c1.radius = 10	# access radius instance variable

# compute and print the area with the findArea method
print "Area = " + c1.findArea[]

Protection levels in a class [Visibility Modifiers]

We can declare members of a class to be public or private.
  • public - can be accessed from inside or outside of the object:
    class Circle:
       def __init__[self]:
          self.radius = 1.0 # Public by not adding __
  • private - can only be used by the object itself:
    class Circle:
       def __init__[self]:
          self.__radius = 1.0 # Private by adding __

The public members of a class make up the interface for an object [i.e. what the outside builder of the object can use]. The user of an object is some other portion of code [other classes, functions, main program].

Although there is no set rule on what is made public and what is made private, the standard practice is to protect the data of a class by making it private. Provide access to the data through public methods, which can maintain control over the state of the object.

Reasons for data hiding:

  • Makes interface simpler for user.
  • Principle of least privilege [need-to-know]
  • More secure. Less chance for misuse [accidental or malicious].
  • Class implementation easy to change without affecting other modules that use it.

Class methods

Class methods are created in the same way regular functions are created except the indentation of the function ensures it is in the class block. Method functions are designed to operate on a specific objects data. To known which object data to operate each class method declared with the self argument. The self argument is always the first argument and the Python interpreter automatically assigns the calling abject to this argument for you.

class Circle:
 
  def __init__[self]:
      self.radius = 1.0 # The initializer method
      self.__test[]
      
  # methods
  def findArea[self]:  
     return [self.radius * self.radius * math.pi]

  def findCircumference[self]:
     return [self.radius * 2 * math.pi]

Special class method

Python classes has a special method called the initializer method. This method runs every time an instance of a class is created. The initializer method can have parameters. Example:

class Circle:
 
   def __init__[self]:   # The initializer method
      self.radius = 1.0

			

A initializer method is automatically invoked when an object is created.

  c1 = new Circle[]    # invokes initializer method constructor

Often programmers want to display a message that indicates the state of an object. This usually entails the value's of the objects attributes. We could simply use the accessors and mutator methods [explained below] to print out this content. However, it is a common enough occurance where Python provides a dedicated method __str__[]:

class Circle:
   
   def __init__[self, radius, center_x, center_y]:
      # Radius of the circle 
      self.__radius = radius   
      self.__center_x = center_x
      self.__center_y = center_y
       
   def __str__[self]:
      return "Radius: %.2f Coordinates [x, y]: [%.2f, %.2f]" \
         % [self.__radius, self.__center_x, self.__center_y]
      
def main[]:
   circle1 = Circle[2, 4, 5]
   print circle1

main[]
      
c1 = Circle[]  
c2 = Circle[]

c1 = c2;
0

The method is automatically invoked for you when the class is required to return a string.

Accessors and Mutators

Since it's a good idea to keep internal data of an object private, we often need methods in the class interface to allow the user of objects to modify or access the internally stored data, in a controlled way.

An accessor method is a function that returns a copy of an internal variable or computed value. A common practice is to name these with the word get .

A mutator method is a function that modifies the value of an internal data variable in some way. The simplest form of mutator function is one that sets a variable directly to a new value -- a common practice is to name these with the word set .

What is another name for the accessor methods?

Accessor methods, also called get methods or getters, allow a way to get the value of each instance variable from outside of the class.

Which type of method modifies the object on which it is invoked?

A method that does modify the object on which is invoked is called a mutator method.

What is the return type of an accessor method?

An accessor method is a method that is used to return the value of an instance [or static] variable to the user. Since the method is returning a value to the user, the method header should contain the return type in place of the keyword void.

What is the process of hiding object data and providing methods for data access called?

Encapsulation is the process of hiding implementation details and providing methods for data access. Encapsulation allows a programmer to use a class without having to know its implementation. Information hiding makes it simpler for the implementor of a class to locate errors and change implementations.

Chủ Đề