Concepts of Java Inner classes and exception classes

Source: Internet
Author: User
Tags getmessage stub throw exception java throws

1. The member variables of the inner class are still valid in the inner class, and the methods in the inner class can call methods in the outer class, and the inner class cannot declare the variables and methods of the class, and the nested class body can declare the object with an inner class as a member of the outer nested class. The inner class is used only for his outer-nested class.

Package com. Example1;

public class Example7_1 {

public static void Main (string[] args) {
TODO auto-generated Method Stub
Redcowform form = new Redcowform ("JJF");
Form.showmessage ();
Form.cow.speak ();

}

}

Class Redcowform {
Static String FormName;
Redcow Cow;

Redcowform () {

}

Redcowform (String s) {
Cow = new Redcow (12, 12, 12);
FormName = s;
}

public void ShowMessage () {
Cow.speak ();
}

Class Redcow {
String cowname = "Red";
int height, weight, price;

Redcow (int h, int w, int p) {
This.height = h;
This.weight = W;
This.price = p;
}

void Speak () {
SYSTEM.OUT.PRINTLN ("* * * *" + cowname + "# # #" + height + "# # #" + formName + "+ weight);
}
}
}

2, the concept of anonymous class

An anonymous class can inherit a method from a parent class or override a method of a parent class, using anonymous to create an object directly with an anonymous class, so an anonymous class must be an inner class, an anonymous class can access variables and methods in an outer-nested class, and an anonymous class weight cannot declare a static member variable and a static method. Because the anonymous class is a subclass and there is no class name, the anonymous class uses the constructor of the parent class directly when creating the object.

Package com. Example1;

public class Example7_3 {

public static void Main (string[] args) {
TODO auto-generated Method Stub
Showboad board = new Showboad ();
Board.showmessage (New Outputenglish ());
Board.showmessage (New Outputalphabet () {
public void output () {
for (char c = ' a '; c < ' W '; C + +)
System.out.printf ("%3c", c);
}
});

}

}

Abstract class Outputalphabet {
public abstract void output ();
}

Class Outputenglish extends Outputalphabet {

@Override
public void output () {
for (char c = ' a '; c < ' z '; C + +) {
System.out.printf ("%3c", c);
}

}

}

Class Showboad {
void ShowMessage (Outputalphabet show) {
Show.output ();
}
}
4. Anonymous classes related to interfaces

Package com. Example1;

Interface Speakhelloman {
void speak ();
}

Class Hellomeachine {
public void TurnOn (Speakhelloman hello) {
Hello.speak ();
}
}

public class Example7_7 {

public static void Main (string[] args) {
TODO auto-generated Method Stub
Hellomeachine machine = new Hellomeachine ();
Machine.turnon (New Speakhelloman () {
public void Speak () {
System.out.println ("Hello You is welcome");
}
});
Machine.turnon (New Speakhelloman () {
public void Speak () {
System.out.println ("Welcome");
}
});

}

}

5, the concept of the exception class

Exception class processing alters the program's control flow, allowing the program to deal with errors, and Java throws an instance of the exception subclass with the Throw keyword to indicate that an exception occurred. Using the Try-catch statement to manipulate exception statements

Package com. Example1;

public class Example7_4 {

public static void Main (string[] args) {
TODO auto-generated Method Stub
int n = 0, m = 0, t = 100;
try {
m = Integer.parseint ("232342");
n = integer.parseint ("787676");
t = integer.parseint ("2402321");
} catch (NumberFormatException e) {
SYSTEM.OUT.PRINTLN ("exception occurred" + E.getmessage ());
}
SYSTEM.OUT.PRINTLN (M + "" + N + "" + t);
try {
System.out.println ("throw exception");
throw new Java.io.IOException ("exception");
} catch (Java.io.IOException e) {
SYSTEM.OUT.PRINTLN ("Eccor error" + e.getmessage ());
}

}

}
6. The concept of custom exception classes

When writing a program, you can extend the exception class to define its own exception class, a method can be declared with the keyword throw throws a dry exception, and in the method body of the method to give an exception, that is, with the response of the exception class to create an object, and use the Throw keyword throws an exception object, Causes the exception to end.

Package com. Example1;

public class Example7_5 {

public static void Main (string[] args) {
TODO auto-generated Method Stub
Bank Bank = new Bank ();
try {
Bank.income (200,-22);
Bank.income (452,-222);
Bank.income (200,-600);
System.out.println (Bank.getmoney ());
Bank.income (9999,-666);
} catch (Bankexception e) {
System.out.println ("Problems arise");
System.out.println (E.warnmess ());
}

}

}

Class Bankexception extends Exception {
String message;

Public bankexception (int m, int n) {
Message = "in" + M + "out" + N;
}

Public String warnmess () {
return message;
}
}

Class Bank {
private int money;

public void income (int in, int out) throws Bankexception {
if (in <= 0 | | out >= 0 | | In + out <= 0) {
throw new Bankexception (in, out);
}
int netincome = in + out;
System.out.println ("" + netincome);
}

public int Getmoney () {
return money;
}

}

7. Assertion Processing Concept

Assertion statement: The program is not ready to handle the error by catching an exception and pausing directly. Assert Booleanexception:message

Concepts of Java Inner classes and exception classes

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.