My Thinking In Java Learning Notes (Nine)

xiaoxiao2021-03-06  77

The fifth chapter hides an important idea of ​​the OOP to achieve an important idea of ​​OOP - let the changed things and the non-changing things are separated from each other. The programmer of the Java library is most important to consider when writing the library, once they change a function in the class or a member variable, so that the programs that have used the previous version of the library will not be influenced. The writer of the library can't know that the programmer calls those functions and variables in the library, so they cannot modify the members of the library, so in order to solve such problems, Java has an access control, and its role is to tell the programmer, those available Those who are not available, and the access control in Java is affected by the package (package), so before learning access controls, you must learn better. Package: The library unit now everyone imagines this situation, if you have developed a Java program locally, now put him into use, when you put the program on the server, maybe you will find the original Some classes are renamed with the Class in the program now! How to solve it? At this time, you need a namespace in Java. It is because there is a namespace that makes the same class of CLASS to survive in their respective relative fields, which don't interfere with each other. When you need to call Class in different namespaces, use the import keyword, for example: call the entire library import Java.util. *; This is called a package, if you want to call a class, such import java.util.math; how to set your own package? Using Package XXX; the front of the program is placed in the XXX package, when using it, as long as IMPORT XXX can be. Keyword Import and Package provide, no matter how many people write Class, no name conflicts will never have! The unique package named package should put all the CLASs that belong to the same package in the same directory, or use JAR. Then now there is 2 questions: how to produce a unique Package name, and how to find a close in the file directory. We generally use the reversal international domain name to ensure the correct solving of the problem 1, my domain name is blog.9cbs.com/maoerzuozuo, so I use Maoerzuozuo.com.9cbs.blog to name the package, so I can generate and others The Non-repetitive Package name (unless he takes my things first).

