Java Small summary from Home (2.4): The knowledge of class is leaked (internal class)

Source: Internet
Author: User
Tags getbase

12     首先,这是一篇自己用的文章,不对读者承担任何责任,所以,要带着批判的眼光来看下面的文章

  

1 发现了,得加上输出结果,怕自己出错,~~

What does this article say?

This article is my recent No. 8.6 in programming thinking code, from the source of the 0~200 page close to here, the following is the summary of the knowledge of this 0~200 page, related to the interface, internal classes. Initialization, numerical calculation of some details. This article will not be finished at once, It may be possible to replenish it every other day. Because the code is really a bit more.

Attention

1 My comments are not necessarily correct (but the headings and the code must be correct, because it is the words in the book, but the comments are not necessarily correct), if you are sure of my content, you may lose a lot, because I am just a rookie, I just to remedy some knowledge, if not copy (preferably on their own) Code to run once, may be misunderstood by my comments in the code, welcome comments and Comments ~ ~ ~ Your comments and comments are my motivation to update (to facilitate my reading of programming ideas faster)

This article is suitable for readers

   Suitable for people with a certain programming foundation, because my code may be wrong, suggest that 0 basic people look for a book first, or be sure to copy my code to run, otherwise, the consequences will be disastrous  < ( ̄▽ ̄) > Wow haha ... Because I'm just a rookie.

One more reminder.

The following comments are just my personal summary.
My comments are not necessarily correct (but the headings and the code must be correct, because they are the exact words in the book, but the comments are not necessarily correct)
, but the word order of the comments messy various smallpox paraquat did not say.!!!! Not the exact words of the book!!!! So it's best to copy my Code, run it again, or delete my comments, or hint me what to say and what to say.

Once again, thanks.

1 Thank you for taking your time to read the following, the collision of thinking is the premise of wisdom. Thank you for reading and you are willing to copy my code and read my strange and ugly comments to think of the collision!~!~

Body:

An inner class for an external class, like a defined relationship, an external class, for an inner class, like an inherited relationship.

1. Creation of an inner class object

It is common to rely on external class objects to use
1. Define an external class object p
The first p.new inner class pairs

