Java Internal class

Source: Internet
Author: User

Reference Http://how2j.cn/k/interface-inheritance/interface-inheritance-inner-class/322.html#nowhere

Non-static inner class

Non-static inner class Battlescore "Combat score"
Non-static inner classes can be defined directly within a class

Like what:
Combat performance only makes sense when a Hero object is present.
So when instantiating battlescore, it must be built on the basis of an existing hero.
Syntax: New External Class (). New inner Class ()
As a non-static inner class of hero, the private instance property of the external class can be accessed directly by name.

Package charactor; Public classHero {PrivateString name;//name     floathp//Blood Volume     floatArmor// Armor     intMovespeed;//Moving Speed//non-static inner class, only when an outer class object exists, it makes sense .//combat performance only makes sense when a Hero object is present.    classBattlescore {intkill; intDie ; intassit;  Public voidLegendary () {if(Kill >=8) System. out. println (name +"Super God! "); ElseSystem. out. println (name +"not yet super God! "); }    }      Public Static voidMain (string[] args) {Hero Garen=NewHero (); Garen.name="Galen"; //instantiate an inner class//Battlescore objects are only meaningful when a Hero object exists.//so its instantiation must be based on an external class objectBattlescore score = Garen.NewBattlescore (); Score.kill=9;    Score.legendary (); } }
Static Inner class

Declare a static inner class inside a class
For example, the enemy crystal, when the enemy crystal without blood, all the heroes have won, not just a specific hero to win.
Unlike non-static inner classes, the instantiation of a static inner class Crystal class does not require an instance of an external class to be used as the basis for a direct instantiation of the
Syntax:new External class. Static inner Class ();
Because there is no instance of an external class, the instance properties and methods of the outer class cannot be accessed inside the static inner class
In addition to accessing private static members of an external class, there is no big difference between a static inner class and a normal class .

Package charactor;  Public classHero { PublicString name; protected floatHP; Private Static voidBattlewin () {System. out. println ("Battle Win"); }         //the enemy's crystal.    Static classenemycrystal{inthp= the; //if the crystal's blood volume is 0, declare victory.         Public voidcheckifvictory () {if(hp==0) {Hero.battlewin (); //static inner classes cannot directly access object properties of external classesSystem. out. println (name +"win this game"); }        }    }          Public Static voidMain (string[] args) {//instantiate a static inner classHero.enemycrystal Crystal =Newhero.enemycrystal ();    Crystal.checkifvictory (); }  }
Anonymous class

An anonymous class refers to instantiating a class at the same time it is declared , making the code more concise and refined
Typically, to use an interface or an abstract class, you must create a subclass

Sometimes, for quick use, instantiate an abstract class directly and implement its abstract method "on the spot".
Since the implementation of the abstract method, then is a new class, just this class, not named.
Such a class, called Anonymous class, Another anonymous class explanation

Package charactor;  Public Abstract classHero {String name;//name              floathp//Blood Volume              floatArmor// Armor              intMovespeed;//Moving Speed           Public Abstract voidattack ();  Public Static voidMain (string[] args) {Adhero adh=NewAdhero (); //by printing the ADH, you can see that the Adh object belongs to the Adhero classAdh.attack (); System. out. println (ADH); Hero h=NewHero () {//implement attack method on the spot             Public voidattack () {System. out. println ("new offensive means.");        }        };        H.attack (); //by printing h, you can see h this object belongs to the Hero$1 class name that is automatically assigned by this systemSystem. out. println (h); }      }
Local class

Native classes can be understood as anonymous classes with names
Unlike an inner class, an inner class must be declared at the location of the member, which is equal to the property and method.
Local classes, like anonymous classes, are declared directly inside the code block, which can be the main method, the for loop, and so on.

Package charactor;  Public Abstract classHero {String name;//name              floathp//Blood Volume              floatArmor// Armor              intMovespeed;//Moving Speed           Public Abstract voidattack ();  Public Static voidMain (string[] args) {//Unlike anonymous classes, the local class has a custom class name        classSomehero extends hero{ Public voidattack () {System. out. println (name+"new offensive means."); }} Somehero H=NewSomehero (); H.name="The Ground BU Division";    H.attack (); }      }
Using an external local variable in an anonymous class

Using an external local variable in an anonymous class, the external local variable must be decorated as final

Why it is more complex to declare as final, refer to the explanation in the second hero code

Note: In Jdk8, there is no need to force decoration into final, if not written final, will not error, because the compiler secretly to help you add the invisible final

Hero.java Code

Package charactor;  Public Abstract classHero { Public Abstract voidattack ();  Public Static voidMain (string[] args) {//using an external local variable in an anonymous class, the external local variable must be decorated as finalFinalintDamage =5; Hero h=NewHero () { Public voidattack () {System. out. printf ("new offensive means, resulting in%d damage.", damage);     }        }; }      }

Abstract Hero Code

Package charactor;  Public Abstract classHero { Public Abstract voidattack ();  Public Static voidMain (string[] args) {//using an external local variable in an anonymous class damage must be decorated as final        intDamage =5; //the native class Anonymoushero is used here to emulate the hidden property mechanism of anonymous classes//In fact, an anonymous class declares an damage property in an anonymous class, and initializes the value of the property using the constructor method//The damage used in attack is really using this internal damage, not the external damage//assume that an external attribute does not need to be declared final//then modifying the value of damage in attack will be implied as modifying the value of the external variable damage//but they are different variables, it is impossible to modify the external variable damage//therefore, in order to avoid misleading, the external damage must be declared final, "look" can not modify the        classAnonymoushero extends hero{intdamage;  PublicAnonymoushero (intdamage) {                 This. Damage =damage; }             Public voidattack () {damage=Ten; System. out. printf ("new offensive means, resulting in%d damage.", This. Damage); }} Hero H=NewAnonymoushero (damage); }      }

Java Internal class

Related Article

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.