Lesson 7 on JAVA (encapsulation and ideas)

Source: Internet
Author: User

Lesson 7 on JAVA (encapsulation and ideas)


Encaplusation ):

Indicates to hide the attributes and implementation details of an object and only provide public access.

Advantages:
1. Isolate changes
2. ease of use
3. Improve importance
4. Improve Security

Encapsulation principles:


1. Hide content that does not need to be provided externally

2. Hide attributes to provide public access


Example code:

Class man {private int age; // age private String sex; // gender public void change (int a, String B) {if (a <0 | a> 130) {System. out. println ("Error Data"); return;} age = a; sex = B; fun ();} void fun () {System. out. println ("age =" + age + ", sex =" + sex) ;}} public class Main {public static void main (String [] args) {man jo = new man (); // jo. age =-20; in this way, there is no security, so private is used to private the member attributes, so the outside world cannot access jo. change (20, "man ");}}


In the actual development process, a specification has been formed

Writing rules: set (storing data) and get (getting data)

Class man {private int age; // age private String sex; // gender public void setage (int a, String B) // store {if (a <0 | a> 130) {System. out. println ("Error Data"); return;} age = a; sex = B;} public int getage () // get {return age;} void fun () {System. out. println ("age =" + age + ", sex =" + sex) ;}} public class Main {public static void main (String [] args) {man jo = new man (); jo. setage (20, "fa"); int ag = jo. getage (); System. out. println (ag );}}

The above code is the embodiment of simple encapsulation, and the most obvious embodiment is security. Note that attributes are generally hidden.


Encapsulation philosophy:


For example, various electrical appliances are a good encapsulation body. The electrical switch is an external access method. As for how to access the inside, you do not need to know about it. It is hidden from users.

Of course, the same is true for objects.


Private: A permission Modifier with the minimum permission. It can only modify members but not local
Private content is only valid in this class
Public, with the maximum Permissions

Note: private is only an embodiment of encapsulation. private is encapsulation, and encapsulation does not mean private.

Class, framework itself is an encapsulation body


Exercises:


Encapsulated and sorted by 10 inserts.

Code:

Import java. util. export; import javax. naming. ldap. sortControl; class jo {public void set (int a []) // public access mode {intsrtsort (a);} private static void intsrtsort (int a []) // Insert the sorting {int len =. length; for (int I = 1; I
 
  
T; j --) {a [j + 1] = a [j]; swap (a, j + 1, j );} a [j + 1] = t ;}} private static void swap (int a [], int x, int y) // change the value {a [x] = a [y] ;}} public class Main {public static void main (String [] args) {int a [] = new int [10]; cin = new partition (System. in); System. out. println ("input 10 numbers for insertion sorting:"); for (int I = 0; I <10; I ++) {a [I] = cin. nextInt ();} jo haha = new jo (); // defines the object haha. set (a); // store data System. out. println ("sorting result:"); show (a); // display result cin. close ();} static void show (int a []) {for (int I = 0; I <10; I ++) {System. out. print (a [I] + "");}}}
 



Related Article

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.