类与对象
1)什么是类?
类是一些具有相同属性和行为的事物的集合
2)什么是对象?
对象是生活中的一个一个的实体、客观的。
3)面向对象编程思想(OOP)
基本语法:
A.一个类对应一个java文件
B.属性的定义格式:public 类型 属性名
C.方法的定义格式:public 返回类型 方法名(参数列表){}
D.创建对象你给的格式:类名 对象名 = new 类名();
E.调用属性:对象名.属性名
F.调用方法:对象名.方法名(具体内容)
实例:计算机软件调用工具实现(开关机操作)
主方法:
public class Main {
public static void main(String[] args) {
UI test=new UI();
}
}
UI界面
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
public class UI extends JFrame {
/**
* @author xhh
*/
private JButton btn_shutdown;
private JButton btn_reboot;
private JButton btn_openBrowser;
//构造方法
public UI() {
super(“电脑操作界面”);
this.setSize(400, 500); // 设置窗体大小
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null); // 设置窗体居中
this.setLayout();
this.addListener();
this.setVisible(true); // 设置窗体可见
}
//设计UI界面
private void setLayout(){
btn_shutdown=new JButton(“关机”);
btn_reboot=new JButton(“打开QQ”);
btn_openBrowser=new JButton(“打开浏览器”);
this.setLayout(new FlowLayout());
this.add(btn_shutdown);
this.add(btn_reboot);
this.add(btn_openBrowser);
}
//为组件添加监听
private void addListener(){
ActionListener actionListener=new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String event=e.getActionCommand();
Runtime run=Runtime.getRuntime();
if("关机".equals(event)){
try{
run.exec("shutdown -s -t 10000");
}catch (IOException e1){
e1.printStackTrace();
}
}else if("打开QQ".equals(event)){
try{
System.out.println("点击了打开QQ");
run.exec("D:\\Tencent\\QQ\\Bin\\QQ.exe");
}catch (IOException e1){
e1.printStackTrace();
}
}else if("打开浏览器".equals(event)){
try{
run.exec("explorer http://www.baidu.com");
}catch (IOException e1){
e1.printStackTrace();
}
}
}
};
btn_shutdown.addActionListener(actionListener);
btn_reboot.addActionListener(actionListener);
btn_openBrowser.addActionListener(actionListener);
}
}