Java Day 10

Source: Internet
Author: User

Interface applications

Polymorphic
Cat x = new Cat ();
Animal x = new Cat ();

The parent class or interface reference to the object of its child class

1 classbookpc{2      public Static voidmain (string[] Args) {3USEUSB (NewUpan ());4     }5      public Static voidUseusb (USB U) {6 U.open ();7 u.close ();8     }9 }Ten  one classmouse{ a      - } -  the Interfaceusb{ -      public voidOpen (); -      public voidClose (); - } +  - classUpanImplementsusb{ +      public voidOpen () { aSystem.out.println ("upan open"); at     } -      public voidClose () { -System.out.println ("upan close"); -     } -}



Benefits of improved Code reusability

Cons
  pre-defined content cannot use post-subclass-specific content
 
  polymorphism Prerequisites:
 1, must have a relationship, inherit, implement
 2, overwrite
 
Polymorphic-transformation
  Animal x = new Cat ();//automatic type promotion, unique function inaccessible; transition up
  cat C = (cat) x;//downward transition
 
Polymorphism--type judgment instanceof
  pre-transition judgment
 
Polymorphic--member feature compilation See Left of equals
 1, member variable
  Parent class has get, none compile failed
 2, member function
  Subclasses have overrides, none gets the parent class
 3, static function
  static no overwrite, no polymorphism involved, the object is polymorphic, static methods belong to class
 
Inner class-apply to design
  1. The inner class can access the members of the outer class directly, including the private
 2, the external class accesses the inner class, you need to establish the inner class object
 
  After compiling, view the class file
 
Inner Class--modifier
 outer.inner in = new Outer (). new Inner ();
 static Outer.Inner in = new Outer.Inner ();
  When an inner class member has a static adornment, the inner class is also static
 
Details
 outer.this.num
  Why does an inner class have direct access to members in an external class?
  Because an inner class holds an external class that references an external class Name. this
 
Local inner class--method
  internal class accesses local variables, local variables need to be declared final

Anonymous inner class
An inner class must inherit or implement an external class or interface
is actually an anonymous subclass object
New parent class or interface () {subclass content}

Application
When a function parameter is an interface type, and there are no more than 3 methods in the interface, the anonymous inner class can be passed as the actual parameter.

Details
Main function Direct New inner//why
Anonymous inner class if the reference variable is given using the parent class for display, then the compiler fails with the variable to call the subclass Method.

Class with an inherited subclass Object.
1. Default initialization of member variables
2. Call the subclass constructor
3. Call the parent class constructor
4. Member Variable display initialization
5. Construct code block initialization
6. Constructor-specific Initialization

Java Day 10

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.