JAVA 9 (inner class, exception, package)

Source: Internet
Author: User
Tags throw exception

Inner class:A class is defined in another class, which is called an inner class.
Example:   class zx{ public static void Main (String []arr)  {outer  o = new Outer (); O.method ();   }} class outer{ class Inner//inner class, you can define multiple internal classes  {  void function ()   {    System.out.println ("inner:" +x); } } int x=3; void method ()  {  System.out.println (x) ;  Inner in = new Inner ();  in.function (); }}    direct access to members in the inner class:  can be written in main () Outer.Inner in = new Outer (). New Inner ();  //internal classes can be privatized, external classes cannot, and cannot be written after privatization.   Internal class access Rule 1, the inner class can directly access members in the outer class, including private.         It is possible to access members in an external class directly because the inner class holds a reference to an external class in the form of an external class. this. Variable name. 2, the external class must establish an inner class object to access the inner class.   Access format: 1 when an inner class is defined on the member position of an external class and is not private, it can be in an external other class. You can create an inner class object directly, in the form outer.inner in = new Outer (). New inner ();  2, when the inner class is in the member position, it can be decorated with the member modifier.         For example, private: Encapsulates internal classes in the outer class         static: Inner classes have static properties. Non-static variables cannot be accessed, only static members in external classes can be accessed, and precedence over the existence of objects.     inner class definition principle when describing a thing, there is something inside the thing, which is described by an inner class, because the internal thing is used externallyThe content of the thing.  class Body{private class xinzang {  }public void Show ()     {        NE W Xinzang ();   }}     defines an inner class within a method, that is, not a member location1, can not be static and private decoration, can still access the external class member variable, because also holds the external class reference class zx{public static void Main (String []arr) {new Outer (). method ();}} class  outer{int x=3; void method () {class inner//can not be statically and privately decorated and can still access the outer class member variable {void function () {System.out.println (x); }} new Inner (). function (); }} 2, but cannot access the local variable that he is in, can only access the part that is not seen by the final decoration, such as int y in method, not with Access Y Anonymous Inner class  1, in fact, is the internal class shorthand format. 2, the premise of defining an anonymous inner class     The inner class must inherit a class or implement an interface. 3, Anonymous inner class format: New parent class or interface () {Defines the contents of a subclass} 4, in fact, the anonymous inner class is an anonymous subclass object, and the object is a bit fat, with content objects.  5, it is best not to have more than three methods defined in an anonymous inner class.    Example Original:    class zx{ public static void Main (String []arr)  {new Outer (). function () ;  }} abstract class a{ abstract void Show ();}  class outer{ int x=3; class Inner extends a {  void Show ()   {  System.out.println (" Show "+x); } } public void function ()  {  new Inner (). Show (); } }  After modifying anonymous: class Zx{ public static void Main (String []arr)  {new Outer (). function ();  }} abstract class A { abstract void Show ();}  class outer{ int x=3;  public void function ()  {  new A ()//a anonymous subclass object   {    void Show ()    {    SYSTEM.OUT.PRINTLN ("show" +x);   } }.show (); }}     anonymous Inner class practice    class zx{ public STAtic void Main (String []arr)  {test.function (). method ();//complement code, run normal  }} interface inter{ void Method ();}   class test{ /* static Inter  function ()  {  return new Inter ()   {   public void Method ()    {    SYSTEM.OUT.PRINTLN ("111");   } };  } * /}    ExceptionException: An abnormal condition occurs when the program is running. Exception Origin: The problem is described in Java, encapsulated into an object.        For serious: Java is described by the error class.        For error, it is not common to write targeted code to handle the very serious: Java is described by the exception class.   For exception can provide targeted code to handle Throwable//Parent Exception Handling: JAVC provides special statement processing, format: try{need to be detected code}catch (Exception class variable) {processing mode}finally{will execute the statement} Throws Exception//throw problem Multiple exception handling  Multiple catch (x e), X is a subclass of Exception, and the parent class should be placed at the end to facilitate the precise identification of the exception type catch (Exception e) {} Custom exception: The unique problem, Java does not encapsulate the exception, to customize. In general, there is an exception inside the function, either Try{}catch () {} inside the function or throws exception in the function header.  For example, when the divisor is a negative number, it is considered an exception. Class zx{public static void Main (String []arr) throws Fushu \\!! {System.out.print (New Demo (). Div (3, 44));}} Class Fushu extends exception{private string msg;void Fushu (String msg) {this.msg=msg;} public string GetMessage () {return   msg;} }class demo{int div (int a,int b) throws Fushu {if (b<0) throw new Fushu ("negative case");//Manually throw exception object through throw//!! Custom exception return A/b; }} PackageThe original management of the class file can have a multi-tier package directory   package org.crazyit.net; The class is stored in the file  org.crazyit.net  classes in different packages access each other. The classes in the package that are accessed and the members in the class require the public adornment   subclasses of the different packages to also have direct access to the members of the parent class that are protected decorated   packages have only two direct permissions to the package, public protected, and the default   permissions are differentiated.                          ,         &NB Sp    ok
&NBSP; pub lic  portected default (default) private
ok ok ok ok
ok ok ok &NBSP;
subclass ok &NBSP; &NBSP;
ok &NBSP; &nbs P; &NBSP;
Only one Java file can produce a public class package. class name = new class name classes in different packages can also inherit, create objects, and so on, ZXZX; In this package public class Zx {//class must be public to be externally accessible public void Show () {System.out.println ("show ran");//function required Must be public to be externally accessed}} public class arraytool{private Arraytool () {} public static void Main (String [] arr) {zxzx. Zx a = new zxzx. Zx ();  Package. Class name is the new class name A.show (); }}   ImportTo simplify the property zxzx of the class name. Zx a = new zxzx. Zx (); Package. The class name is the new class name Improt zxzx. ZX; the ZX Import in package ZXZX can be directly written in this class ZX A = new ZX (); can also write Improt zxzx.*; Import all classes in the ZXZX package.

JAVA 9 (inner class, exception, package)

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.