Package Test.java;public class Parcel2{class contents{private int i=11;public int value () {return i;}} Class Destination{private string Label;destination (String whereto) {label = Whereto;} String Readlabel () {return label;}} Also create two methods to get the object, implement the Polymorphic public Destination to (String s) {return new Destination (s);} Public Contents Contents () {return new Contents ();} public void ship (String dest) {Contents c = Contents ();D estination d = to (dest); System.out.println (D.readlabel ());} public static void Main (string[] args) {Parcel2  p = new Parcel2 ();p. Ship ("Tasmania"); Parcel2 q = new Parcel2 ();//can be two such regression parcel2.contents C = q.contents ();  Parcel2.destination d= q.to ("Borneo");} }

3: Introduction to this and. New

Package Test.java;public class Dotthis{void F () {System.out.println ("donthis.f ()");} public class Inner{public Dotthis outer () {return dotthis.this;  This is the Innner this}public void Haveatry () {//this.f ();D otthis.inner.this.f ();} void F () {System.out.println ("Inner This");}} Public Inner Inner () {return new Inner ();} public static void Main (string[] args) {dotthis dt = new Dotthis ();//Note//dotthis.inner dt = new Dotthis (). New Inner ();//This is can dotthis.inner DTI = Dt.inner ();//Returns an object of the inner class//to the DTI inner//here it is also possible to prove that the inner class must rely on an object of an outer class to create the object//But it's strange//If so, '// Create an object of the inner class,  dti.outer (). f ();//But this is the return of the inner, which is called the F () method of the external class, that is, the external class of Dti.haveatry ();//This proves that DTI is an object of the inner class} }

DONTHIS.F () Inner this

4. Define the inner class in the method

Package Test.java;interface Destination{}public class Parcel5{public Destination Destination (String s) {  // The method defines the inner class Pdestination implements destination{   //This class is part of the method, not part of the class private String label;//cannot be accessed outside the method PD class Private Pdestination (String whereto) {label = Whereto;} Public String Readlabel () {return label;}} return new Pdestination (s);  Returns an inner class object}public static void Main (string[] args) {Parcel5 p= new Parcel5 ();D estination d = p.destination ("ABC");   can also be assigned a value}}

Inside the 5.if inside class

Package Test;interface Destination{}public class Parcel6{private void Internaltracking (Boolean b) {if (b) {class Trackingslip{private String ID; Trackingslip (String s) {ID =s;} String Getslip () {return id;}} Trackingslip ts = new Trackingslip ("Slip"); String s = ts.getslip (); System.out.println (s);//define  and use inner classes in if inside class completely man ~ ~ ~  indicates that the inner class is also just a variable type (special)}//trackingslip ts = new Trackingslip ("X Tracking type cannot parse}public void track () {internaltracking (true);} public static void Main (string[] args) {Parcel6 p = new Parcel6 ();p. Track ();}}

Slip

6. Anonymous Inner class

New Parent class Interface () {    //Note here overrides the method of the parent class.} semicolons cannot be lost;

Package Test;public class Parcel8{public wrapping warpping (int x) {//Set a parameter here, pass it over when the constructor's from parameter return new wrapping (x)// If there is a parameter in the high rise, just do so. {public int value () {return super.value () *47;  Anonymous inner classes can only write a method with the same name for local overrides, the invocation of an anonymous inner class method//must depend on a class, and must also depend on a  method that returns the object of the Class}}; public static void Main (string[] args) {Parcel8 p  = new Parcel8 (); Wrapping W = p.warpping (10);//This returns an object System.out.println (W.value ()) that is clearly worth being an anonymous inner class;}} Class Wrapping{private int i;public wrapping (int x) {i  =x;} public int value () {return i;}}

470

7. The number of assignments within the inner class must be final

Package Test.java;public class Parcel9{//public Destination Destination (final String dest) {public Destination Destination (String dest) {return new destination () {private String label = dest;//when assigning values to an inner class, be sure to change the parameter of the assignment to the final public String R Eadlabel () {return label;}};} public static void Main (string[] args) {Parcel9 p = new Parcel9 ();D estination d = p.destination ("ABC");}} Class Destination{private String label;destination () {}}

8. How to use internal classes

New parent class () {  //Note this is like a new class, and the method cannot be called.    The calling method is   {     //initializing the domain.    }};

Package Test.java;abstract class  base{public base (int i) {System.out.println ("Base constroct, i =" +i);} public abstract void F ();} public class Annoymousconstructor{public static Base getbase (int i) {return new base (i) {//First new constructor { System.out.println ("Inside instatnce initializer")//in the main {} static{} to invoke the method--~~~ the Halo dish}public void f () { System.out.println ("in anoymous f ()");} The f () method does not implement an error because it is an abstract method}; public static void Main (string[] args) {Base base = GetBase (); Base.F ();}}

8. Anonymous inner class implementation factory method

Package Test.java;interface service{void method1 (); void Method2 ();} Interface Servicefactory{service GetService ();} class Implementation1 implements service{//when declared as abstract class, Will not produce wonderful behavior//privat/e implemention1{}//public void Method1 ();//public vo Private Implementation1 () {} public void Method1 ( {System.out.println ("Implements Method"),} public void Method2 () {System.out.println ("Implements method2");}//stati C method public static Servicefactory factory = new Servicefactory () {public Service GetService () {return new Implementatio N1 ();  } };} Class Implementation2 implements service{private Implementation2 () {} public void Method1 () {System.out.println ("Implem Entation2 method1 ");  } public void Method2 () {System.out.println ("Implementation2 method2");} static method public static Servicefactory factory = new Servicefactory () {public Service GetService () {return new imple Mentation2 (); } }; } public class factories{public static void Serviceconsumer (servicefactory fact) {SErvice s = fact.getservice (); S.method1 (); S.METHOD2 (); } public static void Main (string[] args) {//In order for others not to drop easily the object that makes the class, use static to return the object, anonymous inner class, no longer the call Serviceconsumer of the object that created the class (Implemen Tation1.factory); Serviceconsumer (implementation2.factory);} }

9. You can define the inside of the class in the interface.

Package Test.java;public Interface Classiniterface{void howdy (); class Test implements Classiniterface{public void Howdy () {System.out.println ("howdy!");} public static void Main (string[] args) {new Test (). Howdy ();   Because the interface is automatically promoted to public and static, the  variable is added as final,//This proves that the interface should be able to define the class, and the implementation class, but can not specify the method body}}}

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.