Chapter 2 Encapsulation
Object-oriented features: encapsulation, inheritance, and Polymorphism 2-1 What is Java encapsulation?★ConceptHiding some information of a class inside the class, Does not allow external programs to access directly, but usesMethods provided by this classTo achieveOperation and access to hidden information.★BenefitsA. Only the specifiedMethodAccess Data B.HideClass instanceDetailsEasy to modify and implement★Encapsulation implementation steps 2-2 Use packages to manage classes in Java★Package FunctionsA. Managing Java files B. Resolving file conflicts with the same name★Definition package: package nameNote: The first line of package names that must be placed in the Java source program can be separated by "." (hierarchical relationship) eg: com. immoc. MyClass For example: music class-MyClassMusic-musiccom.imooc.music.MyClassMusic-moviecom.imooc.movie.MyClassMusic★Packages in the systemJava. (function). (class)Eg: java. lang. (class) contains the java language-based class java. util. (class) contains various tool classes in the java language java. io. (class) class that includes Input and Output Functions★Use of packagesA. You can useImport keywordUse the class import com. imooc. music. MyClassb. Java in a file,The package naming convention is full lowercase letter spellingC. Not only can I load all files under a certain package, such as com. imooc .*"*" Indicates all files.You can also load all files under a specific sub-package, such as com. imooc. music .*2-3 Access modifiers in Java Access modifier -- you can modify the access range of attributes and methods. ★Summary of access Modifiers Visual analogy Series Figure: class attributes and methods-money of a person Figure: private-locked in the safe and can only be used by the user Figure: default -- available to users under the same eaves Figure: protected-I can also use Figure: public -- available to all users 2-4 this keyword in Java ★This keyword indicates the current objectThis. Attribute operation attribute of the current object this. method call method of the current object★This keyword is often used when encapsulating object attributes. Eclipse provides the ability to automatically generate getter and setter. steps: Step 1: Click Source and select Generate Getters and Setters. Step 2: select the method to be generated and click OK. : This keyword avoids confusion caused by variable names. 2-5 What is an internal class in Java? An Inner Class is a Class defined in another Class.. The class that contains the internal class is calledExternal class.★Main functions of internal classesA. Internal classes provide better encapsulation and can hide internal classes within external classes,Other classes in the same package are not allowed to access this class.B.Internal class methods can directly access all data of the external class, including private dataC. functions implemented by internal classes can also be implemented using external classes, but sometimes internal classes are more convenient.★Types of internal classesMember internal class static internal class method internal class anonymous internal classFigure: Example2-6 internal Member classes in Java The most common internal class is the member internal class, also known as the common internal class.Let's look at the following code: the running result is:From the code above, we can see how to use the internal class of the member:①The Inner class is defined inside the Outer class, which is equivalent toMember variablesLocationThe Inner class can use any access controller.Such as public, protected, and private.②TheTest ()The method can directly access data in the Outer class without being affected by the access control operator.For example, you can directly access the private attribute a in the Outer class.③After the internal class of the member is defined,You must use an external class object to create an internal class object.Instead of directly going to a new internal class object, that is:Internal Class Object Name = External Class Object. new internal class ();④After the above program is compiled, two results are generated. in the class file, the second is the external class. class file, the first is internal class. class file, that is,. the class file always looks like this: External class name $ internal class name. classIn addition,① External classes cannot directly use internal class members and methods. You can first create an internal class object, and then access its member variables and methods through the internal class object. Figure: Revised Version ② 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 of an external class, you can use the this keyword. For example: Running result:My further exploration: Exploration 1:The compiler reports the following error: Explore 2:Running result:2-7 static internal classes in Java Static internal classes are static modified internal classes,This internal class features:①Static internal classes cannot directly access non-static members of external classes.But it can be accessed through the new external class (). Member method.②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 the static members of the external class using the "member name ".③When creating static internal class objects, you can directly createInternal Class Object Name = new internal class ();Running result:My further exploration:Running result:2-8 Internal classes of methods in Java The internal class of the method is the internal class defined in the external class method.,The internal class of the method is only visible within the method, that is, it can only be used within the method.
Be sure to note:Because the internal class of the method cannot be used outside the external classThe internal class of the method cannot use the access controller or static modifier..