33.重写tostring方法 返回格式 一般和java.awt.point的tostring返回格式类似
this.getClass().getName()+"[name="+name+"]";
ClassName+"[name="+name+"]";
34.Object中的clone默认是浅拷贝 像基本类型的拷贝都可以进行,但是如果被拷贝的类中有内部对象,则不会被拷贝,而是共享
假如被克隆的对象里有Date 这样的可变对象 ,则要进行深克隆 在clone时 ,将对象的属性也克隆过来
如果clone方法里有没有实现cloneable的对象 就会抛异常 clonenot-supportExcetion 不支持克隆异常
public class Test implements Cloneable{
Date date =new Date();
public Test clone() throws CloneNotSupportedException {
Test t2=(Test)super.clone();
//t2.date=(Date)date.clone(); 注释去掉 就是 深克隆
return t2;
}
public static void main(String[] args) throws CloneNotSupportedException {
Test test=new Test();
Test test1=(Test)test.clone();
System.out.println(test.date==test1.date);
}
}
在标准类库中 只有大概5%的类实现了clone
35.Timer 定时任务的 实现方式
1 import java.util.Calendar; 2 import java.util.Date; 3 import java.util.Timer; 4 import java.util.TimerTask; 5 6 public class TimeTest { 7 public static void main(String[] args) { 8 timer1(); 9 //timer2(); 10 //timer3(); 11 //timer4(); 12 } 13 14 // 第一种方法:设定指定任务task在指定时间time执行 schedule(TimerTask task, Date time) 15 public static void timer1() { 16 Timer timer = new Timer(); 17 timer.schedule(new TimerTask() { 18 public void run() { 19 System.out.println("-------设定要指定任务--------"); 20 } 21 }, 2000);// 设定指定的时间time,此处为2000毫秒 22 } 23 24 // 第二种方法:设定指定任务task在指定延迟delay后进行固定延迟peroid的执行 25 // schedule(TimerTask task, long delay, long period) 26 public static void timer2() { 27 Timer timer = new Timer(); 28 timer.schedule(new TimerTask() { 29 public void run() { 30 System.out.println("-------设定要指定任务--------"); 31 } 32 }, 1000, 5000); 33 } 34 35 // 第三种方法:设定指定任务task在指定延迟delay后进行固定频率peroid的执行。 36 // scheduleAtFixedRate(TimerTask task, long delay, long period) 37 public static void timer3() { 38 Timer timer = new Timer(); 39 timer.scheduleAtFixedRate(new TimerTask() { 40 public void run() { 41 System.out.println("-------设定要指定任务--------"); 42 } 43 }, 1000, 2000); 44 } 45 46 // 第四种方法:安排指定的任务task在指定的时间firstTime开始进行重复的固定速率period执行. 47 // Timer.scheduleAtFixedRate(TimerTask task,Date firstTime,long period) 48 public static void timer4() { 49 Calendar calendar = Calendar.getInstance(); 50 calendar.set(Calendar.HOUR_OF_DAY, 12); // 控制时 51 calendar.set(Calendar.MINUTE, 0); // 控制分 52 calendar.set(Calendar.SECOND, 0); // 控制秒 53 54 Date time = calendar.getTime(); // 得出执行任务的时间,此处为今天的12:00:00 55 56 Timer timer = new Timer(); 57 timer.scheduleAtFixedRate(new TimerTask() { 58 public void run() { 59 System.out.println("-------设定要指定任务--------"); 60 } 61 }, time, 1000 * 60 * 60 * 24);// 这里设定将延时每天固定执行 62 } 63 }package com.jsdc.ybjava.ybjava;
import java.awt.Color;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JFrameJButton extends JFrame {
private static final long serialVersionUID=1l;
private JPanel jPanel;//创建面板
private static final int WIDTH=400;//宽度
private static final int HEIGHT=400;//高度
//创建构造函数
public JFrameJButton() throws HeadlessException {
setSize(WIDTH, HEIGHT);//设置宽高
//创建按钮
JButton one=new JButton("第一季度");
JButton two=new JButton("第二季度");
JButton three=new JButton("第三季度");
JButton four=new JButton("第四季度");
//实例化jPanel
jPanel=new JPanel();
jPanel.add(one);
jPanel.add(two);
jPanel.add(three);
jPanel.add(four);
add(jPanel);
//给按钮添加监听
ColorActions red=new ColorActions(Color.red);
ColorActions blue=new ColorActions(Color.blue);
ColorActions yellow=new ColorActions(Color.yellow);
ColorActions green=new ColorActions(Color.green);
one.addActionListener(red);
two.addActionListener(yellow);
three.addActionListener(blue);
four.addActionListener(green);
}
//创建内部类 实现 ActionListener
private class ColorActions implements ActionListener{
private Color bgColor;
public ColorActions(Color bgColor) {
super();
this.bgColor = bgColor;
}
public void actionPerformed(ActionEvent e) {
jPanel.setBackground(bgColor);
}
}
public static void main(String[] args) {
new JFrameJButton();
}
}
java swing 里面的 JFram JButton 是java的界面语言 java开发的前台语言
36 public class MyTask extends TimerTask {
public void run() {
System.out.println("该起床了");
}
public static void main(String[] args) {
Timer t=new Timer();
t.schedule(new MyTask(), 3000);
}
}
可以这样使用定时函数