1,面向对象的本质
面向对象是一种方式 通过这种方式 把数据和方法重新封装 以提高代码的重复使用效率
class 类名{属性,方法} //创建了模板
类名 对象名 = new 类名(); //通过模板 创建实际案例的对象
对象名.属性;//使用里面的属性
对象名.方法;//使用里面的方法
2,java继承
访问权限控制:
- extend :扩展 延申 扩大 (java中 "继承" 的意思 extends 是其复数)
- public :公共的 公开的
- protected :受保护的
- default :默认的
- private :私有的 私自的
- super :超级的
3,面向对象三大特性封装,继承,多态:
继承:
1、extends
子类会继承父类的属性和方法
2、super
子类用于调用父类的内容
3、
子类可以重写父类的方法
重载:同名不同参的函数
重写:子类重写父类同名方法
封装:
封装(Java 权限控制)
public 公共的 (在哪儿都可以访问)
protected 受保护的
default 默认的
private 私有的 (只能在自己这个类里使用)受保护的数据 如何访问和修改
通过 指定的函数 例如 getter 和 setter
多态:
1 需要由继承
2 需要子类重写父类的函数
3 需要父类引用指向子类对象
4*子类独有的方法,多态不能启动,如果需要使用则强制类型转换*
list和set继承结构:
双向循坏列表:
集合与泛型:
1,常用子接口:Map,List,Set;
Map常用实现类:HashTable,HashMap,TreeMap
List常用实现类:Vector,Stack,LinkedList,ArrayList
Set常用实现类:HashSet,TreeSet
2,ArrayList 和LinkedList
相同点: 都是有序的 可以放重复的内容
不同点:
ArrayList 底层的实现时数组 有下标 查找块 但是修改慢
LinkedList 底层的实现双向循环链表 查找慢 但是修改块
3,ArrayList和HashSet
相同点:都可以存空值,由于都属于Collection接口,所以有很多方法都一样可以使用
不同点:
ArrayList是可重复的,是有序的,有索引
HashSet是不重复的,是无序的,无索引
4,HashSet和HashMap
相同点:都是哈希储存方法,可以存空值,算然查找数据比较快,但是修改都比较慢。
不同点:
HashSet实现set接口,仅储存对象;
HashMap实现了Map接口,储存的是键绝对,使用时需先将集合转化为set集合,再使用Iterator.
JDBCUtil.java
package com.app.JDBCUtil;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCUtil {
// Connection con = null;
// Statement stm = null;
// ResultSet rs = null;
//封装链接数据库功能 链接后 希望得到 Connection对象
public static Connection linkSQL(){
Connection con = null;
String url = null;
String name = null;
String password = null;
String driversrc = null;
try {
//读取配置文件
//类加载器 加载properties文件
InputStream inputStream = JDBCUtil.class.getClassLoader().getResourceAsStream("JDBC.properties");
//FileInputStream inputStream = new FileInputStream("./src/JDBC.properties");
Properties properties = new Properties();
properties.load(inputStream);
url = properties.getProperty("url");
name = properties.getProperty("name");
password = properties.getProperty("password");
driversrc = properties.getProperty("driversrc");
Class.forName(driversrc);
con = DriverManager.getConnection(url,name,password);
return con;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
//关闭释放资源
public static void close(Connection con){
if (con!=null) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void close(Statement stm){
if (stm!=null) {
try {
stm.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void close(ResultSet rs){
if (rs!=null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void close(Connection con,Statement stm,ResultSet rs){
if (con!=null) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (stm!=null) {
try {
stm.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (rs!=null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
小总结
//javaOOP复习
public class Demo02 {
public static void main(String[] args) {
- //面向对象 三大特征 封装 继承 多态
- //封装:为属性和方法分访问权限,以提高安全性
- //继承:让程序可以拓展,减少代码的重复,提高复用效率
- //多态:让程序更加灵活,减少代码的修改
//封装 四个关键词 当前类 子类 同一个包 不同的包
//public ; √ √ √ √
//protected; √ √ √ X
//default; √ √ X X
//private; √ X X X
继承 extends
- //构造函数(和类名相同的函数)的使用 初始化信息
- //this的使用 指代当前对象
- //super的使用 指代父类对象
重写 和 重载的区别
- //方法的重载 同名不同参数的方法
- //方法的重写 子类对于父类同名的方法进行重新定义
//要求 写一个父类 两个子类 子类对于父类的方法进行重写 并还可以调用父类的方法
//要求2 父类中 有构造函数 并 构造函数有重载
常用类
//包装类
//8大基本数据类型 对应的有8个包装类
//byte>>>Byte short>>>Short long>>>Long int>>>Integer
//float>>>Float double>>>Double char>>>>Character
//可以将基本数据类型转变为对象,也可以有对象转变成基本数据类型
Integer integer = new Integer("123");
int i = integer.intValue();
//自动封箱 拆箱
int i2 = new Integer("123");
//Math 算数常用类
int abs = Math.abs(-123);//绝对值
System.out.println(abs);
Math.ceil(3.14);//向上取整
Math.floor(3.14);//向下取整
Math.round(3.14);//四舍五入
Math.random();//0到1之间的随机小数
//求 随机数 30到60之间的 随机整数
//随机数 n到m之间的 随机整数 Math.random()*(m-n+1)+n
int r = (int)(Math.random()*31+30);
//Arrays 数组工具类
int[] a = new int[3];
a[0] = 123;
a[1] = 345;
a[2] = 166;
Arrays.sort(a);//为现有数组排序
for (int j = 0; j < a.length; j++) {
System.out.println(a[j]);
}
//Date 日期类
//可以获取当前时间
//可以把时间转为毫秒值(时间戳)
//可以设置新时间
Date nowdate = new Date();
System.out.println(nowdate);
nowdate.getHours();
nowdate.getMinutes();
nowdate.getSeconds();
//可以把时间转为毫秒值(时间戳)
nowdate.getTime();
//可以设置新时间
Date fdate = new Date(2021,4,16);
Date f2date = new Date();
f2date.setHours(12);
f2date.setMinutes(30);
//字符串工具类
String string = new String("abcdefg");
string.charAt(3);//根据下标拿第几个字符
string.concat("hijklmn");//拼接字符串
System.out.println(string+"hijklmn");
String[] split = string.split("d"); //以目标字符 分割字符串为数组
for (int j = 0; j < split.length; j++) {
System.out.println(split[j]);
}
string.replace("a", "A");
string.toLowerCase();//大小写转换
string.toUpperCase();
//String 和 StringBulider 和 StringBuffer的区别
//String固定长度 不可以变
//StringBulider长度可变 但是 线程不安全 效率高
//StringBuffer 线程安全 效率低
//集合和泛型
//set list map的异同
//set 无序 不可重复 HashSet(哈希散列) TreeSet(二叉树)
//list 有序 课重复 ArrayList(数组) LinkedList(双向循环链表)
//map 无序 键值对 HashMap(哈希散列) TreeMap(二叉树)
HashSet<String> hashSet = new HashSet<String>();
hashSet.add("张三");
hashSet.add("李四");
hashSet.add("赵六");
Iterator<String> iterator = hashSet.iterator();
while (iterator.hasNext()) {
String string2 = iterator.next();
System.out.println(string2);
}
//IO 考试不考
//File对象 文件关联 可以创建路径 创建文件 可以删除文件
//FileInputStream 字节输入
//FileOutputStream 字节输出
//FileReader 字符输入
//FileWriter 字符输出
//BufferedInputStream 自带缓冲区 字节输入 线程安全
//BufferedOutputStream 自带缓冲区 字节输出 线程安全
//BufferedReader 自带缓冲区 字符输入 线程安全
//BufferedWriter 自带缓冲区 字符输出 线程安全
//异常的处理
//try carch finally 捕获 处理异常 最终执行
//throw 扔异常 谁调用这个方法 谁处理异常
try {
FileInputStream fileInputStream = new FileInputStream("./src/abc.txt");
byte[] b = new byte[1024];
int len;
while ((len = fileInputStream.read(b))!=-1) {
String string2 = new String(b);
System.out.println(string2);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//线程
//实现线程的方式
//1、继承Thread类
//2、实现runnable接口
//问 run()和 start()的区别
//在线程中 主要运行的内容 全部写在 run()里面
//启动线程 用 start() 如果启动run()运行的是非线程
//线程的生命周期
//线程就绪 线程运行 线程阻塞 线程结束
try {
Thread.sleep(2000);//线程等待
Thread.yield();//线程让步
//join()线程加入
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//synchronized 同步 (线程锁)
//JDBC
Cat cat = new Cat();
cat.eat();
cat.sleep();
Dog dog = new Dog();
dog.eat();
}
}
class Animal{
public String name;
//构造函数
Animal(){//方法的重载
}
Animal(String name){//方法的重载
this.name = name;
}
public void eat(){
System.out.println("动物吃");
}
public void sleep(){
System.out.println("动物睡");
}
}
class Cat extends Animal{
}
//overwrite
class Dog extends Animal{
public void eat(){//重写
super.eat();//调用父类的内容
System.out.println("狗在吃");
}
}