Java programmer must read: Basic (8)

Source: Internet
Author: User
Tags add define execution final functions gety variables variable
Program | Programmer Java Programmer must read: basic articles
Time: 2001/09/13 13:31 Author: ZSC Pacific Network College


 

2.6 What is an interface

An interface is a contract that collects methods and constant forms. When a class executes an interface, it promises to declare that all methods are executed in that interface.

An interface is a device or a system that is an unrelated entity for interaction. According to this definition, remote control is an interface between you and the TV, while English is the interface between two people, and the behavior agreement that enforces in the military is the interface between different equivalence persons. In the Java language, an interface is a device that is used to interact with other objects. An interface may be similar to a protocol. In fact, other object-oriented languages have interface functionality, but they invoke their interface protocols.

The bike class and its class hierarchy define what a bicycle is. But bicycles interact with the real world in other ways, for example, in warehouses where bicycles can be managed by an inventory process. An inventory procedure does not care what kind of management project as long as the project provides a certain information, such as price and tracking number. Instead of forcing the class to relate to other unrelated items, the inventory procedure establishes a communication protocol. This protocol consists of constants and method definitions contained in an interface. This inventory interface will define (but not execute) methods to set up and get retail prices, specify tracking numbers, and so on.

In order to operate in the inventory procedure, the bicycle class must comply with this protocol when executing the interface. When one executes an interface, the class adheres to all the methods defined in the interface. As a result, bicycles provide execution for these settings and to obtain retail prices and to specify tracking values, and so on.

You can use interfaces to define a protocol for a behavior that can be executed by any class in the class hierarchy. The main benefits of the interface are a few things:

It is not necessary to force class relationships to intercept similar places in unrelated classes.

A method that declares one or more classes that you want to execute.

Exposes the object's programming interface without exposing the object's class.
2.7 How to translate these object-oriented concepts into code

This section will show you the code that creates objects, executes classes, sends messages, creates a parent class, and executes an interface.

Here is an applet (an applet is a program written in the Java programming language) that can run on a Web browser that is compatible with the Java platform, such as HotJava or Netscape Navigator, called ClickMe. As shown in Figure 10, when you click anywhere in the box, a red dot appears.


(Figure 10)

Tip: The applet above needs to be JDK1.1. If you use an older browser that does not support JDK1.1, you will not be able to run this applet. Instead, you need to look at this page in a 1.1 browser, such as HotJava, JDK Applect Browser (appletviewer), or a version of Netscape Navigator and Internet Explorer.

Here is a specific explanation for this applet.

The ClickMe applet is a relatively simple program, so its code is much shorter. However, if you don't have much programming experience, you can find that the code is not so easy. We don't ask you to immediately understand every problem in the program, and this tutorial is not very detailed. The purpose here is to expose some source code to you and to connect with the concepts and techniques you have just learned. You will learn more about it later in the tutorial.

2.7 How to translate these object-oriented concepts into code

2.7.1ClickMe source code and applet label

To compile this applet you need two source files: Clickme.java and Spot.java. To run this applet you will need to use this applet tag to create an HTML file:

<applet code= "Clickme.class"

Width= "height=" >

</applet>

Where the Clickme.java source code is:

Import Java.applet.Applet;

Import java.awt.*;

Import java.awt.event.*;



public class ClickMe extends Applet implements MouseListener {

Private spot spot = null;

private static final int RADIUS = 7;



public void init () {

Addmouselistener (this);

}



public void Paint (Graphics g) {

Draw a black border and a white background

G.setcolor (Color.White);

G.fillrect (0, 0, GetSize (). Width-1, GetSize (). height-1);

G.setcolor (Color.Black);

G.drawrect (0, 0, GetSize (). Width-1, GetSize (). height-1);



Draw a red dot

G.setcolor (color.red);

if (spot!= null) {

G.filloval (Spot.x-radius, Spot.y-radius, RADIUS * 2, RADIUS * 2);

}

}

public void mousepressed (MouseEvent event) {

if (spot = null) {

Spot = new Spot (RADIUS);

}

Spot.x = Event.getx ();

Spot.y = Event.gety ();

Repaint ();

}

public void mouseclicked (MouseEvent event) {}

public void mousereleased (MouseEvent event) {}

public void mouseentered (MouseEvent event) {}

public void mouseexited (MouseEvent event) {}

}

The source code for the Spot.java is:

public class Spot {

public int size;

public int x, y;



Public Spot (int intSize) {

size = IntSize;

x =-1;

y =-1;

}

}

Then load the Web page into the browser or Appletviewer tool. And make sure that all the necessary files are in the same directory. As shown in Figure 11:


(Figure 11)
Objects in the 2.7.2 ClickMe applet

There are many objects in this applet. The two most obvious are: the applet itself and the red dots.

The browser creates the applet object when it encounters an applet tag in the HTML code that contains the applet. This applet tag provides the name of the class from where the Applet object was created. In this example, the name of this class is ClickMe.

