1. Local internal class example
Implementation: Console output:
The unit price for Apple is 100 yuan.
Code details:
Package com. lixiyu; public class SellOutClass {private String name; // private member variable public SellOutClass () {name = "apple" ;}public void evaluate (int price) {class Apple {// local internal class int innerPrice = 0; public Apple (int price) {innerPrice = price;} public void price () {System. out. println ("start selling now" + name); System. out. println ("unit price:" + innerPrice + "Yuan") ;}} Apple apple = new Apple (price); // instantiate the Apple Class Object apple. price (); // call the local internal class method} public static void main (String [] args) {SellOutClass sample = new SellOutClass (); // instantiate the sample object of the SellOutClass class. callback (100); // call the callback method of the selloutclass class }}
--------
2. Simple alarm Application
Implementation: The console continuously outputs the current time and generates a prompt every second. You can click OK to exit the program.
:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1430205S4-0.jpg "title =" b8wwhh7yhid3c81_wlu@s)ut.jpg "alt =" 231917371.jpg"/>
Code details:
Main program section:
Package com. lixiyu; import javax. swing. JOptionPane; public class PartArea {public static void main (String [] args) {AlarmClock clock = new AlarmClock (1000, true); clock. start (); JOptionPane. showMessageDialog (null, "exit? "); System. exit (0 );}}
Implement the AlarmClock section and apply it to local internal class methods
Package com. lixiyu; import java. awt. toolkit; import java. awt. event. actionEvent; import java. awt. event. actionListener; import java. text. simpleDateFormat; import java. util. date; import javax. swing. timer; public class AlarmClock {private int delay; private boolean flag; public AlarmClock (int delay, boolean flag) {// local internal class this. delay = delay; this. flag = flag;} public void start () {// TODO Auto-generated method stub class Printer implements ActionListener {@ Override public void actionreceivmed (ActionEvent e) {SimpleDateFormat format = new SimpleDateFormat ("k: m: s"); String result = format. format (new Date (); System. out. println ("current time:" + result); if (flag) {Toolkit. getdefatooltoolkit (). beep () ;}} new Timer (delay, new Printer ()). start ();}}
This article is from the "ToBeContinued" blog, please be sure to keep this source http://leexy.blog.51cto.com/4136883/1304680