Java Tour (ix)--object class, Equals,tostring,getclass, inner class access rule, static inner class, Inner class principle, anonymous inner class

Source: Internet
Author: User


Java Tour (ix)--object class, Equals,tostring,getclass, inner class access rule, static inner class, Inner class principle, anonymous inner class

Every day by some trivial harassment, learning to continue

I. Object class

What is Object? We can look through the Java API documentation to see his introduction.




As described above, object is the root class of a class hierarchy, that is, a super class

    • Object: Is the direct inheritance of all objects, the legendary ancestor, your father and father, your father's father and father is a grandfather, this is the inheritance, but your ancestors only one, the definition of this class is definitely the function of all objects
 
class Demo { // extends Object }
1.equals

We can use equals to compare whether two objects are the same

// public class class name
public class HelloJJAVA {
     // public static no return value main method array
     public static void main (String [] str) {
         // compare
         Demo1 demo1 = new Demo1 ();
         Demo2 demo2 = new Demo2 ();
         System.out.println (demo1.equals (demo2));
     }
}

class Demo1 {

}

class Demo2 {

} 

The result will definitely return false




What we should note here is that he compares the memory address

Suppose we need to define a comparative content

// public class class name
public class HelloJJAVA {
     // public static no return value main method array
     public static void main (String [] str) {
         // compare
         Demo demo1 = new Demo (5);
         Demo demo2 = new Demo (8);
         System.out.println (demo1.Comper (demo2));
     }
}

class Demo {

     private int num;

     public Demo (int num) {
         this.num = num;
     }

     public boolean Comper (Demo d) {
         return this.num == d.num;
     }
} 

His return is false, but is it necessary for us to do so?

    • The object class has already provided a comparison of whether the objects are the same, if the custom class also has the same functionality, there is no need to redefine, as long as the function of the parent class, resume its own unique content can be, this is the overwrite, so we have replicated
// public class class name
public class HelloJJAVA {
     // public static no return value main method array
     public static void main (String [] str) {
         // compare
         Demo demo1 = new Demo (5);
         Demo demo2 = new Demo (5);
         System.out.println (demo1.equals (demo2));
     }
}

class Demo {

     private int num;

     public Demo (int num) {
         this.num = num;
     }

     @Override
     public boolean equals (Object obj) {
         // TODO Auto-generated method stub
         return this.num == ((Demo) obj) .num;
     }
} 

This applies a polymorphic downward transformation.




2.toString

Convert to String

// public class class name
public class HelloJJAVA {
     // public static no return value main method array
     public static void main (String [] str) {
         // compare
         Demo demo = new Demo (5);
         System.out.println (demo.toString ());
     }
}

class Demo {

     private int num;

     public Demo (int num) {
         this.num = num;
     }
} 

The result of the conversion ten what?




What value is this?

    • Class name @ hash value

What is a hash value? We can use tohexstring to convert.

System.out.println(demo.toString());System.out.println(Integer.toHexString(demo.hashCode()));




3.getClass

This does not have to say, return to the currently running class, so

// public class class name
public class HelloJJAVA {
     // public static no return value main method array
     public static void main (String [] str) {
         // compare
         Demo demo = new Demo (5);
         / *
          * System.out.println (demo.toString ());
          * System.out.println (Integer.toHexString (demo.hashCode ()));
          * /

         System.out.println (demo.getClass ());
     }
}

class Demo {

     private int num;

     public Demo (int num) {
         this.num = num;
     } 

We'll just go back to the demo here.




There are many ways to do it, like GetName.

System.out.println(demo.getClass().getName());

I got the name of the demo.

Two. Inner class

This is a little bit of knowledge, so let's take a look at the concept

    • One class is defined in another class, and the opposite class is called an inner class (built-in class, nested Class)

    • Access features

      • Inner classes can access members of external classes directly, including private members
      • The outer class must establish an object of the inner class to access the members of the inner class.

Let's write an inner class.

class Outer {
     int x = 3;

     void show () {
         System.out.println ("x =" + x);
         new Inner (). fini ();
     }

     / **
      * Inner class
      *
      * @author LGL
      *
      * /
     class Inner {
         void fini () {
             System.out.println ("Inner Class" + x);
         }
     }
} 

The access rules for internal classes have been shown

    • Inner classes can access members of external classes directly, including private members
    • The outer class must establish an object of the inner class to access the members of the inner class.

So we can not directly access the members of the inner class?

Outer.InnerOuterInner();inner.fini();

This is accessible, but we generally do not, because most of the situation we will be the inner class private

Did you ever think about it? Why do anonymous inner classes have access to external members? We can do a little test like this, defining an X in the inner class is a member variable and a local variable, respectively.

// public class class name
public class HelloJJAVA {
     // public static no return value main method array
     public static void main (String [] str) {
         Outer.Inner inner = new Outer (). New Inner ();
         inner.fini ();
     }
}

class Outer {
     int x = 3;

