First, why to use the internal classWhy use internal classes? In "Think in Java" There is a sentence: the most appealing reason for using inner classes is that each inner class can inherit one (interface) implementation independently, so no matter whether the perimeter class
A simple introduction to anonymous inner classes in the Java Learning Note 25 Preliminary cognitive Java class, but there are a lot of other details in the inner class, so this blog is derived. In this blog you can learn about the
classprivate int i = 2 public void PrintI () { System.out.println (i); } }} The main functions are:void Main (string[] args) { new Outer (0); Outer.privateinnergeti (); Outer.publicinner Publicinner = Outer. new Publicinner (); Publicinner.printi ();} The result of the operation is:02Summarize the points through this example:1, the member inner class is d
create another class in one class, called the member inner class. This member inner class can be static (with the static keyword decorated), or it can be non-static. Since static internal classes are defined and used, there are va
interface only solves some of the problems, while the inner classes make multiple inheritance solutions more complete.To illustrate: Public interface Father {} Public Interface mother {} Public class Son implements Father, mother {} Public class daughter implements Father{ class mother_ implements mother{}
Java internal classes and their instantiationIn Java, it is allowed to define another class within a class (or method, statement block), called an inner class (Inner
inner class method can access the data in the scope where the class definition resides, including private data2: Inner classes can be hidden from other classes in the same package3: Using anonymous inner classes is convenient when you want to define a callback function and
private.An external class must create an object to access the members of the inner class.Package cn;/** * Internal class overview: * Class is defined inside other classes, and this class is called an inner
(); Outer.Innerinch= OT.NewInner ();//// through. New to create a reference to an inner class, notice how the inner class reference is declaredOt.print (inch); }}(2). For nested classes with static adornments, the situation is different. With the example above we can see
classOuter {Private intAge = 20; classInner { Public voidGetage () {System.out.println (age); } } Public voidmethod () {//symbol not found//getage ();Inner Inner=NewInner (); Inner.Getage();}} Public class Test { publicstaticvoid main (string[] args) { new Outer (). New Inner (); Inne
() {System.out.println (j); modifier, otherwise compile error System.out.println (i);//output local inner Class I, The variables in the external method can only be called SYSTEM.OUT.PRINTLN (localinnerclass.this.i) in the form of a parameter, or//the output is i}}inner Inner = new
Writing a multithreaded program today, discovering that inside a method defines an inner class, if the inner class calls the variable in the method, then the variable must be declared as the final type, baffled, and then thought of as the cause of the life cycle, because the variable defined in the method is a local va
original variable is changed in the method, and the local inner class is changed, there is no guarantee that the copy is consistent with the original variable (so: they should have been the same variable.)Bottom line: The rule is a helpless one. It also shows that the design of programming language is limited by the implementation technology. This is one example
(); Outer.Innerinch= OT.NewInner ();//// through. New to create a reference to an inner class, notice how the inner class reference is declaredOt.print (inch); }}(2). For nested classes with static adornments, the situation is different. With the example above we can see
); void set () {in ' = new in (); In.show ();}} Class Hello{public static void Main (string[] args) {//out out = new Out ();//out.display ();//out.set ();/* Directly accesses members of the inner class. */new out (). New in (). Show (); Out.in in = new Out (). New in (); In.show ();}}Access format:When an inner
;interface Product{public Double getprice ();p ublic String getName ();} public class Anonymoustest{public void Test (Product p) {System.out.println ("purchased a" + p.getname () + ", took out" + p.getprice ());} public static void Main (string[] args) {anonymoustest ta = new Anonymoustest ();//Call the test () method, you need to pass in a product parameter,// An instance of its anonymous implementation class
). But the static inner class can have it all. This is the second difference between the two.
local Inner classYes, the Java inner class can also be local, and it can be defined within a method or even a block of code.
Co
corresponding object to invoke the interface, this process is called an interface callback for the object functionCross-class references1) If a is a reference to Class A, then a can point to an instance of Class A. Or the one that points to class ASub-class, which is a case
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.