Clickme.applet will create an object to draw a point on the screen. Each time you click the mouse in the applet, the applet will move the red dot by changing the x and Y coordinates of the object. This point is not drawn by itself, it is drawn by the applet, it is based on the information contained in the Point object.

In addition to the two obvious objects, there are also some unseen objects. There are three objects representing three colors (black, white, red) and event objects that represent the action of the user clicking the mouse, and so on.
Classes in the 2.7.3ClickMe applet

Because the object representing the dots on the screen is simple, let's take a look at this class called spot. It declares three instance variables: the size of the point radius, the x-coordinate that contains the current horizontal position of the point, and the y-coordinate of the current vertical position of the containing point:

public class Spot {

Instance variables

public int size;

public int x, y;


Constructors

Public Spot (int intSize) {

size = IntSize;

x =-1;

y =-1;

}

}

In addition, the class has a constructor that initializes a new object created by the class. Constructors have the same name as the class. This constructor initializes the variables for all three objects. The initialization value for size is provided by the seat parameter at the time of the call. The x and Y variables are set to-1, where-1 is designed to allow the point to be on the outside of the screen at the beginning, creating a false visual effect.

This applet creates a new Point object when the applet is initialized. Here is the code for the Applet class:

Private spot spot = null;

private static final int RADIUS = 7;

...

Spot = new Spot (RADIUS);

The first line declares a variable named spot, which is the spot data type, and initializes the variable to null. The second line declares an integer variable, named radius, whose value is 7. The last line is to create an object. The new keyword allocates memory space to the object. Spot (RADIUS) invokes the constructor described above and passes the RADIUS value so that the size of the point object is set to 7. The left figure shown in Figure 12 represents the spot class, while the right-hand one represents the spot object.


(Figure 12)
Messages in the 2.7.4ClickMe applet

As you know, object A can use messages to request object B to do something, and a message has three components:


The object to which the message is addressed


The name of the execution method to execute


Any parameters that the method requires

The following two lines of code are available in the ClickMe applet:

G.setcolor (Color.White);

G.fillrect (0, 0, GetSize (). Width-1, GetSize (). height-1);

All two messages are from the applet to the object named G. Where G is a graphic object, it knows how to simply draw some shapes or text on the screen. This object provides an applet when the browser instructs the applet to draw. The first line of the above code sets the color to white, and the second line fills a rectangular area of the specified size, and its color is white. As shown in Figure 13, it is a three component of a message:


(Figure 13)
Inheritance in the 2.7.5 clickme applet

In order to run in a browser, the object must be an applet. This means that the object must be an instance of the class that derives from the applet class provided by the Java platform.

The ClickMe applet object is an instance of a ClickMe class, which is declared like this:

public class ClickMe extends Applet implements MouseListener {

...

}

The above statement produces a subclass of the applet. ClickMe inherits many of the functions of the parent class, including initialization, starting and ending by the browser, drawing in the browser area, and registering with the mouse events received. In addition to these features, the ClickMe class also implements the following functions: its drawing code in the Paint method, initialization code must be in the Init method, and so on.

public void init () {

...//Add ClickMe initialization code here

}


public void Paint (Graphics g) {

...//Add ClickMe Paint code here

}

Interfaces in the 2.7.6 ClickMe applet

The ClickMe applet responds to mouse clicks by displaying a red dot in the mouse click. If the object wants to notify the mouse to click, the Java Platform Event system requires the object to perform the MouseListener interface. This object must be registered as a mouse listener as well.

This MouseListener interface declares five different kinds of log workers, each of which uses a different mouse event when the mouse is clicked. When the mouse is clicked, or when the mouse is moved outside the applet, and so on.

Here is the complete code for the ClickMe applet. Where mousepressed is the handling of mouse events:


Import Java.applet.Applet;

Import java.awt.*;

Import java.awt.event.*;



public class ClickMe extends Applet implements MouseListener {

Private spot spot = null;

private static final int RADIUS = 7;



public void init () {

Addmouselistener (this);

}



public void Paint (Graphics g) {

Draw a black border and a white background

G.setcolor (Color.White);

G.fillrect (0, 0, GetSize (). Width-1, GetSize (). height-1);

G.setcolor (Color.Black);

G.drawrect (0, 0, GetSize (). Width-1, GetSize (). height-1);



Draw a Point

G.setcolor (color.red);

if (spot!= null) {

G.filloval (Spot.x-radius,

Spot.y-radius,

RADIUS * 2, RADIUS * 2);

}

}

public void mousepressed (MouseEvent event) {

if (spot = null) {

Spot = new Spot (RADIUS);

}

Spot.x = Event.getx ();

Spot.y = Event.gety ();

Repaint ();

}

public void mouseclicked (MouseEvent event) {}

public void mousereleased (MouseEvent event) {}

public void mouseentered (MouseEvent event) {}

public void mouseexited (MouseEvent event) {}

}

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.