Dark Horse: Object-oriented 7

Source: Internet
Author: User
Tags try catch

I. Internal access rules
1. Internal class definition:
A. The internal class can directly access members in the external class, including private. The reason for direct access to Members in the external class is that the internal class holds an external class reference in the format of: External class name. This.
B. to access an internal class, an internal class object must be created. Format:
External class name. Internal class name variable name = External Class Object. Internal class object;
Outer. Inner in = new outer (). New inner ();
2. When the internal class is on the member variable, it can be modified by the member variable modifier.
For example, private is used to encapsulate internal classes in external classes.
Static: Internal classes have static features. After the internal class is modified as static, you can only directly access static members in the external class, and access restrictions are imposed.
In other external classes, you can directly access non-static members of the static internal class: New outer. New inner (). function ();
In other external classes, you can directly access static members of the static internal class: outer. Inner. function ();
3. When a static member is defined in an internal class, the internal class must be a static class.
When a static member of an external class wants to access an internal class, the internal class must be a static class.

Class outer {private int x = 3; Class inner {int x = 4; void function () {int x = 6; system. out. println ("inner:" + outer. this. x) ;}} void method () {inner in = new inner (); In. function () ;}} public class innerclassdemo01 {public static void main (string [] ARGs) {outer out = new outer (); out. method (); // directly access the outer in the internal class. inner in = new outer (). new inner (); In. function ();}}

Ii. Anonymous internal class
1. The anonymous internal class is in short format.
2. Prerequisites for defining anonymous internal classes: Internal classes must inherit a class or implement interfaces.
3. Anonymous internal class format: new parent class or interface () {defines the content of the subclass}
4. The anonymous internal class is an anonymous subclass object, and the object is a little fat.
5. It is recommended that no more than three methods are defined in the anonymous internal class.
When using anonymous internal classes, remember the following principles:
· No constructor is allowed for anonymous internal classes .??
· Anonymous internal classes cannot define any static members, methods, and classes .??
· Anonymous internal classes cannot be public, protected, Private, static .??
· Only one instance of the anonymous internal class can be created.
· An anonymous internal class must be behind New and be used to implicitly implement an interface or implement a class .??
· Because the anonymous internal class is a local internal class, all restrictions on the local internal class take effect.
· Internal classes can only access static variables or static methods of external classes.

interface Inter{ void method();}class InterImpl implements Inter{ public void method(){  System.out.println("hello world!!");  }}public class InnerDemo{ public static void main(String[] args){   InterImpl impl = new InterImpl();    show(impl);  }  public static void show(Inter abc){    abc.method();  }}

Iii. Exceptions
1. Exception definition: An exception occurs when the program is running.
Exception cause: the problem is also a specific thing in real life. It can also be described in the form of Java classes and encapsulated into objects. In fact, it is the embodiment of the object after Java describes the abnormal situation.
Classification of problems: one is a serious problem, and the other is a non-serious problem.
Serious: Java uses error to describe. Generally, no specific code is written to handle the error.
For non-serious ones: Java uses the exception class to describe. For exceptions, you can use targeted processing methods.
Both error and exception have some common content, such as abnormal information and causes.
Throwable
| --- Error
| --- Exception
| --- Runtimeexception
| --- Non-runtimeexception
2. Handling exceptions
Java provides special statements for processing.
Try {
Code to be detected;
} Catch (abnormal variable name ){
Handling process;
} Finall {
Statements that will certainly run;
}
Handling of multiple exceptions:
When declaring an exception, it is recommended to declare a more specific exception, so that the handling can be more specific.
When several exceptions are declared, they correspond to several catch blocks. do not define additional catch blocks. If an exception in multiple catch blocks has an inheritance relationship, the parent exception catch block is placed at the bottom.
When performing catch processing, the catch processing statement must define a specific processing method. Do not simply define e. printstacktrace () or simply write an output statement. You can write exceptions to log exception files.
3. Exception throw: throws are thrown after the method body.
4. custom exception classes
Custom exception class: because the project has special problems that are not described by Java and encapsulated into objects, therefore, you can customize exception encapsulation for these special problems according to Java's encapsulation philosophy.
When throw throws an exception object in the function, the corresponding processing action must be given, either try catch internally or declare on the function for the caller to handle the exception. Generally, an exception occurs in the function and must be declared on the function.
Cause of inherited exception:
The exception system has a feature because exception classes and exception objects are thrown. This flexibility is unique in the throwable system.
Only the class and object CAI in this system can be throws and throw operations.
5. Difference between throws and throw
Throws are used in functions; throws are used in functions.
The exception classes that follow throws can be separated by commas (,). The exception objects are followed by throws.
6. inherit exception and runtimeexception.
5. Finally
The code block that will be executed.
Iv. Packages
The four access permissions of the package.

Summary:
1. Internal and anonymous internal classes (directly declare methods and define objects );
2. exception system. Pay attention to runtimeexception.

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.