数组
public class Homework2_5 {
public static void main(String[] args) {
int[] arrayInt = new int[3];
int[] intArr = new int[]{5, 6, 8, 20};
long[] arrayLong = {};
int[][] arrayInts = {{1, 2}, {3, 3}};
String[][] arrayStrins = new String[3][2];
arrayStrins[0][0] = "hello";
ArrayList<String> arrayLisy = new ArrayList<>(); //不定长度
arrayLisy.add("hellen");
arrayLisy.get(0);
arrayLisy.remove(0);
// 增强for循环
for (long aaa : arrayLong) {
System.out.println(aaa);
}
}
}
JDBC连接
public class JDBC {
/* private static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver";//oracle*/
private static final String DBDRIVER = "com.mysql.cj.jdbc.Driver";//mysql
private static final String DBURL = "jdbc:mysql://127.0.0.1:3306/cpssys";//oracle:jdbc:oracle:thin@hostname:port:databasename
private static final String USER = "root";
private static final String PASSWORD = "123456";
public static void main(String[] args) throws Exception{
//第一步:加载数据库驱动
Class.forName(DBDRIVER);
//第二步:获取数据库连接
Connection conn = DriverManager.getConnection(DBURL, USER, PASSWORD);
System.out.println(conn);
//第三步:使用Connection来创建一个Statement对象
Statement stmt = conn.createStatement();
/*
String sql = "insert into javatest values('444','555','666')";
int len = stmt.executeUpdate(sql);
System.out.println("被影响的行:"+len);
*/ String sql = "select * from javatest where bbb ='555'";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()){
System.out.println(rs.getString(1) + "\t"
+ rs.getString(2) + "\t"
+ rs.getString(3)
);
}
conn.close();
}
}
文件操作
public class Day2_5 {
public static void main(String[] args) throws IOException {
String fileNameStr = "C:\\Users\\file.txt";
File file = new File(fileNameStr);
if(!file.exists()){
file.createNewFile();
}
//写入文件
BufferedWriter ccc = new BufferedWriter(new FileWriter(fileNameStr));//往文件中写入
ccc.write("输出[1,100]随机生成整型数据:");
ccc.newLine();
//生成随机数字写入文件
Random aaa = new Random();
for(int i=0;i<100;i++){
int randInt = aaa.nextInt(100)+1;//[1,100]随机生成整型数据
ccc.write(String.valueOf(randInt));
if(i<99){
ccc.newLine();
}
}
// ccc.write("码同学,下周也要坚持听课,复习,作业,笔记,编程");
ccc.flush();//将输出流中缓冲的数据全部写入到磁盘
ccc.close();
//读取文件
BufferedReader bufferedReader = new BufferedReader(new FileReader(fileNameStr));
String lineStr = "";
while((lineStr=bufferedReader.readLine())!= null){
System.out.println(lineStr);
};
bufferedReader.close();
}
}
HashMap
public class Day2_1 {
public static void main(String[] args) {
HashSet hashSeq = new HashSet();
hashSeq.add("sss");
HashSet<Integer> hashSeq1 = new HashSet<>();
hashSeq1.add(5);
// 迭代器
Iterator iii = hashSeq.iterator();
while (iii.hasNext()) {
System.out.println(iii.next());
}
HashMap<String, String> hashMao = new HashMap<>();
hashMao.put("name", "aaaa");
hashMao.get("name");
hashMao.containsValue("aaaa");
Set<String> aaa = hashMao.keySet();
for (String bbb : aaa) {
System.out.println(bbb);
System.out.println(hashMao.get(bbb)
);
}
Set<Map.Entry<String, String>> ddd = hashMao.entrySet();
for (Map.Entry<String, String> ccc : ddd)
System.out.println(ccc.getKey() + ":" + ccc.getValue());
}
}
StringBuilder
- StringBuffer 多个线程使用
- StringBuilder 快但线程不安全
public class Day2_2 {
public static void main(String[] args) {
String s = "abcRainbow123mtx";
StringBuilder builder = new StringBuilder();
builder.append(s);//追加
System.out.println(builder);
builder.delete(0, 3);//jdk删除
builder.replace(7, 10, "2022");//替换
System.out.println(builder);
builder.insert(11, "这年");
System.out.println(builder);//System.out.println(builder.reverse());//反转
StringBuilder builder1 = new StringBuilder();
char[] chars = builder.toString().toCharArray();
for (int i = chars.length - 1; i >= 0; i--) {
builder1.append(chars[i]);
}
System.out.println(builder1)
StringBuffer stringBuffer = new StringBuffer("mtx");
boolean b = true;
int i = 100;
String str = "go";//char
stringBuffer.append(b);
stringBuffer.append(i);
stringBuffer.append(str);
stringBuffer.insert(0, 2022);//插入
stringBuffer.setCharAt(2, 'F');//替换
stringBuffer.deleteCharAt(2);//删除第2位的字符
System.out.println(stringBuffer);
//增删改查
System.out.println(stringBuffer.length());//快捷简写 sout//自定义
//逆序
System.out.println(stringBuffer.reverse());
//清空
//stringBuffer.setLength(0);
stringBuffer.delete(0, stringBuffer.length());
System.out.println(stringBuffer.length())
}
}
类和对象
public class Fridge {
//属性,类的成员变量
int height;
int weight;
String color;
//行为,功能,方法、函数
void open(){
System.out.println("open");
}
void save(){
System.out.println("save");
}
void close(){
System.out.println("close");
}
public static void main(String[] args) {
Fridge fridge = new Fridge();
fridge.height = 2;
fridge.weight =1;
fridge.color = "red";
System.out.println(fridge.color);
fridge.open();
fridge.save();
fridge.close();
}
}
实例方法
public class MethodStudy {
public void chu(double m,double n,double o){//定义了chu方法 //入参 参数 参数列表
double result = 0;
try {
result = m/n;
result = result/o;
} catch (ArithmeticException exception){
System.out.println("分母不能为0");
}
catch (Exception exception) {
exception.printStackTrace();
}finally {
System.out.println(result);
}
// void 改成 int //方法的重载(方法名一样,参数类型和个数不同,)
public int chu(int m,int n){//定义了chu方法 //入参 参数 参数列表
int result = 0;
try {
result = m/n; //局部变量
} catch (ArithmeticException exception){
System.out.println("分母不能为0");
}
catch (Exception exception) {
exception.printStackTrace();
}finally {
//System.out.println(result);
return result;//推荐
}
}
public static void main(String[] args) {
//方法,成员方法、实例方法
//要调用实例方法,必须先创建实例对象,然后通过对象来调用其实例方法
MethodStudy methodStudy = new MethodStudy();
System.out.println(methodStudy.chu(500,50));
//返回值 return 定义方法 , void
//注意点 返回值 没有返回值 其他都相同 是重载?不是
}
}
构造方法
1.构造函数,名称是跟类名同名
2.没有返回值 没有void
3.构造函数不能使用static final synchronized abstract native
4.初始化一个对象,自动创建一个无参的构造函数
pubic Person(){}
public class Person{
public Person(String name){
System.out.println(name);
}
public static void main(String[] args) {
Person person = new Person("hellon")
}
}
public class Person {
public String name;
public int age;
//构造函数
public Person(){
System.out.println("name="name+",age"+age);
}
//重载
public Person(String nameStr,int ageInt){
name = nameStr;
age = ageInt;
System.out.println("name="name+",age"+age);
}
public static void main(String[] args) {
Person person = new Person;
Person person2 = new Person("hellon","14");
}
}
构造函数调用实例方法,实例方法可以对此调用
public class Person {
public String name;
public int age;
public void cry(){
System.out.println("cry....");
}
//构造函数
public Person() {
System.out.println("name="name + ",age" + age);
cry();
}
//重载
public Person(String nameStr, int ageInt) {
name = nameStr;
age = ageInt;
System.out.println("name="name + ",age" + age);
cry();
}
public static void main(String[] args) {
Person person = new Person;
Person person2 = new Person("hellon", "14");
person2.cry();//有对象后才可以调用
}
}
多个实例方法与构造方法
特殊的实例函数set,get
public class Person {
private String name;
private int age;
//特殊的实例函数set 改名
public void setName(String nameStr){
name = nameStr;
}
//特殊的实例函数get 获取name
public void getName(){
return name;
}
public void cry(){
System.out.println("cry....");
}
//构造函数
public Person() {
System.out.println("name="name + ",age" + age);
cry();
}
//重载
public Person(String nameStr, int ageInt) {
name = nameStr;
age = ageInt;
System.out.println("name="name + ",age" + age);
cry();
}
public static void main(String[] args) {
Person person = new Person;
Person person2 = new Person("hellon", "14");
person2.cry();//有对象后才可以调用
}
}
1.构造代码块 给对象进行初始化,对象一建立就运行,优先于构造函数运行的
2.构造代码块给所有对象初始化
3.构造函数给单个对象个性化初始化
public class Person {
private String name;
private int age;
{
cry();//构造代码块
}
//特殊的实例函数set 改名
public void setName(String nameStr){
name = nameStr;
}
//特殊的实例函数get 获取name
public void getName(){
return name;
}
public void cry(){
System.out.println("cry....");
}
//构造函数
public Person() {
System.out.println("name="name + ",age" + age);
}
//重载
public Person(String nameStr, int ageInt) {
name = nameStr;
age = ageInt;
System.out.println("name="name + ",age" + age);
}
public static void main(String[] args) {
Person person = new Person;
Person person2 = new Person("hellon", "14");
person2.cry();//有对象后才可以调用
}
}
局部变量和类的成员变量名称相同(构造方法的入参名和成员变量名称相同)
this 修饰
public class Person {
public String name;
public int age;
//构造函数
public Person(){
System.out.println("name="name+",age"+age);
}
//重载
public Person(String name,int age){
this.name = name;
this.age = age;
System.out.println("name="name+",age"+age);
}
public static void main(String[] args) {
Person person = new Person;
Person person2 = new Person("hellon","14");
}
}
类中的构造方法间的互相调用
在这里插入代码片
static静态修饰
当对象中出现共享数据时,该数据就应该被static修饰;
当方法内没有访问非静态数据、非静态方法时,该方法就应该被static修饰;
//static修饰的属性、方法
//随着类的加载而加载,生命周期最长
//优先于对象存在
//被所有对象所共享
//可以直接通过类名所调用
private String name;//姓名
private static String country = "CN";//国籍 内存
public void speak(){//
System.out.println(this.name+"=========="+country);
}
public static void good(){//反义词
System.out.println("good~");
}
//静态方法不能访问非静态方法、非静态成员变量
//静态方法不能使用this、super关键字
public static void speak2(){
System.out.println(this.name+"=========="+country);
//speak() 错误的用法,speak()是非静态方法
}
public static void main(String[] args) {
Chinese.good();//类.静态方法
Chinese dagou = new Chinese();
dagou.name = "大狗";
dagou.speak();//不可以 Chinese.speak();
}
}
继承 extends
父类public
public class Car {
public int num;
public String color;
public String brand;
public void run(){
System.out.println(num+"......"+color+".....run");
}
}
子类
public class SmallCar extends Car {//大型 中型 小型 都是 Car的子类
//相同的属性、行为的两个类,通过继承的方式在java
//父子
public static void main(String[] args) {
SmallCar smallCar = new SmallCar();
smallCar.num = 4;
smallCar.color = "黑色";
smallCar.brand = "奥迪";
smallCar.run();
}
}
父类private
提供方法供子类使用。
Alt+insert 生成set get方法
public class Car {
private int num;
private String color;
private String brand;
private void run(){
System.out.println(num+"......"+color+".....run");
}
public void runSub(){
this.run();//父亲给儿子提供的方法
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
}
子类
public class SmallCar extends Car {//大型 中型 小型 都是 Car的子类
//相同的属性、行为的两个类,通过继承的方式在java
//父子
public static void main(String[] args) {
SmallCar smallCar = new SmallCar();
smallCar.setNum(4);
smallCar.setColor("黑色");
smallCar.setBrand("奥迪");
smallCar.runSub();
}
}
重写 Override
- 子类重写父类的方法;子类的方法和父类的方法名字一样,内容不一样;
- 重写是多态的表现形式
父类
public class Person {//java书籍 挑重点 访问级别
public String name;
public int age;
public void doing(){
System.out.println("run .....");
}
}
子类
public class Student extends Person{//继承 父类Person 子类 Student
@Override
public void doing(){//子类重写了父类的该方法 复写
System.out.println("走.....");
}
public static void main(String[] args) {
Student hui = new Student();
hui.doing();
}
}
多态
子类对象的不同形态
public abstract class Animal {//抽象的父类;父类只知道子类应该包含怎样的方法,无法准确的知道子类如何实现这些方法;
// 无法使用new关键字来调用抽象类的构造器
// 含有抽象方法的类 ,只能被定义为抽象类
abstract void eat();//定义了一个抽象方法;在普通的方法上增加abstract修饰符,并去掉方法后花括号括起来的部分
}
public class Cat extends Animal{
@Override
public void eat() {
System.out.println("鱼");
}
public void catchMouse(){
System.out.println("抓老鼠");
}
}
类的转换
public class PolymorphicStudy {
// public static void function(Cat c){
// c.eat();
// }
public static void function(Animal a){//扩展性
a.eat();
}
//重点 多态 selenium appium
public static void main(String[] args) {
Animal a = new Cat();//子类型赋值给了父类型 (理解),引用类型的提升
a.eat();//把猫提升为动物, 向上转型
//function(a);
//子类的特有方法,
Cat c = (Cat)a;//将Animal a 转成子类对象 Cat c 向下转型
c.catchMouse();
//猫 可能用父类的共有行为,也可能用自己类的特有行为
Animal fuqin = new Animal() {
@Override
void eat() {
System.out.println("我也不知道吃什么");
}
};
//Cat mi = (Cat)fuqin;//不能这样做,将父类对象转成子类对象
//static 继承 多态 是子类对象的不同形态
}
}
抽象类 abstract
- 抽象类一般与继承一起用
- 抽象类abstract 不能直接实例化对象
public abstract class Employee {
//抽象类abstract 不能直接实例化对象
//成员变量 成员方法 构造函数
//继承抽象类,java是单继承,亲生父亲
private String nameStr;
private long phoneLong;
private String addressStr;
private double ratioDouble;//工资系数
private char leverChar;
//构造方法
public Employee(String nameStr, long phoneLong) {
this.nameStr = nameStr;
this.phoneLong = phoneLong;
}
//构造方法
public String show() {
if(addressStr == null){
addressStr = "";
}
if(!Character.isLetter(leverChar)){//字母 Character类 判断leverChar是不是26个字母
leverChar = 'g';
}
return "Employee{" +
"nameStr='" + nameStr + '\'' +
", phoneLong=" + phoneLong +
", addressStr='" + addressStr + '\'' +
", ratioDouble=" + ratioDouble +
", leverChar=" + leverChar +
'}';
}
//抽象方法没有方法体 >>"{}"包含的部分
public abstract double computePay();
}
抽象类abstract 不能直接实例化对象,必须要实现抽象类中的方法
接口 interface
- 特殊的抽象类
- 类和接口实现,一个类可以实现多个接口
- 类和类继承,一个类仅可以继承一个父类
public interface InterfaceStudy {
//接口interface
/*
* 抽象类中全部方法都是抽象方法
* 关键字 class interface
* 接口 常量public static final 抽象方法 public abstract
*
* */
public static final int NUM = 18;
public abstract String show();
public abstract String show(String str);
}
public class Study implements InterfaceStudy {
/*
* 类和类继承,一个类仅可以继承一个父类
* 接口
* 类和接口实现,一个类可以实现多个接口
* */
@Override
public String show() {
return "hehe";
}
@Override
public String show(String str) {
return str;
}
}
多实现
public interface Player {
String playing(String str);
}
public interface KongTiao {//多实现
int NUM = 1;
void makeCold();
void makeHot();
}
实现implements 两个接口 Player KongTiao
public class Ship implements KongTiao,Player {
@Override
public void makeCold() {
System.out.println(KongTiao.NUM);
System.out.println("船制冷");
}
@Override
public void makeHot() {
System.out.println(KongTiao.NUM);
System.out.println("船制热");
}
@Override
public String playing(String str) {
return str;
}
}
必须实现接口的方法,