Advanced features of the 9th Chapter class
First, abstract class
Abstract classes only declare the existence of a method and cannot be instantiated, which means that an abstract class cannot create its object, and the abstract class is preceded by the Abstraction keyword when it is defined.
1 /*defines an abstract class fruit, and defines its abstract method2 * Implement this abstract method in its subclasses3 */4 Abstract classFruit {5 PublicString color;6 PublicFruit () {7Color= "Green";8 }9 //define an abstract methodTen Public Abstract voidHarvest (); One } A - Public classFarm { - Public Static classAppleextendsfruit{ the @Override - Public voidHarvest () { -System.out.println ("Harvest apples! "); - } + } - + Public Static voidMain (string[] args) { AApple apple=NewApple (); at System.out.println (apple.color); - apple.harvest (); - } -}
Second, the internal class
A class is then defined in a class, that is, an inner class. The inner class can directly access the private member variables of the outer class.
1, members of the internal class:
1 Public classOuterclass {2Innerclass in=NewInnerclass ();//instantiating an inner class in an external class3 Public voidOuf () {//internal class methods are called through an inner class instance object in an external class method4 In.inf ();5 }6 classinnerclass{//Defining inner Classes7Innerclass () {//Inner class construction method8 }9 Public voidInf () {//Inner class member methodsTen } One inty=20;//defining internal class member variables A } - PublicInnerclass doit () {//defines an external class method that returns a type of internal class reference - //y=4; //internal class member variables cannot be accessed directly from external class methods theSystem.out.println ("Output from the Mark0:doit method" +in.y); -in.y=4;//must be accessed through an internal class instance object - return Newinnerclass (); - } + Public Static voidMain (string[] args) { -Outerclass out=NewOuterclass (); +Outerclass out1=NewOuterclass (); AOuterclass.innerclass in=Out.doit (); at //instantiate an inner class object in by the Doit method - //An object instantiation operation for an inner class must be implemented in a non-static method of an external class or external class -System.out.println ("Output from the Mark1:out.in method" +out.in.y); -System.out.println ("MARK2: Output of direct in--?? Why is the output here 20?? " +IN.Y);//Why is the output here? - -Outerclass.innerclass INA=OUT1.Newinnerclass (); in //or call the constructor of an inner class from an instance object of an external class -System.out.println ("Output from the Mark3:out1.in method" +out1.in.y); toSystem.out.println ("MARK4: Output of direct ina" +ina.y); + } -}
* If a member variable of the same name is defined in an external class and in an inner class, it can be specified by the This keyword.
1 Public classThesamename {2 Private intx;3 Private classinner{4 Private intX=9;5 Public voidDoitintx) {6x + +;//Parameter x of the Calling method7 This. x + +;//call the variable x of the inner class8Thesamename. This. x + +;//call the variable x of the outer class9 }Ten } One}
2. Local inner class
A local inner class is an inner class that is defined in a method of a class. Correspondingly, the scope of the local inner class is also within the body of the method.
1 Public classSelloutclass {2 PrivateString name;//Defining member Variables3 PublicSelloutclass () {//Defining Constructors4Name= "Apple";5 }6 Public voidSellintPrice) {//Defining member Methods7 classapple{//defining local inner classes8 intinnerprice=0;//member variables for local inner classes9 PublicApple (intPrice) {//construction method of local inner classTenInnerprice=Price ; One } A Public voidPrice () {//member methods for local inner classes -System.out.println ("Start selling Now" +name); -SYSTEM.OUT.PRINTLN ("Unit Price:" +innerprice+ "Yuan"); the } - } -Apple apple=NewApple (price);//instantiate a local inner class -Apple.price ();//methods to invoke local inner classes + } - Public Static voidMain (string[] args) { +Selloutclass sample=NewSelloutclass ();//To create an instantiated object ASample.sell (100);//methods for calling Selloutclass through this object at } - -}
3. Anonymous inner class
Sometimes the inner class can be not named according to the situation, but directly through the creation of the class instance object to implement, that is, in the instance object of the body writing code.
Because the anonymous inner class does not have a name, it uses the default construction method to generate an object of the inner class. And you need to add a semicolon after the anonymous inner class definition ends.
1 Public classoutstring {2 3 Public Static voidMain (string[] args) {4 FinalString sourcestr= "Ji Lin province!";5Istringdeal s=NewIstringdeal () {//writing anonymous inner classes6@Override//methods for overriding interfaces7 PublicString Filterblankchar () {8String convertstr=Sourcestr;9Convertstr=convertstr.replaceall ("", "" ");Ten returnconvertstr; One } A};//Note that you need to add semicolons here -System.out.println ("source string:" +sourcestr); -System.out.println ("converted string:" +S.filterblankchar ()); the } -}
1 // Defining Interfaces 2 Public Interface istringdeal {3 Public String Filterblankchar (); 4 }
4. Static internal class
Static members can be defined in static inner classes (not static inner classes), while static inner classes cannot use non-static members of external classes.
5. Inheritance of inner class
As with normal classes, inner classes can also be inherited. When a class inherits an inner class, it must be forced to the class with a parameter construction method, and the constructor method's parameters
Must be a reference to the outer class of the inner class.
class a{ class b{ }}// Create inner class Bpublicclass for a Extends a.b{ public Outputinnerclass (a a) { A. Super (); }}
"Java from Beginner to Mastery" Chapter Nineth study notes