Thinking in Java Learning Tie (Nine)

xiaoxiao2021-03-06  61

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 the name of your package is called into disk directory.

Package maoerzuozuo.com.9cbs.blog.test; public class demo {public demo () {system.out.println ("maoerzuozuo.com.9cbs.blog.test.demo");}}

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 a conflict, for example

Java.util.date d = new java.util.date ();

Customizing a library Now we can define your own library, which is 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 Under Test, and under the environment variable, ClassPath is taking a good point, 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 use system.out.println (); the INT automatically converts into String

Some advice using 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 and query the 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 any access control in front of class, functions, member data, is Friendly, his meaning is in the same package, all Class can access, or when you don't define any When the package is packaged, 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-exterior Class-shaped with private, you can also call the Package Access PUBLIC (public) key Word Public Represents Anyone can visit him Private (private house money) First think about the concept of private money, private houses are only called, because this 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 reflects encapsulation

Protected (Great Panda) Giant Panda is protected and his child is also protected because of the relationship between the giant panda, so the meaning of protected is to allow derived classes to access the base class, and other classes in the base class It is also accessible (Friendly)

Interface (Interface) AND IMPLEMENTION TRAING TEA INFACE usually uses access controls, he can pack data within the Class, allowing specific people to access, establish different access boundaries, usually we are in 2 Under the circumstances, access control 1 needs to be used, telling secondary development programmers, those available, those unavailable, so when we change relevant data, we 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 declare the constructor as private.

Exercise 1, write a program, generate an ArrayList object, but cannot be explicitly imported

Java.util. *; public class test {public static void main (string args []) {java.util.arraylist al = new java.util.arrayList ();}} 6, write a public, private, protected, Friendly's data member and function, and generate an object to observe, when you use a member of the class, what is compiled?

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 (); // In different packets cannot Access the protected members t.Methodd (); // In different packages cannot access the friendly members}} 7, write a class, let him have protected, Write the second class in the same file, and operate the protected data in the first class here.

Class protected int i;} public class test {public static void main (string args []) {protected-de-name (); pd.i = 10; system.out.println (pd.i);}} This procedure 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 THEIGN CLASS PART of THE C05.LOCAL PACKAGE ANYTHING? Because packagedclass is not public, but Friendly, it is a package access control, which can only be used in the package, if he put him in and foreign The directory, then they are equal to the same package, of course, can be accessed smoothly.

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

New Post(0)