• 内部类代码


     1 package com.yj.fenghao.indoor;
     2 
     3 import java.awt.Toolkit;
     4 import java.awt.event.ActionEvent;
     5 import java.awt.event.ActionListener;
     6 import java.util.Date;
     7 
     8 import javax.swing.Timer;
     9 
    10 /**内部类
    11  * 内部类可以是私有类  ---内部类既可以访问自身的数据域,也可以访问构造他的外围类对象的数据域
    12  * 编译器修改类内部类的构造器,添加了对外部类的引用
    13  * <P>Description: TODO</P>
    14  * @ClassName: TimerClock
    15  * @author 冯浩  2017年4月5日 下午1:19:08
    16  * @see TODO
    17  */
    18 
    19 public class TimerClock {
    20     
    21     private int intervel;
    22     private boolean beep;
    23     public TimerClock(int intervel, boolean beep) {
    24         super();
    25         this.intervel = intervel;
    26         this.beep = beep;
    27     }
    28     
    29     public class TimerPrinter implements ActionListener{
    30 
    31 //        private TimerClock out;
    32 //        public TimerPrinter(TimerClock out){
    33 //            this.out=out;
    34 //        }
    35         
    36         public void actionPerformed(ActionEvent e) {
    37             Date date=new Date();
    38             System.out.println("at the now ,this is "+date);
    39             if(beep){
    40                 Toolkit.getDefaultToolkit().beep();
    41             }
    42 //            if(out.beep){
    43 //                Toolkit.getDefaultToolkit().beep();
    44 //            }
    45         }
    46         
    47     }
    48     
    49     public void start(){
    50 //        ActionListener listener=new TimerPrinter(this);
    51         ActionListener listener=new TimerPrinter();
    52         Timer t=new Timer(intervel,listener);
    53         t.start();
    54         
    55     }
    56 
    57 }
     1 package com.yj.fenghao.indoor;
     2 
     3 import java.awt.Toolkit;
     4 import java.awt.event.ActionEvent;
     5 import java.awt.event.ActionListener;
     6 import java.util.Date;
     7 
     8 import javax.swing.Timer;
     9 
    10 import com.yj.fenghao.indoor.TimerClock.TimerPrinter;
    11 
    12 /**
    13  * 局部内部类---作用域被限定在定义这个类的块中-----不仅能够访问包含他们的外部类,还能访问局部变量(必须定义成final)
    14  * <P>Description: TODO</P>
    15  * @ClassName: TimerClock2
    16  * @author 冯浩  2017年4月6日 上午8:36:02
    17  * @see TODO
    18  */
    19 public class TimerClock2 {
    20     
    21     private int intervel;
    22     private boolean beep;
    23     public TimerClock2(int intervel, boolean beep) {
    24         super();
    25         this.intervel = intervel;
    26         this.beep = beep;
    27     }
    28     
    29       public void start(){
    30             class TimerPrinter implements ActionListener{
    31                 public void actionPerformed(ActionEvent e) {
    32                     Date date=new Date();
    33                     System.out.println("at the now ,this is "+date);
    34                     if(beep){
    35                         Toolkit.getDefaultToolkit().beep();
    36                     }
    37                     
    38                 }
    39                 
    40             }
    41             ActionListener listener=new TimerPrinter();
    42             Timer t=new Timer(intervel,listener);
    43             t.start();
    44             
    45         }
    46 
    47 }
     1 package com.yj.fenghao.indoor;
     2 
     3 import java.awt.Toolkit;
     4 import java.awt.event.ActionEvent;
     5 import java.awt.event.ActionListener;
     6 import java.util.ArrayList;
     7 import java.util.Date;
     8 import java.util.List;
     9 
    10 import javax.swing.Timer;
    11 
    12 import com.yj.fenghao.indoor.TimerClock.TimerPrinter;
    13 
    14 /**
    15  * 匿名内部类---不能有构造器,将构造器参数传递给超类
    16  * <P>Description: TODO</P>
    17  * @ClassName: TimerClock2
    18  * @author 冯浩  2017年4月6日 上午8:36:02
    19  * @see TODO
    20  */
    21 public class TimerClock3 {
    22     
    23     private int intervel;
    24     private boolean beep;
    25     public TimerClock3(int intervel, boolean beep) {
    26         super();
    27         this.intervel = intervel;
    28         this.beep = beep;
    29     }
    30     
    31       public void start(){
    32            
    33             ActionListener listener=new ActionListener(){
    34 
    35                 public void actionPerformed(ActionEvent e) {
    36                     Date date=new Date();
    37                     System.out.println("at the now ,this is "+date);
    38                     if(beep){
    39                         Toolkit.getDefaultToolkit().beep();
    40                     }
    41                 }
    42                 
    43             };
    44             Timer t=new Timer(intervel,listener);
    45             t.start();
    46             
    47         }
    48       
    49       public void t(){
    50           this.test(new ArrayList(){{add("111");add("22222");}});
    51       }
    52       public void test(List<String> list){
    53           
    54       }
    55 
    56 }
     1 package com.yj.fenghao.indoor;
     2 
     3 
     4 /**
     5  * 静态内部类-没有对生成他的外围类的引用
     6  * <P>Description: TODO</P>
     7  * @ClassName: StaticInnerClass
     8  * @author 冯浩  2017年4月6日 上午9:09:31
     9  * @see TODO
    10  */
    11 public class StaticInnerClass {
    12     
    13     public static void main(String[] args) {
    14         double[] d=new double[20];
    15         for (int  i= 0; i < d.length; i++) {
    16             d[i]=Math.random()*100;
    17         }
    18         ArrayAlg.Pair minMax = ArrayAlg.Pair.minMax(d);
    19         System.out.println(minMax.getFirst());
    20         System.out.println(minMax.getSecond());
    21     }
    22     
    23     
    24     
    25 
    26 }
    27 class ArrayAlg{
    28     public static class Pair{
    29         private double first;
    30         private double second;
    31         public Pair(double first, double second) {
    32             super();
    33             this.first = first;
    34             this.second = second;
    35         }
    36         public double getFirst() {
    37             return first;
    38         }
    39         public double getSecond() {
    40             return second;
    41         }
    42         
    43         public static Pair minMax(double[] values){
    44             double maxValue = Double.MAX_VALUE;
    45             double minValue = Double.MIN_VALUE;
    46             for (double d : values) {
    47                 if(d>maxValue)maxValue=d;
    48                 if(d<minValue)minValue=d;
    49             }
    50             return new Pair(maxValue,minValue);
    51         }
    52         
    53         
    54     }
    55 }

    内部类的简单代码,需要的时候看一下

    关于这个定时器的完整功能代码,挺好玩的

     1 package com.yj.fenghao.indoor;
     2 
     3 import java.awt.Toolkit;
     4 import java.awt.event.ActionEvent;
     5 import java.awt.event.ActionListener;
     6 import java.util.Date;
     7 
     8 import javax.swing.*;
     9 import javax.swing.Timer;
    10 
    11 
    12 public class TimerTest {
    13     public static void main(String[] args) {
    14         
    15         ActionListener listener=new TimerPrinter();
    16         Timer t=new Timer(1000,listener);
    17         t.start();
    18         JOptionPane.showMessageDialog(null, "are you ok !");
    19         System.exit(0);
    20     }
    21     
    22     
    23 
    24 }
    25 class TimerPrinter implements ActionListener{
    26 
    27     public void actionPerformed(ActionEvent e) {
    28         Date date=new Date();
    29         System.out.println("at the tone ,this time is +"+date);
    30         Toolkit.getDefaultToolkit().beep();
    31         
    32     }
    33     
    34 }
  • 相关阅读:
    MarkDown 语言简单使用
    开机自启宽带连网设置问题
    JqueryEasyUI EasyLoader
    JqueryEasyUI $.Parser
    FileUpload一键自动上传
    Git读档
    JavaScript typeof运算符和数据类型
    JavaScript 函数 (function)
    Javascript 对象(Object)
    JavaScript 数组(Array)
  • 原文地址:https://www.cnblogs.com/nihaofenghao/p/6671919.html
Copyright © 2020-2023  润新知