My thinking in Java Study Notes (9)

Source: Internet
Author: User

Chapter 5 hidden Implementation Details
First, consider an important idea of OOP-separating changed things from unchanged things.
When writing a library, Java Programmers need to consider that once they change a function or member variable in the class, the programs that have used the previous version of the library will not be affected. The database writer cannot know the functions and variables used by the programmer to call the database, so the database members cannot be modified. To solve this problem, an access control character occurs in Java, its role is to tell programmers that those are available and unavailable, while access controllers in Java are affected by packages. Therefore, before learning access controllers, first, you must learn the package.
Package: Library Unit
Now let's imagine this situation. If you have developed a Java program locally and put it into use now, when you put the program on the server, you may find that, the original class on the server has the same name as the class in your current program! How can this problem be solved? In this case, the namespace in Java is required. It is precisely because of the namespace that each class with the same name exists in their respective fields and does not interfere with each other. When you need to call classes in different namespaces, use the import keyword, example: Call the entire library import Java. util. *; this is to call a package. If you want to call one of the classes, import Java. util. math;
How to set your own package? Use package xxx; put it at the beginning of the program, that is, to include the program into the xxx package. When using it, you only need to import XXX. Keyword import and package provide that no matter how many people write the class, there will never be a name conflict!
Unique package name
Package should place all classes belonging to the same package in the same directory, or use jar. Now there are two more problems: how to generate a unique package name and how to find the class hidden in the file directory. We generally use reverse international domain names to ensure the correct solution of Issue 1. My domain name is _______ (unless he preemptively uses my stuff ). 2nd problems are solved through the classpath in the environment variable. classpath contains one or more directories. Each directory is regarded as the start point for searching the class file, and your package name is converted to the disk directory for searching.
Package maoerzuozuo.com. csdn. Blog. test;
Public class demo
{
Public demo ()
{
System. Out. println ("maoerzuozuo.com. csdn. Blog. Test. Demo ");
}
}
Call
Import maoerzuozuo.com. csdn. Blog. Test .*;
Public class test
{
Public static void main (string ARGs [])
{
Demo d = new demo ();
}
}
We store 2 files under F:/maoerzuozuo/COM/csdn/blog/test/. Then we call it and set the environment variable classpath = .; f:/; so the program runs normally. the compiler can find the package I called and run normally. Note: The class to be called must be public!
Conflict
If I call two packages, but both of them have the test class, when I instantiate the test class, you must tell the Compiler which package you instantiate the class, otherwise, a conflict may occur.
For example, java. util. Date d = new java. util. Date ();
Customize a library
Now, we can define our own library for your convenience. Here we define an output class to facilitate our program. Now we will write a display class.
Package maoerzuozuo.com. csdn. Blog. test;
Public class show
{
Public static void go (string S)
{
System. Out. println (s );
}
}
We store it in F:/maoerzuozuo/COM/csdn/blog/test, and point it to the classpath in the environment variable.
Import maoerzuozuo.com. csdn. Blog. Test. show;
Class Test
{
Public static void main (string ARGs [])
{
Show. Go ("hello ");
}
}
However, please note that when we call show. Go (123);, the Go () function cannot automatically convert int to string like system. Out. println ();
Tips on using package
When you use a package, you actually indirectly specify the actual system directory structure. Therefore, your class must be in this directory, and the system starts from classpath, query the directory.

Java access permission Control Term
In Java, there are access controllers for classes, functions, and member data. They are public, private, protected, and friendly (No Access Controller) in sequence, I remember when I was training, my teacher told me that "public is public, private is private, and protected is Giant Panda"
Friendly (friendly)
When no access control operator is written before class, function, or member data, friendly means that all classes can be accessed in the same package, or when you do not define any packages, as long as files in the same directory can access each other, the compiler will regard them as a package, however, classes outside the package are the same as private, which can also be called package access permissions.
Public)
The public keyword indicates that anyone can access it.
Private (private money)
First, think about the concept of private money. Private money is called this only because it can only be used by myself. So private means that no one can access the class except the class itself. Private fully embodies Encapsulation
Protected)
A giant panda is protected, and its children are also protected because of its inheritance. Therefore, protected is used to allow Derived classes to access members of the base class, other classes in the base class package are also accessible (friendly)
Interface and implemention (Implementation)
The access control operator is usually used to hide implementation details. It can wrap the data in the class according to different needs to allow specific people to access and establish different access boundaries.
Generally, we need to use an Access Controller in two cases.
1. Tell secondary development programmers that those are available and unavailable, so that when we change the relevant data, it will not affect other people's original programs.
2. Separate interfaces from implementations. Generally, client programmers use interfaces of this class. The specific implementation process does not need to be known to programmers. We can encapsulate them. In this way, we can declare the interface as public, and the internal implementation process can be declared as protected or private based on actual needs.
Class access permission
Before placing a keyword in a class, you can declare the access permission of the class. Note that
1. Each file can only have one public class to express a single interface. The file can have a friendly class to support the public class;
2. the name of the public class must be the same as the file name of the file, including the case;
3. When the file does not contain any public class, you can use any name as the file name;
4. The class access control operator cannot be private or protected. It can only be friendly or public. If you want others to not generate objects, you just need to declare the constructor as private.
Exercise answers
1. Write a program that generates an arraylist object, but cannot explicitly import 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, and friendly, and generate an object for observation. When you use the members in the class, what compilation messages do you have?
Package test;
Public class test
{
Public int;
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 ();
T. A = 1;
T. B = 1; // cannot access the private members in different/the same package
T. C = 1; // cannot access the protected members in different packages
T. D = 1; // cannot access the friendly members in different packages

T. methoda ();
T. methodb (); // cannot access the private members in different/the same package
T. methodc (); // cannot access the protected members in different packages
T. methodd (); // cannot access the friendly members in different packages
}
}
7. Write a class to allow its members to have protected. Write the second class in the same file and operate the protected data in the first class.
Class protecteddemo
{
Protected int I;
}
Public class test
{
Public static void main (string ARGs [])
{
Protecteddemo Pd = new protecteddemo ();
PD. I = 10;
System. Out. println (PD. I );
}
}
This program is actually observing that "protected in the same package is equivalent to package Access Control (friendly )"
12. Create the following file in the 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. 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. wocould making the foreign class part of the c05.local package change anything?
Packagedclass is not public, but friendly. It is packet access control and can only be used by members in the package. If you put it in the same directory as foreign, they are in the same package and can be accessed smoothly.

This chapter mainly refers to the access control operator, which is basically not very difficult. In the next chapter, we will explain how to reuse class

 

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.