Example code of local internal classes and Implementation of the simplest alarm Application

Source: Internet
Author: User

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

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.