码家

Web Platform, Cloud and Mobile Application Development

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
 
Ø In Java, Packages are used for grouping a number of related
    classes and interfaces together into a single unit 在Java中,包是用来组织相关的类和接口在一个单元中,将这些类和接口分组分类。
ØIn other object oriented languages like C++ a similar concept of namespaces exist
在其他面向对象语言,例如C++,一个相似的概念就是存在的命名空间。
 
第一层:包
第二层:类和接口
第三层:类/实例变量
第四层:方法
第五层:局部变量、方法内的语句
 
ØPackages can be of two types:
§Built-in Packages
§User-defined Packages
 包可以有两种类型:
内置的包
用户定义的包
 
A package is a collection of related classes and interfaces. Packages are containers for classes that are used to keep the class name space compartmentalized.
一个包就是相关的的类和接口的收集。包是给那些类的容器,这个容器用来区分类的名字空间。
The package declaration, if any, must be at the beginning of the source file.  You may precede it with white space and comments, but nothing else.  Only one package declaration is permitted and it governs the entire source file.
包的定义,如果有的话,必须定义在源文件的开始。你可以将包的定义被空白和注释领先,但是不能被其他的领先。只能允许有一个包的定义,这个包的定义管理整个源文件。
A java program can contain any of the following four parts.
•A single package statement (optional)
•Any number of import statements (optional)
•Any number of classes and interfaces out of which only one class can be public
一个java的程序可以包括任意下面的四个部分。
  • 一个单独的包的定义语句。(可有可无)
  • 任意数量的import语句。(可有可无,可多可少)
  • 任意数量的类和接口,但是只能有一个类可以用public来修饰。
The package statement defines a namespace in which classes are stored.  It is nothing but a directory, in which a class is defined.  When the package name is omitted, it is put into the default package, which has no name (i.e.. The current directory)
 
 包的语句定义了一个命名空间,在这个空间里存储了类。包仅仅只是一个目录,在这其中定义一个类。当包的名字被省略了,就将他看做一个默认的包,没有名字(也就是当前目录)
 
 
Built-in-Packages
内置的包
ØThese packages provide the set of classes, interfaces and methods for the programmer to develop an application in an easier way
ØProgrammer can reuse everything from the package and save effort
ØFew examples of built in packages
§java.lang
§java.io
§java.sql
§java.awt
§java.net
 
Øjava.lang
§Contains classes that  are  essential for developing basic Java programs
§String class, System class etc belong to this package
§There is no need to explicitly import this package
Øjava.io
§Helps to perform various  input and output operations
Øjava.sql
§Provides classes and interfaces that help to connect to the database like SQL server, Oracle easily
 
Øjava.awt
§Provides classes for implementing GUI Application
    eg: windows, buttons, menus etc
Øjava.net
§Provides classes for networking
 
User Defined Packages
用户定义的包
ØA Package can be created with the help of  ‘package’ keyword
ØThe package  should be the first statement in the source file(.java)
一个包可以用关键字"package”来定义。
包必须是源文件中的第一句。
package <<package name>>;
public class classname{
//Code goes here
}
class classname{
}
 
•Organize your classes into smaller units  and make it easy to locate and use the appropriate class file – i.e., you can split up the classes logically.
组织类到一个更小的单元,将它变的更容易定位。使用适当的类文件,也就是你可以从逻辑上将类分开。
•Avoid naming conflicts – Hierarchical approach to store files/classes.
避免名字的冲突,分层的方法去存储文件或者类
•Packages allow you to protect your classes, data and methods in a larger way than on a class-to-class basis.
包允许你去保护类,数据,和方法用一个更大的方式,相比于基于类与类的方法
•Package names can be used to identify your classes.
包的名字可以用来鉴定你的类
 
ØIn one .java file, there can be only one class declared public and that becomes the name of the .java file
在一个类的文件中,只有一个类能够被定义成public修饰的,被public修饰的也成为了java文件的名字
ØA package can however have several public classes spread across multiple .java files
然而一个包可以有几个public修饰的类,只要这些类是分布在不同的类文件中
ØMore than one class/interface can have the same name but it should be in a different package
可以不止一个类或者接口有同样的名字,不过这些类应该在不同的包中。
 
If there is main method in a class in a .java file, that class should be the name of the .java file. 
如果在java文件中的一个类有一个main的方法,那个类应该是那个java文件的名字。
 
Accessing Classes from the Packages
 存取其他包中的类
ØThere are two ways to access a class associated with a package
有两种方法可以访问和包关联的类
ØMethod 1:
§Using the fully qualified class name
–java.lang.Math.sqrt(varOne);
方法1
用完整的合格的类名
Ø Method 2:
§Import the package and use the class name directly
–import java.lang.Math;
Math.sqrt(varOne);
方法2
引入包,然后直接使用类名
 
Here, sqrt() method is a static method of Math class, hence can be invoked using the class name.
这里,sqrt()方法,是一个数学的类的静态的方法,因此可以直接用类名来调用
 
Access Modifiers – Revisited
访问修饰符,复习
 
?In Java , there are four access control modifiers
在java中,有四种访问控制符.
§private: Accessible only within the class
private:只能在这个类中被访问
§default: No keyword, Accessible only within the package
默认的,没有关键字,只能在包中被访问。
§protected: Similar to default with the addition that it is available to all child classes; that is, even if child class is in a different package
protected:和default比较相似,被pretected修饰的可以被所有的子类利用;即使这个子类在不同的包中
§public: Accessible to all

public:可以被所有的得到或者访问
ØData Members and Methods can have any of these

specifiers
ØClasses and Interfaces can have either the

public access or the default access

public:可以被所有的得到或者访问
ØData Members and Methods can have any of these  specifiers
数据成员和方法可以被任意这些的修饰符修饰。
ØClasses and Interfaces can have either the  public access or the default access
类和接口可以被public或者默认的来修饰符来控制访问。
 
If no access specifiers are provided for a class, methods and variables in Java , the default access specifier is considered
如果类、方法、变量没有被修饰符修饰,那么就会被看成默认的访问控制符。
 
Access Control
 
  
 
Method Overriding - Revisited
overriding用在子类继承父类的关系下,有overriding的前提是必须要有继承的关系,没有继承就谈不上overriding
 
The access level cannot be more restrictive than those given in parent
访问控制符的水平在子类中不能比父类的更严格
The access level can be less restrictive than that given in the parent
子类中的访问控制符可以没有父类中的访问控制符那么严格,可以弱一点
posted on 2011-05-22 09:45  海山  阅读(293)  评论(0)    收藏  举报