Java Getting Started: Chapter 6 Passages, Classes, Bags and Interfaces

zhaozj2021-02-16  77

In the previous chapters, our detailed introduction to Java's simple data type, array, operator, and formal formula and flow control method law. From now on, we have to go deep into the programmed programming technology, and we have gone to Java's most eye-catching place. In this chapter, we first explain the basic concepts and features of the iconic order design, and then discuss the class, objects, bags, and interfaces in Java, and the last small junction, give a complete Java file format. § 6.1 Side-to-face procedure to set up the procedure of the process from the solution to each step of the solution, it is suitable for solutions than smaller simple questions. The C language uses the programming model of the surface process, but is a language structure that has no support code reuse, and lacks a uniform interface, and the regulation of the program is difficult to control its complexity. The procedure of the pair of objects is based on the characteristics of the real world. It is the same as a pair, with its own status and behavior, and the response to the message should be completed. 6.1.1 Objects, classes, and messages An object is the collection of variables and related methods, where variables indicate the status of the object, indicating that the behavior of the object, the following figure shows the feature of an object: It can be seen from the figure, The variable of an object constitutes the core of this object, which encloses the object and other objects from the other object. For example, we can use the car car to look like a pair, with variables to show it the first state, such as speed, oil quantity, model, location, etc., it can be accelerated, brake, shift, etc. When we operate the vehicle, do not take into account the details of each of the parts of the car, but only the action of the car can be used as the method of using the corresponding method. In practice, the programming of the pair is now a seal for the object, so that the lines of do not have to pay attention to the object of the like are such a detail. Through the sealing of the object, the modularization and information hidden, which are beneficial to the transplantability and safety of the program, and also beneficial to complex to the complexity. There must be an interaction to interact with each other. For example, to speed up the car speed, you must send it a message, telling it what kind of action (this is acceleration) and the parameters required to realize this movement (this is required to reach the speed, etc.). The following figure shows the message passing process between the object A and the object B. From the figure, you can see that a message contains three square content: ● Message receiptists ● Receive the object to be used by the method ● The parameters required by the method. At the same time, after receiving the message, after the method of executing the correspondence, it may return some information to the sending message (in the above example, the vessel of the car will appear to be up to the speed, etc.).

By the way, the owner of any of the objects can be described by the method, and the interaction between the full-real-time pair, at the same time, in the same time, it can be interactively interact with the interval of the main machine. The pair of pairs said is a specific thing. Examples of an example, each car is a different object. However, there are some common sexes, such as all of the car have a wheel, and the direction of the air, there are some common things, such as all car cars have a wheel, a square pan, brake equipment, etc. It is the commonality of us like a class of objects, this is class. Define variables and methods for a class of objects in the class. A class of instantization is a pair of such a class. For example, if we can define a steam type to describe the commonality of the auto cart. Decons for class can be reused in real codes. We don't have to describe each of the objects (such as an auto car), but an example of creating a class (such as a car) to create a pair of objects, which greatly reduces the design of the software. 6.1.2 Continued by the contrast, class, we are now sealed, and we can act in real. For the previous example, bus, taxi, trucks, etc. are cars, but they are different cars, except for the commonality with cars, they also have their own characteristics (such as different ways, different purposes, etc.) . At this time, we can do it as a subclass of the car car, and they continue to be the state and behavior of the parent class (car), and the same time increases from the same time. Through the parent class and subclasses, we realize the level of class, which can be specially specialized from the most common class, step by step, and define a series of subclasses. At the same time, the reuse of the code is also reused, enabling the complexity of the program, rather than a geometric level. Supporting multiple relay in C , that is, a class can relax in multiple parent classes, which makes the active power of the like to be non-often complex and unpredictable (set up a number of parent classes with some of the variables and methods). Java only supports a single success, and greatly reduces the complexity. In Java, passing the interface can be reached in real, but the concept of interface is simpler, which is more simple, and is not limited to the relay, it makes multiple unacceptable classes can have the same method. 6.1.3 Multi-state JAVA communication and method reloading and manifold rendering. More than the method of rewriting, a class can be used in a manner that has a way with the same name. Which method is determined by the different numbers and type parameters delivered to them, which is multitamous.

