in the previous days, one is learning the C # language, using C # in some small projects, today turned to the Java learning, or feel a little unaccustomed, no previous Chinese interface, all English. Writing code has always kept the coding habit in C #, but learning Java is not difficult, today's Java Abstraction and encapsulation and C # have learned very similar, so as long as C # learning good, go to Java is quite easy. Okay, here's the subject.
one. About access rights for class classes.
01.public: Can be accessed by all classes.
In Java, if a class does not have an access modifier, his default modifier can be called friendly, but the Java language does not have a friendly modifier, and the friendly is derived from C + +. In fact, the default access rights for classes in Java are package-level access. That is, if you write a class that does not have a write access modifier, the class is the default package-level access permission, and the class under the same package can access it. If you want to use the class under a different package, you must first import Baoming to access the class.
two. Encapsulation in Java
01. What is encapsulation:
Encapsulation is the privatization of properties that provide public access to private properties.
This is done by modifying the visibility of properties to restrict access to properties, and creating a pair of value (getter) methods and assignment (setter) methods for each property for access to those properties.
02. Why Encapsulation is required:
By encapsulation, data access restrictions on attributes can be achieved, and the maintainability of the program is increased.
Because the value method and the assignment method hide the implemented changes, it does not affect the class that reads or modifies the attribute, avoids the large-scale modification, and the maintainability of the program is enhanced.
03. How to Implement the package:
01. Modify the visibility of the property to restrict access to the property.
02. Create a pair of assignment methods and value methods for each attribute to access the properties.
03. In the assignment and accessor methods, add a restriction on the access to the attribute.
First we can define a class: If you define a dog class. The class has the following properties: The dog's name (name), the dog's Healthy value (health).
Public class Dog { private String name= "anonymous"; nickname privateint health=100; Health value }
To implement the steps for encapsulating these two fields:
01. Press the shortcut key Alt+shift+s will appear the interface shown, check the red box out of the place.
02. Go to the next interface and follow the pattern.
03. Post-Packaged code
PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; }
Three. This keyword:
This is a reference within the object that refers to itself. You can directly reference an object to resolve any conflicts that occur between an instance variable and a local variable with the same name.
Three. Tips: Formatting code
Here is a shortcut key ctrl+shift+f, note (in the absence of input method in the case of use).
Encapsulation in Java