什么是包
包就是文件夹,在实际开发中,是多级包,目的是为了将代码分层,不同的代码放在不同的包下
一些包的分类以及含义:
权限修饰符
四种权限修饰符:
private:私有的
默认修饰符:指没有任何修饰
protected:受保护的
public:公开的,公共的
在同一个包同一个类下 | 在同一个包的子类或同一个包的无关类中 | 在不同包的子类中 | 在不同包的无关类中 | |
private | √ | |||
默认修饰符 | √ | √ | ||
protected | √ | √ | √ | |
public | √ | √ | √ | √ |
四种权限修饰符的访问范围
private范围最小,其次默认修饰符,然后是protected,最大的范围是public
形式参数问题
方法的形式参数如果是引用类型,这个引用类型是一个具体类,调用方法的时候,实际参数需要这个类的具体对象
class Cat{
public void eat(){
System.out.println("所有的猫都要吃");
}
}
Class CatDemo{
public viod method(Cat cat){
cat.eat();
}
public Class CatTest{
public static void main(String [] args){
//调用CatDemo中的method方法
CatDemo cd = new CatDemo();
Cat c = new Cat();
cd.method(s);
}
}
方法的形式参数如果是引用类型,这个引用类型是一个抽象类,调用方法的时候,实际参数需要这个抽象类的子类对象
abstract class Animal{
public abstract void eat();
}
class AnimalDemmo{
public void show (Animal a){
a.eat;
}
}
class Cat extends Animal{
public void eat(){
System.out.println("猫需要吃");
}
}
class AnimalTest{
public static void mian(String [] args){
AnimalDemo ad = new AnimalDemo();
Animal an = new Cat();
ad.eat(an);
}
}
方法的形式参数如果是引用类型,这个引用类型是一个接口,调用方法的时候,实际参数需要这个接口的子实现类对象
interface Animal{
coid eat();
}
class AnimalDemo(){
public void function (Animal a){
a.eat;
}
}
class AnimalImpl implements Animal{
public void eat(){
System.out.println("所有动物都要吃");
}
}
public class AnimalTest{
//调用AnimalDemo中的function方法
public static void main(String [] args){
AnimalDemo ad = new AnimalDemo();
Animal a = new AnimalImapl();
ad.fuction(a);
}
}
1.引用类型作为返回值,返回得是当前这个类的对象
class Cat{
public void eat{
System.out.println("猫需要吃东西");
}
}
class CatDemo{
public Cat method{
return new Cat();//返回匿名对象的格式
//或者 Cat cat = new Cat();
//return cat;
}
}
public class ReturnDemo1 {
public static void main(String[] args) {
//调用CatDemo这个类里面的method方法
CatDemo cat = new CatDemo() ;
Cat c = cat.method(); // 这个方法的本质: new Cat();
c.eat() ;
}
}
2.返回值类型是抽象类,返回的是抽象类的子类对象
abstract class Animal{
public abstract void eat();
}
class AnimalDemo{
public Animal show {
return new Cat;
}
}
class Cat extends Animal{
public void eat(){
System.out.println("猫需要吃东西");
}
}
public class AnimalTest{
public static void main(String[] args){
AnimalDemo ad = new AnimalDemo();
Anilan animal = ad.show();
animal.eat();
}
}
3.返回值类型是接口,返回的是接口的子实现类对象
interface Mary{
void mary() ;
}
//定义一个类
class MaryDemo{
public Mary method(){ //方法的返回值是接口类型,接口实例化不了的!
return new WeddingCompany() ;
}
}
class WeddingCompany implements Mary{
@Override
public void mary() {
System.out.println("婚庆公司布置婚礼现场!");
}
}
public class ReturnDemo3 {
public static void main(String[] args) {
//调用MaryDemo类名的method方法
MaryDemo maryDemo = new MaryDemo() ;
Mary m = maryDemo.method();//method方法本质:创建子实现类对象 new WeddingCompany() ;
m.mary();
}
}
内部类
成员内部类:在类的成员位置的类,称为成员内部类
成员内部类的特点:
成员内部类的成员方法可以直接访问外部类的成员包括私有
外部类的成员方法:要去访问成员内部类的成员方法,必须通过成员内部类的对象来访问!
直接通过外部类访问内部类的成员方法
外部类名.内部类名 对象名 = 外部对象.内部类对象 ;
格式:
class test{
private String name;
class testA{
方法体;
}
}
局部内部类:在局部位置的类
格式:
不管是局部内部类/成员内部类: 里面的成员都可以直接访问外部类的成员包括私有
通过外部类直接访问局部内部类
在局部位置上,创建了局部内部类的对象,对象名调用成员方法
interface Student{
void show();
}
class StudentDemo{
public void method(){
Studnet s = new StudentImpl(){
public void show(){
System.out.printn("学生就是要学习");
}
l.show
}
}
}
class Test{
public static void main(String [] args){
StudentsDemo sd = new StudentDemo();
sd.method();
}
}
匿名内部类在开发中的应用
package com.qf.innerclass_02;
/**
* 匿名内部类在开发情况的使用
*
* 方法的形式参数如果是抽象类的情况,调用该方法,实际参数需要传递当前抽象类的子类对象
*
* 匿名内部类的本质:
* 代表继承了 该类(抽象类)或者实现类该接口的子类对象
* new 抽象类名或者接口名(){
*
* 重写抽象方法...
* } ;
*/
abstract class Person{
public abstract void work() ;//工作
}
class PersonDemo{
public void method(Person p){ //抽象类 作为形式参数,抽象类不能实例化
p.work();
}
}
//定义子类 继承自Person类,重写work
/*class Worker extends Person{
@Override
public void work() {
System.out.println("日日夜夜敲代码...");
}
}*/
//测试类
public class Test {
public static void main(String[] args) {
//调用PersonDemo类的method 方法
/* PersonDemo pd = new PersonDemo() ;
//抽象类多态:
Person p = new Worker() ;
pd.method(p)*/; // ---->p.work();
//方式2:匿名内部类
PersonDemo pd = new PersonDemo() ;
// new 抽象类名或者接口名(){
// *
// * 重写抽象方法...
// * } ;
pd.method(new Person(){
@Override
public void work() {
System.out.println("日日夜夜敲代码");
}
});
}
}
Java中的常用类
Object:所有类的父类,也称为超类,任何类默认继承自Object
常用的功能: public String toString():本身的含义 :返回对象的字符串表示形式(直接输出对象的名称,就是地址值)结果应该是一个简明扼要的表达.
Object类的equals
public boolean equals(Object obj):比较两个对象是否相同
==和equals区别
==:
基本类型:比较的是数据值是否相同
引用类型:比较的是地址值是否相同
equals():Object类的方法
public boolean equals(Object obj) {
return (this == obj); //this--->就是p obj---->向上转型 p2 ==:比较是两个对象地址值
}
equals比较的是成员信息是否相同int a = 10; int b = 20; boolean flag = (a==b); //false System.out.println(flag); String s1 = new String("hello"); String s2 = new String("hello"); flag = s1 ==s2; System.out.println(flag);//false flag = s1.equals(s2); System.out.println(flag);//true
String字符串
String类: 代表字符串,字符串是一个常量,一旦被创建,其值不改变(地址不变,除非重新赋值) ;
常用的String类的构造方法
public String() :空参构造
public String(char[] value):将字符数组构造出字符串
public String(char[] value,int index,int length):将一部分字符数组转换成字符串
public String(byte[] bytes) :将字节数组构造字符串
public String(String original):将一个字符串值存储进去,构造成一个字符串对象String str = new String();//空参构造 char [] chs ={'a','b','c'}; String str2 = new String(chs);//将char[]转成字符串 byte [] bytes ={1,2,3}; String str3 = new String(bytes);//将byte []转成字符串
字符串变量相加:常量池是否存在,不存在,开空间
字符串常量相加:先拼接,在常量池中找,有--->返回地址;否则,开辟空间
public class StringDemo3 { public static void main(String[] args) { String s1 = "hello" ; String s2 = "world" ; String s3 = "helloworld" ; System.out.println(s3 == (s1+s2)); //false System.out.println(s3.equals(s1+s2));//true System.out.println("--------------------------------------"); System.out.println(s3 == "hello"+"world") ; //true System.out.println(s3.equals("hello"+"world")) ;//true } }
String 对象是不可变的,所以它们可以被共享。String s1 = "abc" ;String s2 = "abc" ;// 内存中只有一个 "abc" 对象被创建,同时被 s1 和 s2 共享。
String常用功能
String作为引用类型,在方法中作为形式参数,和基本类型一样,随着方法的调用而存在,随着方法的结束而消失,不会影响实际参数
判断功能
String s1 = new String("hello");
String s2 = new String("HellO");
System.out.println(s1.equals(s2));//false
System.out.println(s1.equalsIgnoreCase(s2));//true
获取功能
String s1 = new String("hello world");
int x = s1.length();//获取字符串s1的长度
System.out.println(s1);
String str = s1.concat("HHHHHH");//将指定的字符串添加到s1后面
System.out.println(str);
char c = str.charAt(5);//返回str字符串第五处的字符
System.out.println(c);
int i = str.indexOf('H');//返回H第一次出现的索引值
String sub = str.substring(2);//截取功能,截取下标2包括2处后面所有元素
System.out.println(sub);
String sub1 = str.substring(6, 9);//截取从6到9下标的元素,包括6不包括下标9;
System.out.println(sub1);
转换功能
String s1 = new String("hello world");
char[] chars = s1.toCharArray();//将字符串转换成字符数组
for(int x = 0; x < chars.length; x++) {
System.out.println(chars[x]);
}
byte[] bytes = s1.getBytes();//将字符串转换成字节数组
for(int x = 0; x < bytes.length; x++) {
System.out.println(bytes[x]);
}
String replace = s1.replace("world", "WORLD");//将world替换成WORLD
System.out.println(replace);
分割功能
String s1 = new String("he|llo|world");
String[] split = s1.split("|");//根据|"|"将s1分割开
for(int x = 0; x < split.length; x++) {
System.out.print(split[x]); // he llo world
其他功能
public boolean isEmpty():判断字符串是否空(是否为空内容)
public boolean startsWdith(String prefix):判断此字符串是否一指定的字符串开头
public boolean endsWith(String suffix):判断此字符串是否一指定的字符串结尾
boolean contains(String s):判断字符串中是否包含指定的某个字符或者某个子字符串(按照第一个字符判断)
String s1 = new String("hello world");
boolean empty = s1.isEmpty();//判断是否为空内容
System.out.println(empty);
boolean h = s1.startsWith("h");//在是否以h开头
System.out.println(h);
boolean b = s1.endsWith("d");//判断是否以d开头
System.out.println(b);
boolean wang = s1.contains("wang");//判断是否含有wang
System.out.println(wang);
public String toLowerCase():将字符串转换成小写
public String toUpperCase():将字符串转换成大写
String s1 = new String("hello world");
String s = s1.toUpperCase();//将s1内容转换成大写
System.out.println(s);
String s2 = s.toLowerCase();//将s内容转换成小写
System.out.println(s2);
StringBuffer
线程安全的可变的字符串缓冲区,支持可变的字符序列
StringBuffer()://空参构造:创建一个支持缓冲区对象
StringBuffer(String str)://将指定的字符串存储在字符串缓冲区中
int length();//获取字符缓冲区的长度
StringBuffer sb = new StringBuffer();//空参构造
String str = "hello world";
StringBuffer sb1 = new StringBuffer(str);//将字符串储存在字符缓冲区中
int length = sb1.length();//获取字符串缓冲区的长度
StringBuffer常用的功能
添加
public StringBuffer append(任意类型数据):添加任何类型数据到字符串缓冲区中返回值字符串缓冲区本身
public StringBuffer insert(int offset,String xx):插入:在指定位置的前面插入一个新的字符序列
public StringBuffer reverse():将字符串缓冲区字符序列反转
StringBuffer sb = new StringBuffer();//空参构造
sb.append("hello world");//在空参构造的字符缓冲区中添加"hello world"
StringBuffer sb1 = sb.insert(0, "javaEE");//在sb字符缓冲区中的0下标前添加javaEE字符串
System.out.println(sb1);
StringBuffer reverse = sb1.reverse();//sb1字符串缓冲区反转
删除
StringBuffer sb = new StringBuffer();//空参构造
sb.append("hello world");//在空参构造的字符缓冲区中添加"hello world"
sb.deleteCharAt(0);//删除下标0处的字符
System.out.println(sb);
sb.delete(0,2);//删除下标0到下标2的字符
System.out.println(sb);
替换
StringBuffer sb = new StringBuffer();//空参构造
sb.append("hello world");//在空参构造的字符缓冲区中添加"hello world"
sb.replace(0,5,"JavaEE");//下标0-5的字符被JavaEE替换
System.out.println(sb);
截取
public String substring(int start)
StringBuffer和String的相互转换
//String 转换成StringBuffer
String s = "hello" ;
StringBuffer sb = new StringBuffer() ;
sb.append(s) ;//方式一:拼接,空参构造StringBuffer,在后面拼接字符串
//方式二 有参构造
StringBuffer sb2 = new StringBuffer(s) ;
System.out.println(sb2);
//StringBuffer 转换成String
StringBuffer buffer = new StringBuffer("helloworld") ;//字符序列helloworld,外面的类型StringBuffer
//方式1:String类的构造方法
public String(StringBuffer buffer) ;
String str = new String(buffer) ;
//方式2:public String toString() 显示的应该是字符串缓冲区中的实际的字符序列内容
String str2 = buffer.toString();
StringBuffer和String的区别String的特点:字符串常量,一旦创建,其值不改变; 不支持可变的字符序列StringBuffer:支持的可变的字符序列,而且线程安全的,---执行效率低,多线程环境会 用,单线程环境下用的StringBuilder这个类;方法的形式参数:String和基本类型效果一样;形式参数的改变不影响实际参数StringBuffer:作为方法的形式参数,形式参数的改变,直接影响实际参数;
Integer包装类
Integer it = new Integer(7);
Integer it1 = new Integer("85447");
System.out.println(it);
System.out.println(it1);
//int转Strint
//方式一 拼接字符
int a = 100;
System.out.println(a+" ");
//方式二 装箱
Integer i = new Integer(a);
String s = i.toString();
System.out.println(s);
//方式三 Integer类的静态方法:public static String toString(int i)
String str = Integer.toString(i);
System.out.println(str);
String 转int
String str = "100";
int i = Integer.parseInt(str);
System.out.println(i);
Character
构造方法:
Character(char value) :将一个char类型的字符值构造成Character类对象
常用成员方法:
public static boolean isUpperCase(char ch)//判断字符是否为大写字母字符
public static boolean isLowerCase(char ch)//判断字符是否为小写字母字符
public static boolean isDigit(char ch)// 判断字符是否为数字字符
char ch = 'a';
Character a = new Character('a');//将char类型的构造成Character的
System.out.println(Character.isLowerCase('a'));//判断是否为小写字母
System.out.println(Character.isUpperCase('A'));//判断是否为大写字符
System.out.println(Character.isDigit('5'));//判断是佛为数字
Random随机数构造
int nextInt(int n):[0,n) :范围
Random random = new Random() ;
int i = random.nextInt(100) +1;
System类的复制数组
int[] arr1 = {1,2,3,4,5} ;
int[] arr2 = {6,7,8,9,10} ;
System.arraycopy(arr1,1,arr2,2,2);//复制后的arr2 6,7,3,4,5
//public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
//bject src, 数据源(举例:原数组)
//int srcPos, 原数组中某个位置
//Object dest, 目的数据(目标数组)
//int destPos, 目标数组的某个位置
//int length 复制的长度
Date,DateFormat类
Date重点:日期文本格式和Date如何转换?
* String日期文本格式 "2022-8-13"------>java.util.Date 日期格式
* Date日期格式 转换成 String
*
* 使用中间桥梁:DateFormat类 是一个抽象类,不能实例化,jdk提供更具体的子类:SimpleDateFormat
*
* 构造方法
* public SimpleDateFormat(String pattern)
* yyyy:年 MM:月 dd:月中日期 HH:小时 mm:分钟 ss:秒
*
* 成员方法:
* Date---->String public String foramt(Date date) :格式化
*
* String--->Date: public Date parse(String dateStr) throws ParseException: 解析
* 这个本身可能出现异常,如果解析的字符串的格式和 pattern参数的模式匹配,就出错了,解析失败!
* public SimpleDateFormat(String pattern)
*
* String s = "2008-5-12" ;
* SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日") ;
* SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd ") ;
public class DateDemo2 {
public static void main(String[] args) throws ParseException {
//Date---->String :格式化操作
Date date = new Date() ; //创建当前系统时间日期对象
System.out.println(date);
//使用中间桥梁 public SimpleDateFormat(String pattern)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd") ;
// public String foramt(Date date) :格式化
String s = sdf.format(date);
System.out.println(s);
System.out.println("-------------------------------------") ;
//String--->Date: public Date parse(String dateStr) throws ParseException: 解析
String dataStr = "2008-5-12" ;
//创建中间桥梁 public SimpleDateFormat(String pattern)
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd") ;
//解析
Date d = sdf2.parse(dataStr);
System.out.println(d);
}
}
* Collection<E></>父接口 代表所有的单列集合,只能存储一种引用类型的集合
两个子接口 List<E>/Set<E>
ArrayList:经常默认使用的集合(效率高)
常用功能
Collection c = new ArrayList();
c.add("hello world"); //添加字符串元素
c.add("java");//添加字符串元素
c.remove("java");//删除元素
c.contains("hello");//判断是否包含hello这个元素,返回值是boolean类型
c.toArray();//将c转换成数组
//迭代器遍历
Iterator i = c.iterator();//Collection的迭代器
while(i.hasNext()){//hasNext()判断是否还有下一个元素
System.out.println(i.next());//打印下一个元素
}
泛型
泛型:<引用类型> 集合中使用的
1)创建集合的时候,就明确集合存储的引用类型的数据类型是什么!
2)可以有效防止程序不安全,通过的迭代器获取数据 next()--->返回值E (Object任意Java类型),
向下转型可能出问题,心里明确迭代器里面现在放的什么类型
3)不需要在强转了,避免了强转类型转换
eg:
Collection<String> c1 = new ArrayList<String>();//明确了c1中存储类型是String类型
List集合
List集合的特点:
- 元素可以重复
- 元素的存储和去除具有有序性
成员方法
List<String> list = new ArrayList<>();//创建List集合对象
list.add("hello world");//添加元素
list.add(0,"JavaEE");//在指定下标前添加元素;
System.out.println(list.get(0));//获取下标5处的内容
List<String> list = new ArrayList<>();//创建List集合对象
list.add("hello world");//添加元素
list.add("java");
list.add("python");
list.add("C");
for(int i = 0;i<list.size();i++){
System.out.println(list.get(i));
迭代
ListIterator listIterator() 列表迭代器
Collection<String> c = new ArrayList();
c.add("hello");
c.add("wordle");
c.add("aaaa");
c.add("javaEE");
Iterator <String>it = c.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
LinkedList集合
LinkedList集合:底层数据结构是一种链表,链表的特点:查询慢(每次需要冲链表头查询到末尾),
增删快;线程不安全的,不同步,执行比较高!
特有功能:
public void addFirst(E e):添加到链表的开头
public void addLast(E e):添加到链表的末尾
public E getFirst():获取链表的第一个元素
public E getLast():获取链表的末尾的元素
public E removeFirst():删除链表第一个元素
public E removeLast():删除链表的最后一个元素
public void push(E e):推送到链表开头(压栈)---等价于public void addFirst(E e):添加到链表的开头
public E pop():弹出最后一个元素(弹栈) 等于removeFirst() :将第一个元素删除并返回
LinkedList<String> link = new LinkedList<>();
link.addFirst("hello");//添加到链表开头
link.addLast("world");//添加到链表结尾
link.getFirst();//获取链表首个元素
link.getLast();//获取链表的最后一个元素
link.removeFirst();//删除链表的首个元素
link.removeLast();//删除链表的最后一个元素
Voctor
Vector集合底层数据结构数组,是List集合接口子实现类之一
特点:底层数据结构数组(查询,更新元素比较快,添加,删除比较麻烦的),线程安全的类---同步的---执行效率比较低(多线程环境下使用安全性比较大的类)
public void addElement(E obj):添加任意元素 如果E类型确定了,就只能添加这个数据类型
类似于集合的通用功能add(E obj)
public Enumeration<E> elements():获取组件,类似于Collection集合的Iterator iterator()迭代器
类似于Collection集合的Iterator iterator()迭代器
E nextElement() :获取下个元素
boolean hasMoreElements() 判断是有更多的元素
Set集合
Set集合的特点:保证元素唯一,而且不能保证迭代次序唯一,无序性(元素不重复,不能保证顺序)
HashSet<String> hs = new HashSet<>();
hs.add("hello");
hs.add("hello");
hs.add("world");
hs.add("world");
hs.add("A");
hs.add("B");
hs.add("BC");
System.out.println(hs);
//输出结果,无序,唯一
[A, BC, B, world, hello]
选择排序
首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。以此类推,直到所有元素均排序完毕.
int [] arr = {8,20,5,49,36};
System.out.println("排序前");
System.out.print("[");
for (int i = 0;i<arr.length;i++){
System.out.print(arr[i]+", ");
}
System.out.print("]");
System.out.println();
System.out.println(Arrays.toString(arr));
System.out.println("-------------------------排序后------------------------");
for(int i =0;i<arr.length-1;i++){
for (int x = i+1;x< arr.length;x++){
if(arr[i]>arr[x]){
int temp = arr[i];
arr[i] = arr[x];
arr[x] = temp;
}
}
}
System.out.println(Arrays.toString(arr));