     void show () {
         System.out.println ("x =" + x);
         new Inner (). fini ();
     }

     / **
      * Inner class
      *
      * @author LGL
      *
      * /
     class Inner {
         int x = 5;

         void fini () {
             int x = 6;
             System.out.println ("Inner Class" + x);
         }
     }
} 

We're outputting this x now, do you know how much it is? The result is obvious, 6.




Now I want to print this 5 how to play? You can use this.




So we want to print this 3? This is the inner class, so we need this outside, just use outer.this.x, the output is 3.




It is possible to directly access members in an external class because the inner class holds a reference to an external class, which is the external class name. This

Three. Static Inner class

When the inner class is in the member position, it can be modified by the member modifier, for example:

    • Private, encapsulating inner classes in the outer class
    • Static, the inner class has the static characteristics, when the inner class is static decorated, only directly access static members of the outer class, there is access restrictions, but static internal classes appear not many, after all, there is access limitations




In the outer class, how do we access the static inner class?

// public class class name
public class HelloJJAVA {
     // public static no return value main method array
     public static void main (String [] str) {
         new Outer.Inner (). fini ();
     }
}

class Outer {
     private static int x = 3;

     / **
      * Inner class
      *
      * @author LGL
      *
      * /
     static class Inner {

         void fini () {
             System.out.println ("Inner Class" + x);
         }
     }
} 

This will allow you to access the

Four. Internal class principles

Let's analyze how the inner class is coming, why it's used

    • When describing a thing, there is something inside the thing, which is described in an inner class, because the inner thing is using external things

The inner class is a direct access to the specific things in the external classes, which are generally used in programming

Five. Anonymous inner class

General internal classes are not implemented by the public, our inner classes can be defined anywhere, or you can do this

class Outer {

     int x = 3;

     void fini () {

         class fini {

             void show () {

                 System.out.println ("Inner Class");
             }
         }

     }
} 

This program, the internal class will run? The answer is no, because there's no object, we're going to give him a new object.

// public class class name
public class HelloJJAVA {
     // public static no return value main method array
     public static void main (String [] str) {
         new Outer (). fini ();
     }
}

class Outer {

     int x = 3;

     void fini () {

         class fini {

             void show () {

                 System.out.println ("Inner Class");
             }
         }
         new fini (). show ();
     }
} 

This gives you access to the inner class defined in the local

    • 1. Can not be modified by member modifiers
    • 2. Members in an external class can be accessed directly because they also hold references in the class, but cannot access the local variable that he is in, and can access only the final decorated local variables

And our anonymous inner class, what is the concept? As the name implies, anonymous inner class, no name.

    • 1. The anonymous inner class is actually the shorthand format for the inner class.
    • 2. The premise of defining an anonymous inner class is that an inner class must inherit a class or implement an interface

Normal logic code

// public class class name
public class HelloJJAVA {
     // public static no return value main method array
     public static void main (String [] str) {
         new Outer (). function ();
     }
}

class Outer {

     int x = 3;

     class Inner extends AdsDemo {
         @Override
         void show () {
             System.out.println ("method:" + x);
         }
     }

     public void function () {
         new Inner (). show ();
     }
}

abstract class AdsDemo {

     abstract void show ();
} 

And now we're going to use anonymous inner classes to simplify the code, what do we do?

// public class class name
public class HelloJJAVA {
     // public static no return value main method array
     public static void main (String [] str) {
         new Outer (). function ();
     }
}

class Outer {

     int x = 3;

     // class Inner extends AdsDemo {
     // @Override
     // void show () {
     // System.out.println ("method:" + x);
     //}
     //}

     public void function () {
         // new Inner (). show ();
         new AdsDemo () {
             @Override
             void show () {
                 System.out.println ("x:" + x);
             }
         };

     }
}

abstract class AdsDemo {

     abstract void show ();
} 

This is anonymous. Inner class

    • Anonymous inner class format: New parent class or interface () {Defines the contents of a subclass}
    • In fact, an anonymous inner class is an anonymous subclass object. And this object is a bit fat, you can also understand him as a content object
    • The method defined in the anonymous inner class should not be more than three

OK, this space is here, our Java Tour This course has been told so much, from the original uncertainty, want to try to write, now has accumulated to the nineth, so summed up, we want to do things still have to try, in case the realization of it?

With like-minded people, welcome Dabigatran: 555974449


Java Tour (ix)--object class, Equals,tostring,getclass, inner class access rule, static inner class, Inner class principle, anonymous inner class


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.