CATEGORII DOCUMENTE |
Asp | Autocad | C | Dot net | Excel | Fox pro | Html | Java |
Linux | Mathcad | Photoshop | Php | Sql | Visual studio | Windows | Xml |
When you use the public keyword, it means that the member declaration that immediately follows public is available to everyone, in particular to the client programmer who uses the library. Suppose you define a package dessert containing the following compilation unit: (See page 97 if you have trouble executing this program.)
//: Cookie.java
// Creates a library
package c05.dessert;
public class Cookie
void foo()
Remember, Cookie.java must reside in a subdirectory called dessert, in a directory under C05 (indicating Chapter 5 of this book) that must be under one of the CLASSPATH directories. Don't make the mistake of thinking that Java will always look at the current directory as one of the starting points for searching. If you don't have a '.' as one of the paths in your CLASSPATH, Java won't look there.
Now if you create a program that uses Cookie:
//: Dinner.java
// Uses the library
import c05.dessert.*;
public class Dinner
public static void main(String[] args)
You can create a Cookie object, since its constructor is public and the class is public. (We'll look more at the concept of a public class later.) However, the foo( ) member is inaccessible inside Dinner.java since foo( ) is friendly only within package dessert.
You might be surprised to discover that the following code compiles, even though it would appear that it breaks the rules:
//: Cake.java
// Accesses a class in a separate
// compilation unit.
class Cake
In a second file, in the same directory:
//: Pie.java
// The other class
class Pie
You might initially view these as completely foreign files, and yet Cake is able to create a Pie object and call its f( ) method! You'd typically think that Pie and f( ) are friendly and therefore not available to Cake. They are friendly - that part is correct. The reason that they are available in Cake.java is because they are in the same directory and have no explicit package name. Java treats files like this as implicitly part of the "default package" for that directory, and therefore friendly to all the other files in that directory.
Politica de confidentialitate | Termeni si conditii de utilizare |
Vizualizari: 787
Importanta:
Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved