java基本知识



第八章 多线程

1.线程创建两种方法

  • extends Thread

    //声明线程时声明类即可
    public class TestThread1 extends   Thread{
        @Override
        public void run() {
            for (int i = 0; i < 20; i++) {
                System.out.println("执行了run方法"+i);
            }
        }
        public static void main(String[] args) {
            TestThread1 testThread1 =new TestThread1();
            //调用线程,用的是start方法
            testThread1.start();
        }
    }
    ​

  • implements Runnable

    声明线程时声明类即可要声明Thread(代理模式)
    public class TestThread2 implements  Runnable{
        @Override
        public void run() {
            for (int i = 0; i < 20; i++) {
                System.out.println("执行了run方法"+i);
            }
        }
    ​
        public static void main(String[] args) {
            TestThread2 testThread2 =new TestThread2();
            //调用线程,用的是start方法
           //new 一个Thread方法去进行线程模式,在这里用了代理模式
            new Thread(testThread2,'name').start();
        }
    }

Thread.currentThread.getName()可获得当前线程名字

2.线程间的同步机制

- sleep
- isAlive
- interrupt
- isInterrupted
- synchronized
- public synchronized void put(int i){
    
}
- 在run中
    synchronized(对象){
    
}
- wait
- notify

第十章 输入输出流与文件处理

输入输出流(引入import java.io.*)

字节流
DataInputStream d =new DataInputStream(new FileInputStream("1.txt"));
//读 ,有返回值
- readInt()
- readDouble()
- readChar()
    
DataOutputStream b =new DataOutputStream(new FileOutputStream("1.txt"));
//写 //可用for循环写入数组
- writeInt(int a)
- writeDouble(double a) 
- writeChar(char a)
- writeChars(String a)

字符流
BufferedReader din = new BufferedReader(new FileReader("1.txt"));
//or BufferedReader din = new BufferedReader(new InputStreamReader("1.txt"));
//读
- readline();//一读读一行
BufferedWriter dout = new BufferedWriterr(new FileWriter("1.txt"));
//写
- write("可写任意类型")
- newLine() //换行

文件处理

File dir =new File(".");//当前目录
File[] files =dir.listFiles();//返回当前文件下所有目录
- isFile() //是否是文件
​

文件过滤器

InnerFilter filter

第六章 图形用户界面(GUI)

  1. 导入包 > import java.awt.*

  2. 导入包>import javax.swing.*

基本构成

基本操作

//基本操作
import javax.swing.*;
//1.继承JFrame
public class Demo extends JFrame {  
    public Demo(){
        super("标题");//设置标题
        this.setBounds(100,300,200,200);//设置位置与大小
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);//可关闭
        this.setVisible(true);//可看见
    }

    public static void main(String[] args) {
        Demo demo = new Demo();
    }
}
 

基本布局

布局分为三种布局分别是流布局管理器FlowLayout,边布局管理器 BorderLayout和网格布局管理器GridLayout

//为面板或容器设置布局时,一个关键的命令是 
- setLayout(new FlowLayout())
- setLayout(new GridLayout(3,1))
- setLayout(new BorderLayout()) // 用BorderLayout布局设置方位时 p1.add(button2,BorderLayout.EAST);

当然也可以直接设置方位如 JButton a =new JButton("as"); a.setBounds(24,95,156,46);

基本组件详细解释

//所有的组件要想加到容器中要用到这个命令
this.getContentPane().add(p1)
  //组件——>面板————>容器
  1. JDialog

import javax.swing.*;

// 导入java.awt包,这个包提供了GUI和Web应用程序的许多基础类和接口  
import java.awt.*;

// 导入java.awt.event包的ActionEvent和ActionListener,用于处理按钮点击等事件  
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

// 声明一个名为Example6_11的公共类  
public class Example6_11 {
    // main方法是Java程序的入口点  
    public static void main(String[] args){
        // 创建DialogDemo对象,打开对话框示例  
        new DialogDemo();
    }
}

// 创建一个名为DialogDemo的类,这个类继承自JFrame类,JFrame是一个顶层窗口容器  
class DialogDemo extends JFrame {
    // 创建私有按钮对象btn和自定义对话框对象dialog  
    private JButton btn;
    private MyJDialog dialog;

    // DialogDemo构造函数  
    DialogDemo() {
        // 调用父类构造函数,设置窗口标题为"对话框示例"  
        super("对话框示例");
        // 设置窗口的位置和大小  
        this.setBounds(100, 100, 200, 200);
        // 设置窗口默认的关闭操作为EXIT_ON_CLOSE,即关闭窗口时结束程序  
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        // 创建一个按钮,并设置按钮上的文字为"打开对话框"  
        btn = new JButton("打开对话框");
        // 将按钮添加到窗口的内容面板上  
        this.getContentPane().add(btn);

        // 创建一个自定义对话框  
        dialog = new MyJDialog(this);
        // 为按钮添加监听器,当按钮被点击时,执行监听器中的actionPerformed方法  
        btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // 如果事件源是btn按钮,则使对话框可见  
                if (e.getSource() == btn)
                    dialog.setVisible(true);
            }
        });
        // 设置窗口为可见状态  
        this.setVisible(true);
    }

    // 创建一个名为MyJDialog的类,这个类继承自JDialog类  
    class MyJDialog extends JDialog //继承是关键{
        // MyJDialog构造函数,需要一个JFrame对象作为参数  
        public MyJDialog(JFrame jframe) {
            // 调用父类构造函数,初始化对话框,设置对话框的父窗口,标题和模态  
            super(jframe, "我的对话框");
            // 设置对话框的位置和大小  
            this.setBounds(jframe.getX() + jframe.getWidth() + 10, jframe.getY(), 150, 150);
            // 设置对话框默认的关闭操作为HIDE_ON_CLOSE,即关闭对话框时隐藏对话框,不结束程序  
            this.setDefaultCloseOperation(HIDE_ON_CLOSE);
            // 设置对话框内容面板的布局为流布局  
            this.getContentPane().setLayout(new FlowLayout());
            // 向对话框的内容面板上添加两个按钮,并设置按钮上的文字为"学习"和"休息"  
            this.getContentPane().add(new JButton("学习"));
            this.getContentPane().add(new JButton("休息"));
        }
    }
}
  1. JRadioButton

//在处理单选按钮时,需要只有一个选项,这时,就要把一组多选框放到一组ButtonGroup中

ButtonGroup bg =new ButtonGroup();
bg.add("单选按钮1");
bg.add("单选按钮2");
p1.add(bg);//将bg加到面板中
  1. Jlist JComboBox

两者添加的可以是一个数组

4.JMenuBar

事件处理机制

我认为可能会考的两个事件处理

  • ActionListener ----> JButton ----> actionPerformed(ActionEvent e) 单机操作

//操作
JBUtton  b1 = new JButton("sad");
b1.addActionListener(this);
//1.继承JFrame 2.继承ActionListener
public class Demo extends JFrame implements ActionListener {
    private JButton b1;
    private JPanel p1;

    public Demo(){
        super("标题");//设置标题
        this.setBounds(100,300,200,200);//设置位置与大小
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);//可关闭
        this.setVisible(true);//可看见
        this.getContentPane().setLayout(new BorderLayout());
         p1 = new JPanel();
        this.getContentPane().add(p1,BorderLayout.CENTER);
        b1 = new JButton("asd");
        b1.setBackground(Color.BLUE);
        p1.add(b1);
        b1.addActionListener(this);//此处为关键

    }

    public static void main(String[] args) {
        Demo demo = new Demo();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==b1){//判断点击的按钮
            p1.setBackground(Color.red);
        }
    }
}
 

  • MouseListener

  1. 事件适配器写法

package henu;
//基本操作
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

//1.继承JFrame
public class Demo extends JFrame
{
    Container c;

    public Demo(){
        super("标题");//设置标题
        this.setBounds(100,300,200,200);//设置位置与大小
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);//可关闭
        this.setVisible(true);//可看见
        c = this.getContentPane();
       this.getContentPane().addMouseListener(new MouseAdapter() {//匿名类
           @Override
           public void mouseClicked(MouseEvent e) {
              c.setBackground(Color.BLUE);
           }
       });


    }

    public static void main(String[] args) {
        Demo demo = new Demo();
    }



}
 
  1. 事件监听器接口

package henu;
//基本操作
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

//1.继承JFrame
public class Demo extends JFrame implements MouseListener// 继承MouseListener类
{
    Container c;

    public Demo(){
        super("标题");//设置标题
        this.setBounds(100,300,200,200);//设置位置与大小
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);//可关闭
        this.setVisible(true);//可看见
        c = this.getContentPane();
       this.getContentPane().addMouseListener(this);


    }

    public static void main(String[] args) {
        Demo demo = new Demo();
    }


    @Override
    public void mouseClicked(MouseEvent e) {
        c.setBackground(Color.BLUE);
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
 

第五章 Java基本类

java.lang语言包

img

字符串类

最基本的字符串声明不用多说,在这里只说明其常用的几个方法

- s.length // 字符串长度获取
- s.equals // 字符串比较
- s.equalsIgnoreCase // 忽略大小写的字符串比较
-  字符串的检索
    - char charAt(int index) // 返回index上的字符
    - int indexOf(int ch) //返回ch在字符串中出现的第一个位置
    - int lastIndexOf(int ch) //返回ch在字符串中出现的最后一个位置
    - int indexOf(String str) //返回str在字符串中出现的第一个位置
    - int lastIndexOf(String str) //返回str在字符串中出现的最后一个位置
    - void getChars(int srcbegin,int end,char buf[], int destbegin)//将该字符串从srcbegin到end-1放到buf[] 的起始位置为destbegin中 
- 修改字符串
    - String concat(Strign str) //连接字符串可用+代替
    - String trim() //去掉前后空格
    - String replace(char oldChar,char newChar) //替换字符串
    - String substring(int beginIndex) //获取从beginIndex到结尾的子串
    - String  substring(int beginIndex,int end)//获取从beginIndex到end-1的子串

Java.util

集合API中的List

img

//示例
List<String> x =new ArrayList<>();
	x.add("sa");
	x.add("sda");
	x.add("a");

List的输出

// 1.for循环输出
for(int i =0 ;i<x.size();i++) {
		System.out.println(x.get(i));
	}
//2.ListIterator 迭代器输出
ListIterator<String> le = x.listIterator();
	while(le.hasNext()) {
		 String s =le.next();
		 System.out.println(s);
	}
	//用ListIterator而不用Iterator是ListIterator可更改
    ListIterator<String> le = x.listIterator();
	while(le.hasNext()) {
		 String s =le.next();
         if(s.equals("a")){
             le.add("sdad");//迭代器加
         }
	}
//3.增强for循环输出
     for(String s: x)  
    {
        System.out.println(s);
   
Random
Random ran =new Random();
ran.nextInt(100)//生成100以内的随机数
Data
        Date date = new Date();// 实例化一个Date类,用于获取当前系统时间
        System.out.println(date);// 输出时间,Thu May 23 14:36:10 JST 2019

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd E HH-mm-ss");//在java.text 包
        String str = sdf.format(date);// 按照指定格式转为字符串
        System.out.println(str);// 输出转换后的时间,2019-05-23 星期四 14-36-10

第三章 Java面向对象

我所认为的难点

  • static 变成了类方法或类变量,类方法中不能有super与this

  • super

  • 类的继承,接口与多态

类的基本结构

image-20240104125222546

类的继承(extends)

区分子类方法中重名的三种变量:

1.局部变量: 直接写成员变量名

2.本类的成员变量: this.成员变量名

3.父类的成员变量: super.成员变量名

  • 能继承的情形

    • 子类能继承父类的非私有成员变量(实例与类变量)

    • 子类能继承除了构造方法以外的所有成员方法

  • 其他特性

    • 子类不能删除父类成员

    • 子类可以增加成员变量与方法

    • 在子类的构造方法中可用super()初始化父类构造方法

    • super.成员方法 super.成员变量

  • 上转型

    • 即 People p1 =new Student(); People为父类,Student为子类

  • 隐藏,覆盖,动态绑定

    • 对于子类重定义父类成员的情况

      • 对于成员变量与静态方法,子类的隐藏父类的。上转型引用的是父类的。

      • 对于实例成员方法,子类覆盖父类的。上转型引用的是子类的,这种方式成为动态绑定。

      • 若子类实例成员方法未覆盖父类,调用父类方法时,方法中用的是父类的成员变量,甚至是私有变量。

类的接口

满足类的接口中不能有具体的实例,所有方法,子类都需实现

  • 23
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值