Chapter 5 Java's class
Class is the basic elements of the Java language-facing object-based object, which defines a parallelism and behavior of an object. In the Java program, you have to set up the concept into a certain class. A class defines a structure of a pair of icons and its functional interface, and the function interface is called a member function. When the Java program is run, the definition of the custom class is aware of the instance of the class, the instance of the class is a true object. The general form of a definition is as follows:
Class classname extends superclassname {type instance-variable1; type instance-variable2; ..................................................................................... -variablen; type methodname1 (parameter-list) {methodname2 (parameter-list) {method-body;} ..................... .................................................................... {method-body;}}
This, classname and superclassname are the identifier of the method. Key words extends are used to express ClassName is a subclass of superclassname. There is a class named Object, which is the root of all Java classes. If you want to define the direct subscriber class of Object, you can omit the Extends clause, and the compiler will automatically package it. The lower side is a definition of a simple class.
Class university {}
5.1 Also instance
Class names can be used as a type of variable, and if a variable type is a class, it will point to the instance of this class, called an active example. All object instances and their types (a class? Copy; instances of subclasses of the subclasses are compatible. As can be assigned to the INT type variable, you can put any Object's subclass of any The instance is an Object type variable. One instance is a separate copy of the class template with its own data set called instance variable. Each instance can also be used as an object. When you define a variable type is a certain When class, its default is null, null is an instance of Object. Object NULL does not value, it is different. In this example, the type of declared variable U is class university.
University u;
In this, the value of the variable U is NULL.
5.2 instance variable
Java sounds the data into a class in a category. The variables in this are called example variables. The examples of the next example define a class named University, which has two instance variables: Name and City.
Class university {string name, city;
5.3 New Operation
The operation NEW is used to generate an example of a class. The following example is generated by an instance of the class university, which is stored in the variable U. University u = new university ();
In this case, the variable u points to this object, but it is not really incorporated with this object. You can point to the same object with multiple variables. In the following example, create a university's object, but built two variables to it.
University u = new university (); university u2 = u;
Anything of the object to which the U2 pointed to the U2 works to the object to which it is pointed, because they are the same object. The assignment to U and U2 just points them to this object, neither distribution memory, and no part of this object. The re-assignment of U is simply removed U and the original pair of icons, and does not affect the image of the body, the case under the following.
University u = new university (); university u2 = u; u = null;
Although u is assigned NULL, U2 still points to the original object created by the operator NEW. In the previous example, I have become an object and point it to it twice. This allows two variables to change the same object. When you create a new object, you can directly assign a value to its instance variable. Each pair is a copy of the instance variable it belongs. Each pair of instance variables are separated from the example variables of other objects, and the example variables that change a pair of objects do not affect their example variables. The following example creates two university's objects and assigns them to them:
Class Twouniversity {public static void main (string args []) {UNIVERSITY U1 = new university (); university u2 = new university (); u1.name = "North? copy; university"; u1.city = "North? Copy "; u2.name =" Tsinghua University "; u2.city =" North? copy; "; system.out.println (" University: " U1.Name " City: " u1.city); system. Out.println ("University:" U2.Name "City:" U2.city);}}
This example creates two university's objects, and the Name, city is different, which is true that the two pairs are true. The lower surface is the output result after the order is run.
C: /> Java TWOUNIVERSITY University: North? Copy; Mathematical City: North? Copy; University: Qinghua University City: North? Copy; 5.4 (.? COPY;
Point (.? Copy; operator is used to receive an example variable variable and a member function of an object. The lower surface is a general form of a general form variable.
ObjectReference.variablename
This ObjectReference is an object, Variablename is the instance variable you want to receive in this object. The next step segment explains how to use a point operation to give the instance variable.
U.NAME = "North? Copy; University"; u.city = "North? Copy;"
It is said that it is clear how to use a point of operation to obtain the value of the exemplary variable.
System.out.println ("University:" U.Name "City:" u.city);
Incorporate a member of a member in the University, I created a complete example, which used the new operation to create a university, assigned by point operation, and then printed.
Class university {string name, city; public static void main (string args []) {university u = new university (); u.Name = "North? copy; university"; u.city = "North? copy;"; System.out.println ("University:" U.Name "City:" u.city);}}
After running this process, you will get the result of the lower surface.
C: /> Java University University: North? Copy; University City: North? Copy;
5.5 Member Function Definition
The member function is a class of functional interface, which is a sub-program in the class definition, in the same level in the definition of the class, and the instance variables at the same level. You must use a class's instance to call the member function. The function function can be directly used without pointing actions. The member function has an input parameter, with some type of return value. The general form of a function of the function of the function is as follows:
TYPE METHODNAME (FORMAL-parameter-list) {method-body;}
Here TYPE refers to the type of return value of the member function. If there is no return value, use no value (Void? Copy; type. MethodName can be any legal identifier, but cannot be the same as the current class name. Formal-parameter -list is the type of comma-separated type, identifies the sequence of the pair. If there is no parameters, brackets are empty. Or use our university example, the following member function is used to initialize two instance variables. The member function is classified Brandic? REG; within the defined, and the exemplary variables are the same .class university {string name, city; void init (string a, string b) {name = a; city = b;}}
Note, we directly give Name and City assignment, and not like U1.Name before. This is performed in an instance of the class for each member function. The instance of the class created by our class has its own instance variable, and the member function can be directly used.
5.6 member function call
You can use the point (.? Copy; manifold to call the members of a class of instances. General formulations of the member function calls are as follows:
ObjectReference.methodName (parameter-list);
Here, ObjectReference is a variable that points to an object. MethodName is a member function of the class of ObjectReference. Parameter-List is a sequence of variables or expressions separated by commas, and they want to define the number of parameters and types of the member function. match. In this example, we can assign Name and City to Name and City in this example. The procedure section of the lower surface explains how to finish this work.
University u = new university (); u.init ("North? Copy; University", "North? Copy;");
This example creates an instance of the university to store it in the U. To call the INIT member function of this example, put "North? Copy; University" and "North Copy;" to the parameters a and b. In the init entry function, Name and City direct the example variables of the object pointed to the U. Assign Name to "North? Copy; University", the city assignment is "North? Copy;", then return. In this example, INIT is defined as no value (void? Copy; return type. After this member function is called, u point to this Name value and the CITY value change the university object.
5.7 THIS
Java has a special instance value called this, which is used to point to the front of the previous object in one member function. In the front-faced example, I call U.init, one? Copy; into the Init membership function, this will point to the object pointing to the U. In Java, the local variables of the same name of the same name in the same range are unable to define. Interestingly, the local variable, the parameter of the member function can be the same as the name of the real-variable variable. The front side did not use Name and City as the parameter name of the members of the member, because of this, the instance variable Name and City hide the instance variable Name and City, namely Name points to the parameter Name, hidden the real variable Name. This allows us to directly point to the object. The next is another version of INIT, uses Name and City as a parameter name, with this to receive the instance variables of the front object. Void init (string name, string city) {this.name = name; this.city = city;
The lower side is a Twob County Escort with a new init initial entry.
Class university {string name, city; string city {this.name = name; this.city = city;}}
Class TwouniversityInit {public static void main (string args []) {university u1 = new university (); university u2 = new university (); u1.init ("North? copy; university", "North? copy;"); U2.init ("Tsinghua University", "North? Copy;"); System.out.println ("University:" U1.Name "City:" U1.city); System.out.Println ("University : " U2.Name " City: " U2.city);}}
5.8 Constitor® Copy;
Every instance of establishing a class is to initialize its variable. If a pair is created, it will be made into the initial work, which will be simple and simple. Therefore, Java is in the class, Copy; a special member function called constructor (constructor? Copy ;. A constructor is a member function of the initial object when the object is created. It has the same class is exactly the same. Name. One? Copy; Define a constructor to call it automatically when creating an object. The constructor does not return the type, even if the Void type is not. This is because the type of constructor's return value is this class. It itself. The task of constructor is the internal state of the initial object, so after creating an instance with the New operator, it will be clear, available objects. In this example, the constructor replaces the member function init.class University {string name, city; university (string name, string city) {this.name = name; this.city = city;}}
Class universitycreate {public static void main (string args []) {university u = new university ("North? copy; university", "North? copy;"); System.out.Println ("University:" u.name "City:" u.city);}}
The parameter after the name in the New statement is the biocandon function.
5.9 Member Function Contact
For a few membership functions close to the similar, there is a name of the same name. Therefore, the Java language is now a member's function to create a few names, and the number of non-paragraphs is different. Member function reloading? Copy; multi-state line of Java. The examples of the lower surface are used for overload.
Class university {string name, city; so, string city {this.name = name; this.city = city;} university () {name = "North? copy; university"; city = "North? COPY "}}
Class universityCreatealt {public static void main (string args []) {UNIVERSITY u = new university (); system.out.println ("University:" u.name "City:" u.city);}
This example creates a university object and calls the second constructor. The lower surface is its lucky result.
C: /> Java universitycreatealt University: North? Copy; Maxima City: North? Copy; One Configuration Function can be called another configuration function to create an example. E.g:
Class university {string name, city; network {this.name = name; this.city = city;} university () {THIS ("North? Copy; University", "North? copy;" }}
The second structural function is adjusted by the first constructor to complete the initialization of the example. You can also create a uniform member function with the reload. There are two versors of the two versions of the UNIVERSITY class in this example. SAMECITY judges whether a big learning is in a city or a big school and another big learning is in the same city. One member function is used by a city, and the other is parameter with the university pair.
Class university {string name, city; string city {this.name = name; this.city = city;} boolean samecity (string city) {if (city.equals (this.city) Return True Else Return False;} Boolean SameCITY (UNIVERSITY U) {Return SameCity (U.city);}}
Class universityCITY {public static void main (string args []) {string city = "Shanghai"; university u1 = new university ("North? copy; university", "North? copy;"); University U2 = New University (" Tsinghua University "," North? Copy; "); System.out.Println (" U1 = " U1.NAME ", " U1.city); System.out.Println (" U2 = " U2.NAME "," u2.city); system.out.println ("city =" city); system.out.println ("U1.SAMECITY (U2) =" U1.SAMECITY (U2)); system. Out.println ("U1.SAMECITY (City) =" U1.SAMECITY (City));}}
The lower surface is the operational result of the program.
C: /> java universityCITY U1 = North? Copy; University, North? Copy; u2 = Tsinghua University, North? Copy; city = Shanghai u1.samecity (u2) = true u1.samecity (city) = false
5.10 relay
The second basic face-to-object system is successful. The continuation is the class of classes related to the level of closing. The rear generation of a class can keep the ancestors of the ancestors and the function function, just like the creation of self-establishment. A class of direct father is called its superclass (Superclass? Copy; one? Copy; You created a class like the university, it is very simple. A subclass of a class is its inheritance. Special versions of instance variables and members functions. In this example, we derive the UNIVERSITY class into subclats with third elements called Country .class universityWorld Extends University {String Country; String Name, String City, String Country) {this.name = name; this.city = city; this.country = country;} universityWorld () {THIS ("North? Copy; University", "North? Copy;", "China");}}
Key words Extends are used to show that I have to create a subclass of UNIVERSITY. Name and City do not need to be in the universityWorld, because they are from the university. Java allows the variable Name and City in the universityWorld, but this will hide the Name and City in the university, which is to avoid it with the purpose of the subclass, should be avoided. In the instance of UNIVERSITYWORLD, Name, City and Country are both.
5.11 Super In the universityWorld case, there is a code and its super-class university's recurrence, this code is initialization Name and City,
THIS.NAME = Name; this.city = city;
Just like this in the University Example, use this to point to the first constructor, there is another variable in Java to make super super, which directs the configuration function of the super class. This example is used to initialize the variable Name and City with super, then print out the content of this object.
class UniversityWorld extends University {String country; UniversityWorld (String name, String city, String country) {super (name, city); // constructor is called University (name, city) this.country = country;} public static void main (String args []) {UNIVERSITYWORLD U = New UniversityWorld ("North? Copy; University", "North? Copy;", "China"); System.out.Println ("University:" u.name "city : " U.city " Country: " u.country);}} The following is the run results.
C: /> Java universityWorld University: North? Copy; Mathematical City: North? Copy; Country: China
5.12 Human Function Coverage
This new subclass of this university is successively taking its super class member function SAMECITY. However, this membership function Samecity judged the name of the two cities, which is not enough. Because there are two two famous names that are different countries, we must use the same time to judge the city and the national members of the country to cover it. The lower surface is the example that is now covered.
Class university {string name, city; string city {this.name = name; this.city = city;} boolean samecity (string city) {if (city.equals (this.city) Return True Else Return False;} Boolean SameCITY (UNIVERSITY U) {Return SameCity (U.city);}}
class UniversityWorld extends University {String country; UniversityWorld (String name, String city, String country) {super (name, city); this.country = country;} boolean samecity (String city, String country) {if (city.equals ( U.city) && country.equals (u.country)) Return True; Else Return False;} Boolean SameCITER {Return Distance (Other.city, Other.country);}}
Class universityWorldCity {public static void main (string args []) {string city = "Shanghai"; string country = "China"; UNIVERSITYWORLD U1 = New UniversityWorld ("North? Copy; University", "North? Copy;", " China "); UniversityWorld U2 = New UniversityWorld (" Tsinghua University "," North? Copy; "China"); System.out.Println ("U1 =" U1.NAME "," U1.city "," u1.country); system.out.println ("U2 =" U2.NAME "," U2.city "," U2.COUNTRY); System.out.Println ("City =" city ", country =" country); system.out.println ("U1.SAMECITY (U2) =" u1.samecity (u2)); system.out.println ("U1.SAMECity (city, Country ) = " u1.samecity (city, country);}} The following is the output result.
C: /> Java universityWorldCity u1 = North? Copy; University, North? Copy;, China U2 = Tsinghua University, North? Copy ;, Chinese City = Shanghai, country = China u1.samecity (u2) = true u1.samecity City, country = false
5.13 dynamic member functions send
When you use a point-in-operator to call a member function of an instance, the class belonging to the object instance is checked at the time of compiling to ensure that the membership function is stored in this class. At the time of run, the object instance can point to an example of the sounded type of subclass. At this? Copy; if the child is covered by the child, Java uses instance to determine which member function is called. As the example below, two classes are the subclass and super class, and the subclass covers the super class of members.
Class a {void callme () {system.out.println ("in a Callme member function function");}}
Class B Extends A {Void Callme () {System.out.Println ("in the Callme Member Function of B);}}
Class dispatch {public static void main (string args []) {a a = new b (); a.callme ();}}
Interestingly, in the membership function main, we spoke the variable A as type A, then put one example of class B to it. We call the member function in A. The Java compiler determines that the class A does have a member function Callme, but is running, the A thing is implemented, the Callme of B, and does not adjust A. The lower surface is a row of rows: C: /> Java dispatch in the Callme members of B
5.14 Final
In the default, all employees functions and instance variables can be covered. If you hope that your variable or the function function is no longer covered by the subclass, you can spoke them into Final. This means that the examples will rely on this definition. E.g:
Final int file_new = 1; final int file_save = 3; fianl int file_saveas = 4; Final Int file_quit = 5;
Final variables use a larger write identifier to be a general approach.
5.15 static state
If you want to create a member function that can be called outside the instance, then you only need to declare it is static (static? Copy; it will run normally. Static member functions can only call other static member functions directly, You can't use this or super in any way. You can also declare the variable as static. If you want to initialize a static variable, you can declare a block with a Static declaration to execute a program when you call. The following example is a band There is a static member function, several static variables, and a class of static initial blocks.
Class static {static int a = 3; static int b; static void method (int x) {system.out.println ("x =" x); system.out.println ("a =" a); system .out.println ("b =" b);} static {system.out.println ("Static initial block"); b = a * 4;} public static void main (string args []) {Method (42 }}
One? Copy; This class is called, all static variables are initialized, and a is assigned to 3, then run the Static block, which will print a message, and put B to a * 4, ie 12. Then the postlon is called the MAIN member function, which calls the member function Method, the parameter X is 42. These three PrintLn statements print two static variables A, B and local variables x. The lower surface is the results of the run:
C: /> Java Static Static Initial Block X = 42 A = 3 B = 12 A static member function can be called by the class name it belongs. The like modulus variables, you can call the static member function and static variables with a point of operation. Java is this real-world function and a total variable. In the case of the next example, we created a class with a static member function and two static variables. The second class can call the first class of static member functions and static variables in communication with the name.
Class StaticClass {Static Int A = 42; Static INT B = 99; Static Void Callme () {System.out.Println ("a =" a);}}
Class StaticByname {public static void main (string args []) {staticclass.callme (); system.out.println ("b =" staticclass.b);}}
The lower surface is the results of the run:
C: /> java staticByname a = 42 b = 99
5.16 abstract
If you need to define a whole, you have given an abstract structure, but don't give the finished category of each member function. If a member function does not have a complete implementation, you must be overwritten by subclasses, you can declare it as abstraction (Abstract? Copy; "The class containing the abstract member function must be declared as abstraction. In order to put a class declaration as Abstract, you only need to place a keyword abstract. This is a Copy; Class cannot generate an instance with a New operator directly because their full implementation has not been defined. You cannot define an abstract constructor or Abstract static member functions. Abstract classes or all of its superclass all abstract member functions, or also being declared as abstract. The following example is a class with abstract member functions, which is later implemented. The class of the member function.
Abstract Class A {Abstract Void Callme (); void metoo () {system.out.println ("in a Metoo member function in A");}}
Class B Extends A {Void Callme () {System.out.Println ("in the Callme Member Function of B);}}
Class abstract {public static void main (string args []) {a a = new b (); a.callme (); a.metoo ();}}
The lower surface is the results of the run:
C: /> Java Abstract in the Callme members of B in the Metoo members in A
chapter summary