12-20java object-oriented Object Class & amp; Package Class & amp; anonymous internal class

Source: Internet
Author: User

12-20java object-oriented Object Class & amp; packaging Class & amp; anonymous internal class
1. Object Class
In java Development, all classes exist as inheritance. If a class does not explicitly specify the class to be inherited, the Object class is inherited by default.

For example

 

class Person {}

 

The above Code defines the Object by default, and its complete expression is:

class Person extends Object{}

In the jdk documentation, API> index> java. lang> Object searches for the Object class.

A complete class should be the method for overwriting the Object class. The following describes the important methods.

1.1 public String toString ()

Called when the object is printed.

For example

 

Class Person // defines the Person class, which actually inherits the Object class {} public class TestObject {public static void main (String [] args) {Person per = new Person (); system. out. println (per );}}

 

The result is the per address.

Since Person inherits the Object class, all methods of the Object class (friendly and public) can be used. Use toString ()

Class Person // defines the Person class, which actually inherits the Object class {} public class TestObject {public static void main (String [] args) {Person per = new Person (); system. out. println (per. toString ());}}

If the results are the same, the toString () method will be called during printing. It is called by default. You can use this method to output information.

Class Person // defines the Person class, which actually inherits the Object class {private String name; private int age; public Person (String name, int age) {this. name = name; this. age = age;} public String toString () {return name: + this. name +, age: + this. age ;}} public class TestObject {public static void main (String [] args) {Person per = new Person (Tyrion, 24); System. out. println (use toString () method + per. toString (); System. out. println (do not use the toString () method + per );}}

Therefore, if you want to print the output object information in the future, you can directly overwrite the toString method and use the object to print it.

1.2 public boolean equals (Object obj)

Equals () compares object content, which was previously used in the String class.

Class Person // defines the Person class, which actually inherits the Object class {private String name; private int age; public Person (String name, int age) {this. name = name; this. age = age;} public boolean equals (Object obj) {if (this = obj) // indicates that the two classes occupy one address {return true;} if (obj instanceof Person) // you must ensure that the Object is a class before comparison. The Object cannot be compared because the attributes of the two objects are the same {// Object is the parent class. Therefore, you must compare the attributes of the parent class, person per = (Person) obj; if (this. name. equals (per. name) & this. age = per. age) {return true;} else {return false;} public String toString () {return name: + this. name +, age: + this. age ;}} public class TestObject {public static void main (String [] args) {Person per1 = new Person (Tyrion, 24); Person per2 = new Person (Tyrion, 24); System. out. println (per1.equals (); System. out. println (per1.equals (per2 ));}}

Note:

During the overwriting process, the Object is used. Because the Object is the parent class of all classes, you must first perform the instanceof judgment and then perform a downward transformation to change the Object class into a subclass before continuing the comparison.

Program expansion:

This program illustrates this problem-different sub-classes can be defined (as opposed to objects), and they may need to be compared to directly inherit the method.

But the problem is:

 

Class Person // defines the Person class, which actually inherits the Object class {private String name; private int age; public Person (String name, int age) {this. name = name; this. age = age ;}} public class TestObject {public static void main (String [] args) {Person per1 = new Person (Tyrion, 24); Person per2 = new Person (Tyrion, 24); System. out. println (per1.equals (per2 ));}}
False

 

 

Class Person // defines the Person class, which actually inherits the Object class {private String name; private int age; public Person (String name, int age) {this. name = name; this. age = age ;}} public class TestObject {public static void main (String [] args) {Person per1 = new Person (Tyrion, 24); Person per2 = new Person (Tyrion, 34); Object obj1 = per1; Object obj2 = per2; System. out. println (obj1.equals (per1 ));}}
True

 

 

The equals method of the Object class implements the maximum possible equality relationship between objects. That is, for any non-null reference values x and y, if and only when x and y reference the same Object, this method returns true (x = y has a value of true ).
In the first example, "Person" inherits the equals method and is not overwritten. Therefore, it is better to compare whether the object references are the same. --> true is the same, falsep1 is the same, and p2 is created by new, allocate two different spaces in the heap, so false

In the second example, obj1.equals (per1) compares obj1 and per1, Object obj1 = per1; When passing the per1 value to obj1, the two objects point to the same space, returns true;

 

