Dark Horse Programmer-object-oriented objects and inner classes

Source: Internet
Author: User

--java Training, Android training, iOS training,. NET training, look forward to communicating with you! ——-

1. Object: is the parent class of all classes, direct or indirect, with the functionality that all objects have.

2, Inner class: will be defined in a variety of other classes inside, the inner class is called the inner class.

▲ access rules for internal classes:

    1. Inner classes can directly access members in external classes, including private.

    2. To access an inner class, an external class must establish an object of the inner class.

    3. Inner classes have direct access to members in external classes because there is a reference to an external class in the inner class, in the form of an external class name.

▲ Access format:

    1. When an inner class is defined on an external class member location, it is not private, and an inner class object can be created directly in the outer other class.
      The outer class name. Internal class Name Variable name = External Class object. Inner Class object
      Outer.Inner o = new Outer (). New Inner ();

    2. When an inner class is defined on a member position, it can be modified by the member modifier:
      ? Private: Encapsulates inner classes in the outer class.
      ? Static: An attribute with static in the inner class.
      When an inner class is modified by static, only static members in the outer class can be accessed, with access limitations.
      In other external classes, direct access to non-static members of the static inner class:
      New. Outer.Inner (). function ();
      In other external classes, direct access to non-static members of the static inner class:
      Outer.Inner.function ();
      Note: When a static member is defined in an inner class, the inner class must be static.

▲ when describing the interior of a city, there is something inside the thing that uses an inner class to describe it because the internal transaction is using external transactions.

The inner class is defined locally and cannot be decorated by the member modifier, and can be accessed directly from the members of the outer class, because it also holds references in the outer class, but cannot access the local variables in which it resides, and can access only the final decorated local variables.

3. Anonymous inner class

    1. The anonymous inner class is actually the shorthand format for the inner class.

    2. Defining an anonymous inner class premise: An inner class must inherit a class or interface.

    3. The format of the anonymous inner class:

      new.父类或借口(){    定义子类内容} 函数
    4. An anonymous inner class is an anonymous subclass object.
Class outer{Private Staticint x =3;Static  class Inner {        Static void  function() {System.out.println ("inner:" +x)}}Static  class Inner2 {        voidShow () {System.out.println ("Inner2 Show")}} publicStatic voidMethod () {Inner.function();    New.inner2 (). Show (); }} class innerdemo{     Public Static voidMain (string[] args) {Outer.method (); }}

Dark Horse Programmer-object-oriented objects and inner classes

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.