JAVA object-oriented feature Encapsulation

Source: Internet
Author: User

JAVA object-oriented feature Encapsulation
Some information of the class is hidden inside the class, and external programs are not allowed to directly access it. Instead, the class provides methods to operate and access the hidden information. Advantage: 1. data can only be accessed through specified methods. 2. Hide the instance details of the class to facilitate modification and implementation.

1 public class HelloWorld {2     private int a =1;3     public int getA() {4         return a;5     }6     public void setA(int a) {7         this.a = a;8     }9 } 
Encapsulation steps:Modify the visibility of attributes ------ create the getter/setter method in private ------ add attribute control statements to the getter/setter Method for Attribute read/write (evaluate the validity of attributes) Java packagePackage: Manages java files to resolve conflicts with files of the same name. Definition package: package name (it must be placed in the first line of the java source program. The package names can be separated) Access modifier in javaAccess modifier ----- You can modify the access scope of the implementation and method. This keyword of java1 this keyword indicates the current object: this. Attribute operation specifies the attributes of the current object. This. The method calls the method of the current object. 2. this keyword is often used when encapsulating object attributes. Internal classes in javaInnerClass is the class defined in another class. corresponding to the class that contains the internal class, it is called the internal class of the external class. Its main role is: 1. Internal classes provide better encapsulation and can hide internal classes within external classes, other classes in the same package are not allowed to access the methods of Class 2 internal classes to directly access all data of the external classes, functions implemented by internal classes include private data 3 can also be implemented using external classes, but internal classes are more convenient: (1) the most common internal class is the member internal class, also known as the common internal class * The internal class defines the interior of the external class, which is equivalent to the location of a member variable of the external class, internal classes can use arbitrary access controllers, such as public protected private.
1 package com. lx; 2 public class lx1 {3 int a = 1; 4 public void a1 () {5 System. out. println ("I am an external class method! "); 6} 7 public class lx2 {8 int a = this. a; 9} 10}
1 package com.lx;2 import com.lx.lx1.lx2;3 public class lx3 {4     lx1 l1 = new lx1();5     lx2 l2 = new lx2();6 } 
* The methods defined in the internal class can directly access data in the external class without being affected by the access control operator. They can directly access private properties in the external class * After defining the internal class of the member, you must use an external class object to create an internal class object. Instead, you cannot directly create an internal class object, that is, an internal Class Object Name = an external class object. new internal class (); * after the above program is compiled, two results are generated. class file, internal class file. class file and external class. class file member internal class. the class file always looks like this: External class name $ internal class name. class Note: External classes cannot directly use the member variables and methods of internal classes. You can create an internal class object and then access its member variables and methods through the internal class object. If the external class and internal class have the same member variables or methods, the internal class accesses its own member variables or methods by default. If you want to access the member variables or methods of the external class, you can use the this keyword. (2) Static internal classes are Static modified internal classes, which features: * Static internal classes cannot directly access non-Static members of external classes, but it can be accessed by a "new external class ()" member. * If the static member name of the external class is the same as that of the internal class, you can use "class name. static members "Access static members of the external class. If the static members of the external class are different from those of the internal class, you can directly call static members of the external class through" member name. * When creating static internal class objects, you can directly create objects without external classes. Internal class name = new internal class (); (3) internal class method internal class is the internal class defined in the external class, the internal class of the method is only visible inside the method, that is, it can only be used in this method. Note: because the internal class of the method cannot be used outside the external class's method, the internal class of the method cannot use the access controller and Static modifier. (4) Anonymous internal classes: anonymous internal classes, that is, internal classes without names, can only be used once because they have no names, it is usually used to simplify code writing, but there is also a prerequisite for using an anonymous internal class: A parent class must be inherited or an interface instance must be implemented. 1. abstract methods are implemented without using an anonymous internal class.
 1 abstract class Person { 2     public abstract void eat(); 3 } 4  5 class Child extends Person { 6     public void eat() { 7     System.out.println("eat something"); 8     } 9 }10 11 public class Demo {12     public static void main(String[] args) {13     Person p = new Child();14     p.eat();15     }16 }
Running result: As you can see in eat something, we use Child to inherit the Person class, and then implement an instance of Child to transform it to reference the Person class. However, if the Child class is used only once, isn't it very troublesome to compile it as an independent class? In this case, the basic implementation of anonymous internal class 2 is introduced.
 1 abstract class Person { 2     public abstract void eat(); 3 } 4  5 public class Demo { 6     public static void main(String[] args) { 7         Person p = new Person() { 8             public void eat() { 9                 System.out.println("eat something");10             }11         };12         p.eat();13     }14 }
Running result: As you can see in eat something, we can directly implement the methods in the abstract class Person in braces so that we can omit the writing of a class and, the anonymous internal class can also be used on the interface instance 3: using the anonymous internal class on the Interface
 1 interface Person { 2     public void eat(); 3 } 4  5 public class Demo { 6     public static void main(String[] args) { 7         Person p = new Person() { 8             public void eat() { 9                 System.out.println("eat something");10             }11         };12         p.eat();13     }14 }
Running result: As shown in the preceding example, if a class is abstract or an interface, the methods in its subclass can all use anonymous internal classes to achieve the most common situation is the implementation of multithreading, because to implement multithreading, you must inherit the Thread class or inherit the Runnable interface instance 4: anonymous internal class implementation of Thread class
 1 public class Demo { 2     public static void main(String[] args) { 3         Thread t = new Thread() { 4             public void run() { 5                 for (int i = 1; i <= 5; i++) { 6                     System.out.print(i + " "); 7                 } 8             } 9         };10         t.start();11     }12 }
Running result: 1 2 3 4 5 instance 5: anonymous internal class implementation of the Runnable interface
 1 public class Demo { 2     public static void main(String[] args) { 3         Runnable r = new Runnable() { 4             public void run() { 5                 for (int i = 1; i <= 5; i++) { 6                     System.out.print(i + " "); 7                 } 8             } 9         };10         Thread t = new Thread(r);11         t.start();12     }13 }
Running result: 1 2 3 4 5

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.