Because an Object is the parent class of all classes, objects of all classes can be received using objects, and objects can also receive any referenced data type.

 

Use Object for interface reception

Interface A {public abstract String getInfo ();} class B implements A // class B implements interface A {public String getInfo () {return hello world ;}} public class TestObject {public static void main (String [] args) {A a = new B (); // instantiate the interface Object obj = a; // use the Object for receiving, transform A x = (A) obj upwards; // transform System down. out. println (x. getInfo ());}}

Receiving arrays using objects

Public class TestObject {public static void main (String [] args) {int arr [] = {1, 3, 4, 5}; // defines the array Object obj = arr; print (obj);} public static void print (Object obj) {if (obj instanceof int []) // determines whether it is an integer Array {int x [] = (int []) obj; // perform a downward transformation to convert an object to an integer array for (int I = 0; I
 
  
1.3 Conclusion
  

1. An Object is the parent class of all classes. As long as it is a referenced data type, it can be transmitted using an Object.

2. objects must be passed up before the downward transformation, and must be determined by instanceof.

2. Packaging

In java, we propose the idea that everything is an object. Then, the basic data type is not an object, and 8 basic data types are packaged into classes.

8 packages

 

For the Number class

 

Number is the direct subclass of an Object. It converts the content in the digital packaging class to the basic data type (unpacking ).

Packing: change the basic data type to a packaging class.

Unpacking: Change the packaging class to the basic data type

Taking Integer as an example:

Public Integer (int value)

Public class TestWapper1 {public static void main (String [] args) {Integer I = new Integer (11); // the construction method of the Integer class, which is equivalent to subclass instantiation, boxed Number num = I; // The int x = num is passed up. intValue (); // call the method of the Number class, unpack }}

Taking Float as an example:

Public class TestWapper2 {public static void main (String [] args) {Float f = new Float (11.1f); // The constructor of the Float class, which is equivalent to subclass instantiation, binning float x = f. floatValue (); // call the method of the Number class, unpack }}

Before JDK1.5, the program packaging class cannot directly use "+-*/", because they are all a class. Then, the function of the packaging class is changed, and automatic packing and unpacking are added. You can also use the packaging class for numerical calculation.

Public class TestWapper3 {public static void main (String [] args) {Integer I = 30; // automatically boxed into Integerint j = I; // automatically split into int }}

In the packaging class, there is a big feature-changing the string to the specified data type.

In integer

ParseInt

Public static int parseInt (String s) throws NumberFormatException

ParseFloat

Public static int parseFloat (String s) throws NumberFormatException

This string must be composed of numbers. The feature of this method is that strings can be passed through input.

Public class TestWapper3 {public static void main (String [] args) {String str1 = 300; // The String must be a String str2 = 300.33; // The string must be all digits int I = Integer. parseInt (str1); float j = Float. parseFloat (str2); System. out. println (multiplication of integers: + I + * + I + = + I * I); System. out. println (decimal multiplier: + j + * + j + = + j * j );}}

Public class TestWapper3 {public static void main (String [] args) {String str1 = new String (args [0]); // The String must be a numeric String str2 = new String (args [1]); // The String must be a numeric int I = Integer. parseInt (str1); float j = Float. parseFloat (str2); System. out. println (multiplication of integers: + I + * + I + = + I * I); System. out. println (decimal multiplier: + j + * + j + = + j * j );}}
2. Anonymous internal class

Definition: A class is only used once in the entire operation. It is defined as an anonymous internal class and developed based on the abstract class and interface.

Interface A {public abstract void print (); // defines the internal abstract method} class B implements A {public void print () {System. out. println (hello java) ;}} class C {public void fun1 () {this. fun2 (new B (); // upload an instance of B and call the interface method} public void fun2 (A) {. print () ;}} public class noName_Inner {public static void main (String [] args) {C c = new C (); c. fun1 ();}}

If Class B is used only once at this time, there is no need to define a class separately.

Interface A {public abstract void print (); // defines the internal abstract method} class C {public void fun1 () {this. fun2 (new A () {public void print () {System. out. println (hello java) ;}}); // transmits an instance of B and calls the interface method} public void fun2 (A a) {. print () ;}} public class noName_Inner2 {public static void main (String [] args) {C c = new C (); c. fun1 ();}}










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.