Ii. Java Object-oriented (7) _ Encapsulation idea--understanding encapsulation thought

Source: Internet
Author: User

2018-04-30

Understanding Encapsulation Ideas

Encapsulation is the privatization of properties that provide public access to private properties.

* Steps to implement encapsulation:

(1) Modify the visibility of the property to restrict access to the property.

(2) Create a pair of assignment (set method) methods and a value (Get method) method for each property for access to these properties.

(3) In the assignment and value method, add access restrictions to the attribute.

* * For good encapsulation, we typically declare member variables of a class as private and then Public method to access this variable . The operation of a variable is generally read and Assignment operation, we define two methods to achieve these two operations, one is GetXXX () (Xxx means the name of the member variable to access), to read this member variable operation, and the other is setxxx () Used to assign a value to this member variable.

* * If the outside program can arbitrarily modify the member variables of a class, it will cause unpredictable program errors, like a person's height, can not be arbitrarily modified outside, can only be through a variety of nutrient intake methods to repair change this property.

public class accpteacher{

private String name;

private int age;

Public String GetName () {

return name;

}

public void SetName (String name) {

THIS.name = name;

}

public int getage () {

return name;

}

public void SetName (String age) {

if (age<22) {

System.out.println ("Age does not fit, too small!") ");

}else{

This.age = age;

}

}

}

Benefits of Encapsulation:

(1) The implementation details of the hidden class;

(2) To allow users to access the data only by pre-customized methods, you can easily join the control

Logic, restricting the unreasonable operation of the attribute;

(3) Easy to modify, enhance the maintainability of the Code;

----------------------------------------------------------------------

eg:
Save Contact Features:
void Save (String name,int age,string phonenumber,string email,string Address ...)
because you can save multiple messages when you save a contact, the Save method must be set to have multiple parameters.
problem found: exploded growth of the method's parameter list.
think of the contact information as a whole.
class Linkman
{
String name;
int age;
String Address;
String PhoneNumber;
String Email;
}


the above method of saving the contact person;
void Save (Linkman man) {}

--------------------------------------------------------------------------------------

What is encapsulation (one of the three main object-oriented features):
1. Consider the state of an object (attribute/field) and behavior (function/method) as a unified whole, storing the two in a separate module (a class).
2. " Information hiding ": To hide the information that does not need to be known to the outside world, as far as possible to hide the details of the function of the object, outward exposure method, to ensure the security of the outside world (to hide all the data information, as far as possible to hide many functions, only outward exposure convenient method for call).
---------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------


What do you do with hidden and exposed functions?

High cohesion: The module's internal data, function details are hidden inside the module, Direct external intervention is not allowed.
Low coupling: This module only needs to expose a small number of functional methods to the outside world.

--------------------------------------------------------------------------------------------------------------- -



You can change the above code to reflect the encapsulation:
class Person
{
String name;
private int age;//can only use the age variable in this class.
void Setage (int a)
  {
if (a<0)
    {
System.out.println ("Age cannot be negative");
return;//represents the End method
    }
Age = A;
  }
  
}


//Demo Package
class Persondemo
{
Public static void Main (string[] args)
  {
Person p = new Person ();
p.name = "Baihe";
P.setage (+);
System.out.println (P.name + "," +p.age);
  }
}

Ii. Java Object-oriented (7) _ Encapsulation idea--understanding encapsulation thought

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.