The second question is solved by the ClassPath in the environment variable, and the classpath contains one or more directories. Each directory looks into the starting point of the Class file, and convert your package name to disk directory for Package Maoerzuozuo.com.9cbs.blog .test; public class demo {public demo () {system.out.println ("Maoerzuozuo.com.9cbs.blog.test.demo");}} call import maoerzuozuo.com.9cbs.blog.test. *; public Class test {public static void main (string args []) {DEMO D = new demo ();}} We store 2 files in f: / maoerzuozuo / COM / 9CBS / Blog / Test / under, then we call him, And set the environment variable classpath = .; f: /; then the program is running normally, the compiler can find the package I call and can run normally. Note: The Class you need to call must be public! Conflict, if I call 2 packs, but there are Test classes in 2 packs. When I instantiate Test classes, you must tell the compiler that you instantiate the class in which package, otherwise it will cause conflicts such as Java .util.date d = new java.util.date (); Customize a library Now we can define your own library, convenient for your use, here we define an output class to facilitate our program. We now write a display class package maoerzuozuo.com.9cbs.blog.test; public class show {public static void go (string s) {system.out.println (s);}} We store him in f: / Maoerzuozuo / COM / 9CBS / BLOG / TEST, and under the environment variable, ClassPath is correct, now use import maoerzuozuo.com.9cbs.blog.test.show; class test {public static void main (String Args " ) {Show.go ("Hello");}}, but please note that when we call Show.go (123);, the go () function has no way to automatically perform INT.Println (); Convert to String Some Advice Use Package When you are using package, you are indirectly specifying the actual system directory structure, so your class must be in this directory, and the system will start from ClassPath, inquiry Go to this directory.

Java Access Permissions Control Words Java, the class, function, member data will have access controls, and they are in turn 4 of the public, private, protected and friends, I remember that I was in the time, my The teacher told me "public is public, private is private money protected is a giant panda" Friendly (friendly) when there is a class, function, member data does not write any access control, his meaning is true In a package, all CLASs can be accessed, or when you do not define any packages, as long as the files in the same directory can also be accessed to each other, the compiler will treat them as a package, but for the package Class shaped with private, can also be called Package Access PUBLIC (public) Key PUBLIC Represents Anyone can access him Private (private room money) Think about the concept of private house, private money is only called, because this The money can only be used by myself. So the representative of Private means that in addition to the Class itself, no one can access him. Private fully embodied encapsulated protected giant panda is protected, and his child is also protected because of the relationship between the giant panda, the meaning of protected is to allow derived classes to access the base class, and base class Other classes in the package are also accessible (interface) AND IMPLEMENTION hidden hidden use of access controls, which can be implemented using access controls, and he can pack data in the class according to different needs, so that People access, establish different access boundaries, usually we need to use access control 1 in 2 cases, tell secondary development programmers, those are available, those are not available, so when we change relevant data, Will not affect other people's original procedures. 2. Separate the interface and implementation. The general client programmer uses the interface of this class, and the specific implementation process does not have to let the programmer know, we can pack him. In this way, we can declare the interface as a public, and the internal implementation process can be declared as protected or privately based on the actual needs.

Class Access Permissions Before placing the keyword, you can declare the Class access rights, you need to pay attention to 1, you can only have a public class in each file to express a single interface, there can be in the file to support public The Friendly class exists; 2, the name of the public class must be the same as the file name of the file, including case in cases; 3, when there is no public class in the file, you can use any name as a file. Name; 4, Class's access control can't be private or protected, can only be Friendly and Public, if you want others unable to generate objects, you can solve the constructor as private, you can answer 1, write A program that produces an ArrayList object, but cannot be explicitly imported into java.util. *; Public class test {public static void main (string args []) {java.util.ArrayList al = new java.util.Arraylist (); }} 6, write a data member and function with public, private, protected, friendly, and generate an object to observe, what is the compile message when you use a member of the class? package test; public class test {public int a; private int b; protected int c; int d; public void methodA () {} private void methodB () {} protected void methodC () {} void methodD () {} public Static void main (string args []) {test t = new test (); ta = 1; tb = 1; // in different / same packets Cannot Access the private members tc = 1; // in different packages CANNOT ACCESS The Protected Members Td = 1; // In Different Packs Cannot Access The Friendly Members T.Methoda (); T.Methodb (); // Different / Same Pack Cannot Access The Private Members T. Methodc (); // CANNOT Access The Protected MEMBERS T.METHODD () in different packages;

// In a different package, Cannot Access The Friendly Members}} 7, write a class, let him have protected, write the second Class in the same file, and operate in the first class of Protected Data Class ProtectedDemo {Protected Int i;} Public Class Test {Public Static Void Main (String Args []) {ProtectedDemo Pd = New ProtectedDemo (); Pd.i = 10; System.out.pr INTLN (PD.I);} This program is actually our "protected in the same package" Friendly "12, Create The Following File in C05 / Local Directory (PRESUMABLY IN YOUR CLASSPATH ): //: c05: local: PackagedClass.java // = M @echo compile by hand to see results package c05.local; class PackagedClass {public PackagedClass () {System.out.println ( "Creating a packaged class") }} ///: ~ THEN CREATE The FOLLOWING FILE IN A Directory Other Than C05: //: c05: Foreign: Foreign.java // = m @echo compile by hand to see results package c05.foreign; import c05. Local. *;

public class Foreign {public static void main (String [] args) {PackagedClass pc = new PackagedClass ();}} ///: ~ Explain why the compiler generates an error Would making the Foreign class part of the c05.local package. CHANGE Anything? Because packagedclass is not public, but Friendly's, it is a package access control, which can only be used in the package, if he put him in the same directory as Foreign, then they are equal to the same package Of course, you can access smoothly. This chapter is mainly to access the control. It is basically not very difficult. Next chapter we explain any problem with the replicate use of Class, you can contact me E-mail molmd@163.net / 163.com QQ: 31349283 We can learn together! Welcome to my blog, http://blog.9cbs.com/maoerzuozuo has more learning content!

转载请注明原文地址:https://www.9cbs.com/read-120559.html

New Post(0)