For example, for a class of a mapping, it has a DRAW () method to draw or output text, we can pass it to it a string, a rectangle, a circle, or even specify the initial position of the mapping, The color of the graphics, etc. For each implementation, just achieve a new Draw () method, without a new name, this greatly simplifies the implementation and call of the method, programmers and users do not need to remember A lot of method names, only need to pass the corresponding parameters. In-process reload, the subclasses can reach some of the methods of the parent class to make it self-sufficient. For example, in the acceleration method of the steam type, some new parts can be added to improve the acceleration energy, which can be reinforced by the parent class in the race. Reversing the method of hiding the parent class, making the subclass with self-sufficient implementation, and further referring to the speciality of the child with the parent class. In this section, we have explained some basic components of the iconic order design, and we will explain that Java is in this incoming. § 6.2 Class Class is the basis of the basin of Java. It encloses a class of objects and methods, which are the prototypes of this type of object. In the example of the previous chapter, we have defined a simple class such as the Hellowo RLDAPP class. Public class helloworldapp {public static void main (string args []) {system.out.println ("Hello World!");}} It can be seen that a class implementation contains two parts: classDeclaration {classbody} below Each part is described in detail. 6.2.1 Class declarations A simplest class declaration is as follows: Class className {...}, for example: Class Point {...}, at the same time, in the class declaration, you can also include the parent class, the interface implemented by the class, and modifier public , Abstract or final. We will be in a few sections of the rear. 6.2.2 Category The variables are defined in the variables of this class and the method of the approach supported. The usual variable is defined before the method (not necessarily required), as shown below: Class ClassName {MEMBERVARIABEDECLARESSME {MEMBERVARIABEDEDECLARESME {MEMBERVARIABEDEDECLARESSMEDDECLARATIONS} The following example defines a Point class, and declares its two variables X, Y coordinates, and implement the init () method The first value is assigned to X, Y. Example 6.1 Class Ponit {INT X, Y; Void Init (INT IX, INT IY) {x = IX; Y = IY;} The variables and methods defined in the class are members of the class. The members of the class can set up the access rights to limit its access to it, and there are several of the following: Private, Protected, Publi C, Friendly. We will discuss in § 6.6.

At the same time, the members of the class can be divided into two kinds of practitioners and class members. We will discuss in § 6.8. 6.2.3 The simplest member variable of member variables is: Type Variablename; if the variables declared in Example 6.1, INT X, Y; member variables can be any data type in Java including simple types, arrays, Class and interface. In a class, the member variable should be unique, but the name of the member variable can be the same as the name of a method in the class, for example: Class Point {Int x, y; int x () {returnx x;}} Method X () and variable x have the same name. The partial variables of the class and the partial variables spoke in the method are different, and the work domain of the membership variable is the entire class, and the part of the local variable is only the internal part of the method. For a member variable, we can also define its access rights (see § 6.6), define it as a class variable (see § 6.7) with static, or define the following modifiers: Final: Used to declare a constant, such as: Class finalvar {final int constant = 50; ...} The constant constant is declared, and the value is 50. For the constant constant constituent with Final, it cannot be changed in the program. Tong often use uppercase letters.  (untrained to stay) 6.2.6 Configuration method is a special method. Each class in Java has a configuration method, which is used to initially initialize a new object of the class. Configuration method has the same name as the class name, and does not return any data type, in the realization of the configuration method, can also be rewritten in progress. Example 6.5 Class Point {INT X, Y; Point () {x = 0; Y = 0;} Point (int x, int y) {this.x = x; this.y = y;}} We have implemented two construction methods for class Point, and the name is POIN T, which is the same as the class name. And we used the method to rewrite, and the X, Y coordinate assignment of the point is divided into the same point. Recall in Example 6.2, we used the init () method to initialize the X, Y coordinate of the point. The two have the same function, where is the benefits of constructing methods? When using the operator NEW to allocate memory to an object, to call the object constructor, and when you create an object, you must use New to allocate memory for it. . Therefore, it is initialized by the initialization of the constructive method to avoid the initialization method of the object to be adjusted after the biopsy. If there is no real-catenation method, the Java running system will automatically provide the default constant method. It does not have any arguments. In addition, the configuration method can only be called by the New operator. We will be detailed in § 6.3. There is also a limit to the restrictions on the constructive method (see § 6.6).

6.2.7 Finalize () The method of the Java running time before the collection of pairs of garbage back to the Finalize () method of the self-moving parallel (), such as the open file or socket. The sound of the method must be as follows: protected void finalize () Throws throwable finalize () is now real in class java.lang.object. If the method is to be implemented in a defined class to release the resources occupied by this class (ie, the Finalize () method of the parent class is to be reloaded, the resources used by the class are translated, and it is generally called. The FINALIZE () method of the parent class is to clear all the resources used by the object, including resources obtained by the inheritance relationship. The usual format should be: protected void finalize () throws throwable {... // Clean Up code for this class super. Finalize (); finalize () method of its parent class is called through Superze () method. We will talk about § 6.4, and we will also talk about § 6.4 for class java.lang.ob-ject. § 6.3 Objects instantiate classes, we can generate multiple objects, these objects are interacted by messaging (message delivery, activating the method of the specified object to change its status or let it generate a certain behavior), finally completed Complex task. A pair of lifetime includes three stages: generated, used and clear. Below you, you can tell: 6.3.1 The biochemical enclosure of the biography of the object is inclusive, instantiated, and the initialized tripartite. The normal format is: type objectname = new type ([paramlist]); 1. Type ObjectName; a type of Type is ate. The medium TYPE is a set type (including classes and interfaces). The vocabulament of the like is not to distribute memory space for the object. 2. The operator NEW is a pair of memory spaces, instantaneous objects. The new call to the structure of the object, returns a reference to the object (ie the memory where the object is located). Use new to instantly use multiple different objects for a class. These objects are divided into different memory spaces, so this changed the status of a pair of objects in it not affecting its appearance. 3. The final step of the biopsy is to execute the square method, and the initialization is initialized. By rewriting by the conformation method, it is divided into different conformation methods by giving different numbers or types of parameters.

Take the class Point in Example 6.5 as an example, we generate the object of the class Point: Point P1 = new point (); point p2 = new point (5, 10); here, we have generated two objects for class Point P1 , P2, they respectively call different construction methods, P1 calls the default constructor (ie, there is no parameters), and P2 calls the constructor of the belt parameter. P1, P2 is different to the different memory spaces, and their values ​​are different, and they can be operated all over alone. Although the NEW operator returns a reference to an object, the pointer in C, C is different, and the reference to the object is to the intermediate data structure, which stores the information about the data type and the number of the stack where the current object is located, The actual memory address where the object is located is uncomfortable, which guarantees security. 6.3.2 Objects of objects The use of objects including reference objects include the member variables and methods of reference objects. Through operators, the calls, variables, and methods of access and methods of variables, can be used to set certain access rights (see § 6.6) Allow or disable other objects to its access. We first define a class Point, which adds some internals in the definition of Example 6.5.

Example 6.6 Class Point {INT X, Y; String Name = "a point"; Point () {x = 0; y = 0;} Point (int X, int y, string name) {this.x = x; THIS .y y;} int GETX () {return x;} int gety () {return y;} void Move (int newx, int newy) {x = newx; y = newy;} POINT NewPoint (string name) {point newp = new point (-x, -y, name); return newp;} Boolean Equal (int x, int y) {if (this.x == x && this.y == y ) Return True; Else Return false;} void print () {system.out.println (Name ": x =" x "y =" y);}} public class usingObject {public static void main (String args ]) {POINT P = New Point (); p.Print (); // Call method of an Object P.move (50, 50); system.out.println ("** after moving **"); system .out.println ("get x and y Directly"); system.out.println ("x =" p.x "y =" py); // Access variabl es of an object system.out.println "or get x and y by calling method"); system.out.println ("x =" p.gety () "y =" p.Gety ()); if (p.equal (50, 50 )) System.out.println ("i like this point !!!!"); Else Sy Stem.Out.println ("i Hate !!!!!"); p.NewPoint ("a new point") .print (); new point (10, 15, "another new point") .print () }}} The result is: c: / java usingObject a point: x = 0 y = 0 ****After moving ***** get x and y Directly X = 50 y = 50 or get x and y by calling Method x = 50 y = 50 i like this point !!!! a new point: x = -50 y = -50 Another new point: x = 10 y =

转载请注明原文地址:https://www.9cbs.com/read-14014.html

New Post(0)