interfaceEquivalent to an implementation class object that defines the interface and overrides all the abstract methods in the interfaceSportable s=new sportable () {public void Sport () {System.out.println ("playing basketball");}};S.sport ();System.out.println (S.getclass ());//class com.aowin.noname.test$3}}Entity classClass person{public void Eat () {System.out.println ("eat");}}
Java-8.6 anonymous internal class
In this section, we will discuss anonymous internal classes.
We have mentioned the internal class as follows:
package com.ray.ch03;public class Test {private
Anonymous classes cannot be referenced because they cannot have names. They must be declared as part of the new statement at creation. This requires another form of new statement, as shown below: new Java code
Copy codeThe Code is as follows: interface pr
{
Void print1 ();
}
Public class noNameClass
{
Public pr dest ()
{
Return new pr (){
Public void print1 ()
{
Anonymous inner class: we can use an inner class to create an object with no name and use it to access members in the class. The main purpose is to simplify the program code and to complement the method that is not defined in the inner class.public class app8_19{public stati
1, first anonymous inner class to inherit from the abstract base class or implement the base class interfaceLike thisAbstract class Seed{int Cnt;public Seed (int x) {cnt=x;} abstract void Showinfo ();} Class Apple{int Num=300;publ
();}------------------------------------------------------------------------------------------------------------/** Second way: Implement Runnable interface* (1): Create a class that implements the Runnable interface for this class* (2): Rewrite the Run method* (3): Create objects of this class* (4): Creates an object of the thread
The anonymous class in Java has a magical usage, as shown in the following code example: Package contract; Public Interface Isay { void SayHello ();}Above is a simple interface, below is how to use: Package Jimmy; Import contract. Isay; Public class Program { publicstaticvoid main (string[] args) { N
Java anonymous inner classes usually have no object variable points. It can only be used onceFor example, the following code, to use the abstract class people eat also inherits and replicates the method eat way, very cumbersome. The same situation applies to interfaces as well.Package com.lubby.nosynchronizedtest; Public Abstract
anonymous object NewWoman (). AA ("LOVEU1"); }}/*** Anonymous object: Defines an object with no Name, features: can only be used once (directly in the heap to open up memory space, used to become a garbage object, collected by GC)*/classwoman{ PublicString name; Public intAge ; Public voidAA () {System.out.println ("Aa1"); } Public voidAA (String name) {System.out.println ("AA2:" +name); }
defined. Besides these, it does not seem to be any different from a normal class.The following example looks a little strange:
//: Parcel6.java//A method This returns an anonymous inner class package c07.innerscopes;
public class Parcel6 {public Contents cont () {return new Contents () {private int i = 11
Inner classes usually contain callbacks, and the function that references that anonymous inner class is gone, so the inner class references the outside local variable to be final, so that the variable is found when the callback is made, and if the member variable of the perimeter class does not need to be final, Becaus
Learning notes: Because the java Anonymous class learns some minor usage of interfaces, learning notes javaI found a lot of interesting code when looking at the CometD sample code. But to be honest, it is really tiring to look at other people's code. So let's record this point. First look at a piece of code: Code 1 contains a new operation in the Code and is carr
not just at this point.Here's a more bizarre example: Public classtestinnerclass{Private voidIntoBooleanb) { if(b) {classgdestination {PrivateString label; Publicgdestination (String whereto) {label=Whereto; } PublicString Readlabel () {returnlabel; }} gdestination Destination=NewGdestination ("Beijing"); System.out.println (Destination.readlabel ()); } } Public voidDestBooleanb) {into (b); } Public Static voidMain (String args[]) {testinnerclass inn
constant pool is an ordered set of constants used for this type, including actual constants (string, integer, and floating point constants) and pair types, symbol reference of fields and methods. The data items in the pool are accessed through indexes like array items. because the constant pool stores symbolic references of all types, fields, and methods used by a type, it plays a core role in Dynamic Links of Java programs.
2) Field Information:
class reference to the subclass instance of new, so what happens to the interface?Example 3: Anonymous class implementation interfaceFirst define an interface public interface Human {public void eat ();p ublic void Drink ();}public class Unnamedclass {public static void main (string[] args) { As you can
internal classes
1. Basic internal theory:
Java internal class: the definition of an internal class is defined within another class. The reason is:1) An internal class object can access the implementation of the object created for it, including private data. That is, an
anonymous inner class implementation of the instance 4:thread class
public class Demo {public static void main(String[] args) {Thread t = new Thread() {public void run() {for (int i = 1; i 5; i++) {System.out.print(i + " ");}}};t.start();}}
Operation Result: 1 2 3) 4 5An
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.