Java Basics Review (6) __java

Source: Internet
Author: User
Tags constant definition modifier
1. Define Package and Reference package
2. Interface definition, characteristics of properties and methods in interfaces
Common exceptions in 3.java, try,catch,finally statement execution order
4.math,date, use of string classes



I. Defining packages and referencing packages

java-about customizing package creation in Java

I am learning thinking in Java4, when creating a custom package, encountered a lot of errors, let me depressed for a few days, through the online data search, the package to create a detailed list of the process.

We take thinking in Java4 as an example

Create two class vectors and list. Place the compiled Vector.class and List.class files under the package Net.mindview.simple (the package is located in C:\DOC\JavaT).

Vector.java

Package net.mindview.simple;

public class vector{

Public Vector () {

System.out.println ("Net.mindview.simple.Vector");

}

}

List.java

Package net.mindview.simple;

public class list{

Public List () {

System.out.println ("Net.mindview.simple.List");

}

}

Save these two classes under C:\DOC\JavaT\net\mindview\simple

The two classes are then compiled under DOS commands. The following figure

Create a libtest class, put it under C:\DOC\JavaT (not in net.mindview.simple bag)

Import above two classes in Libtest.java

Libtest.java

Import net.mindview.simple.*;

public class Libtest {

public static void Main (string[] args) {

Vector V=newvector ();

List L=new list ();

}

}

The following figure

Study should pay attention to two points: 1 vector,list and libtest can not be placed in the same (Libtest should be placed outside the Net.mindview.simple package)

2 Classpath settings can also be set under environment variables

Referencing the contents of another package in one package:

The top-level directory of another package must be under CLASSPATH, otherwise it cannot be import.
The top level directory is the PACKAGEAAA.BBB.CCC of the class to be introduced; The AAA in the declaration statement.
Classes that have declarations like PACKAGEAAA.BBB.CCC must be placed under a folder such as AAA/BBB/CCC. Otherwise it cannot be introduced.
To satisfy the above conditions, write directly in classes that refer to other packages or other classes
Import package name. *; (Introduce all the classes in a package) or the import package name. class name; (Introducing a single Class)

Two. Interface
1. Define Interface
Use interface to define an interface. Interfaces define similar definitions, and are also divided into interface declarations and interface bodies, where the interface body consists of two parts: constant definition and method definition. The basic format for defining an interface is as follows: [modifier] Interface Interface Name [extends list of parent interface names]{

[Public] [Static] [Final] constant;
[Public] [Abstract] method;
}
Modifier: Optional to specify access rights for an interface, with an optional value of public. If omitted, the default access rights are used.
Interface Name: A required parameter that specifies the name of the interface, and the interface name must be a legitimate Java identifier. Under normal circumstances, the initial letter is required.
Extends list of parent interface names: An optional parameter that specifies which parent interface the interface to define inherits from. When using the extends keyword, the parent interface name is a required parameter.
Method: The method in the interface is defined but not implemented.

For example, define an interface for calculation, in which a constant pi and two methods are defined, with the following specific code:

[Java] View plaincopy

1. Public interface Calinterface

2. {

3. Final float pi=3.14159f;//defines the constant pi used to represent PI

4. Float Getarea (float R);//define a method for calculating the area getarea ()

5. Float Getcircumference (float R);//define a method for calculating the perimeter getcircumference ()

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.