整数字面量为整型(int)
小数字面量为双精度浮点型(double)
数据类型:
byte short int long float double
接下来代码展示理解
public class Test{
char c = 'a';
switch(c){
case 'b':
System.out.println('b');
break;
case 'c':
System.out.println('c');
break;
case 'a':
System.out.println('a');
break;
default:
System.out.println('d');
}
}
}
swith( char byte short int)只允许四种类型
public class Test{
public static void main(String args[]){
int score = 90;
if(score > 85 && score <= 100){
System.out.println("成绩为优");
}
else if(score > 75 && score <= 85){
System.out.println("成绩为良");
}
else if(score > 60 && score <= 75){
System.out.println("成绩为中");
}
else if(score <= 60 && score >= 0){
System.out.println("成绩为差");
}
else if(score > 100 || score < 0){
System.out.println("成绩不在正常的范围之内");
}
}
}
for(int i = 0; i < 10; i++){
System.out.println(i);
}
public class Test{
public static void main(String args[]){
int i = 0;
while(i < 10){
System.out.println(i);
i++;
}
}
}
打印100-200的素数:
class Test{
public static void main(String args[]){
for(int i = 100; i < 201; i++){
boolean b = false;
for(int j = 2; j < i-1; j++){
int k=i%j;
if(k==0){
b=true;
}
}
//如果不是true就打印出素数
if(!b){
System.out.println(i);
}
}
}
}
public class Test{
public static void main(String args[]){
int i = 5;
int j = i++ + 5;
System.out.println(i);
System.out.println(j);
}
}
j=10;
i=6;
int j = ++i + 5;
System.out.println(j);
j=11;
i=6;
&逻辑与
&&短路与
class Test{
public static void main(String args[]){
for(int i=1; i<5; i++){
for(int j=0; j<4-i; j++){
System.out.print(" ");
}
for(int k=0; k
面向对象是一种编程方式
面向对象是一种思维方式
面向对象不是一种编程语言
掌握一门面向对象语言的语法
掌握面向对象的思维方式
熟悉面向对象设计原则
掌握面向对象设计模式
确定谁来做,其次确定怎么做
考虑整体,其次考虑局部
首先考虑抽象,其次考虑具体
不要认为掌握了一门面向对象语言就是掌握了面向对象
习惯了将面向对象与现实世界做比较
对象和对象的引用
定义类的方法
class类名
{
属性;
方法;
}
属性也叫成员变量,主要用于描述类的状态
方法也叫成员方法,主要用于描述类的行为
格式:类名 对象名=new 类名() 例如:Dog dog = new Dog(); 创建一个Dog的引用 创建一个Dog的对象
类和对象的关系 类是抽象,而对象是具体的。
变量的类型 变量的值
Java的数据类型,基本数据类型和引用数据类型
对象就是引用数据类型
生成对象的方法
Java虚拟机把内存分为栈内存和堆内存
对象就叫引用数据类型
应该如何学习面向对象
面向对象的思维方式
class Test{
public static void main(String args[]){
Dog d = new Dog();
d.name="哈哈";
d.age=2;
d.jump();
System.out.println("名字是"+d.name);
}
}
对象的使用方法,多对象的创建方法,匿名对象的创建和使用方法。 对象的使用方法 使用对象调用变量和函数 对象.变量 对象.函数()
生成多个对象 匿名对象的使用 可以不定义对象的引用名称,而直接调用这个对象的方法,这样的对象叫做匿名对象 如:new
Dog().jump();
重载的表达
class A{
void funA(){
System.out.println("没有参数的funA函数");
}
void funA(int i){
System.out.println("有参数的funA函数");
}
void funA(int i,double d){
System.out.println("拥有两个参数的funA函数");
}
}
class Test{
public static void main(String args[]){
A a = new A();
a.funA();
a.funA(1,2.0);
}
}
两个或者多个函数在同一个类当中; 函数名相同; 参数列表不同
A(){
}
使用this调用成员变量和成员函数 使用this调用构造函数
静态成员变量只有一份 在静态函数当中不能使用this 静态代码块的主要作用是为静态成员变量赋值
class Person{
static{
System.out.println("dd"):
}
static String name;
..
}
在现实世界当中,继承就是儿子得到老子的东西,在面向对象的世界当中,继承就是一个类得到了另一个类当中的成员变量和成员方法;
Java当中只支持单继承
Java只支持单继承,不允许多继承
使用继承是为了减少重复代码
子类实例过程:
生成子类过程:
使用super调用父类构造函数的方法
class Person{
String name;
int age;
Person(){
System.out.prinltn("Person的无参数构造函数");
}
Person(String name,int age){
this.name=name;
this.age=age;
System.out.println("Person有参数的构造函数");
}
void eat(){
System.out.println("定义吃饭的方法");
}
}
class Student extends Person{
//子类继承父类
Student(){
//父类
super();
System.out.println("Student的无参数构造函数");
}
Student(String name,int age,int id){
super(name,age);
this.id=id;
}
}
class Test{
public static void main(String args[]){
Student student = new Student();
}
}
虽然子类不能继承父类的构造函数,但我能用super()来调用父类的构造函数。
调用子类的构造函数,一定会调用父类的构造的函数。
使用super调用父类的成员函数
在具有父子关系的两个类当中,父类和子类各有一个函数,这两个函数的定义(返回值类型,函数名,参数列表)完全相同
对象的转型(多态性地体现)
什么是向上转型?向上转型就是将子类的对象赋值给父类的引用。 什么是向下转型?向下转型就是将父类的对象赋值给子类的引用。
Student s = new Student();
Person p = s;
//一个引用能够调用哪些成员(变量和函数),取决于这个引用类型 //一个引用调用的是哪一个方法,取决于这个引用所指向的对象
class Student extends Person{
String address;
void introduce(){
super.introduce();
System.out.println("我家在"+address);
}
}
class Test{
public static void main(String args[]){
String s = new Student();
Person p = s;
p.name = "hhh";
p.age = 20;
p.introduce();
}
}
向下转型:
Student s1 = new Student();
Person p = s1;
Student s2 = (Student)p;
类是抽象的,对象是具体的,比类更抽象的,就是抽象类
先抽象,后具体
只有函数的定义,没有函数体的函数被称为抽象函数
abstract void test();
使用abstract定义的类被称之为抽象类 抽象类不能够生成对象; 如果一个类当中包含有抽象函数,那么这个类必须被声明为抽象类
如果一个类当中没有抽象函数,那么这个类也可以被声明为抽象类
抽象类不能实例化,继承抽象类,那么该类必须为抽象类
如何调用抽象类,用复写的方法
abstract class Person{
abstract void eat();
}
class Chinese extends Person{
void eat(){
System.out.pritln("hhh");
}
}
class Test{
public static void main(String args[]){
Person p = new Chinese();
p.eat();
}
}
代码:
abstract class Person{
Person(){
System.out.println("Person没有参数的构造函数");
}
Person(String name,String age){
this.name=name;
this.age=age;
}
String name;
int age;
void introduce(){
System.out.println("我的名字是"+name+",我的年龄是"+age);
}
abstract void eat();
}
}
class Chinese extends Person{
String address;
Chinese(){
super();
System.out.println("Chinese的构造函数");
}
Chinese(String name,int age,String address){
super(name,age);
this.address=address;
}
void eat(){
//复写
System.out.println("吃饭");
}
}
class Test{
public static void main(String args[]){
Person p = new Chinese();
p.eat();
}
}
虽然不能调用抽象类的构造函数,但是可以通过用子类来调用抽象函数,生成子类对象的时候使用调用构造函数。抽象函数是没有函数体的函数,用abstract来定义抽象类。
Java中的访问权限 public:公共权限 private:私有权限 default:默认权限 protected:受保护权限
public > protected > default > private
接口中的方法都是抽象方法,public权限,全是抽象函数,不能生成对象
interface Student{
public void read();
public void write();
}
class ChineseStudent implements Student{
//复写
public void read(){
System.out.println("read");
}
public void write(){
System.out.println("write");
}
}
class Test{
public static void main(String args[]){
ChineseStudent chinesestudent = new ChineseStudent();
Student student = chinesestudent;
student.read();
student.write();
}
}
实现接口用implements关键字,一个接口可以实现多个接口,一个接口可以继承多个接口
interface Student{
public void read();
public void write();
}
interface Teacher{
public void teach();
public void test();
}
class Person implements Student,Teacher{
public void read(){
System.out.println("read"):
}
public void write(){
System.out.println("write");
}
public void teach(){
System.out.println("teach"):
}
public void test(){
System.out.println("test"):
}
}
class Test{
public static void main(String args[]){
Person person = new Person();
Student student = person;
student.read();
student.write();
Teacher teacher = person;
teacher.teach();
teacher.close();
}
}
工厂方法模式:
interface Printer{
public void open();
public void close();
public void print(String s);
}
class Printer1 implements Printer{
public void open(){
System.out.println("printer1 open");
}
public void close(){
System.out.println("printer1 close");
}
public void print1(String s){
System.out.println("print1"+s);
}
}
class Printer2 implements Printer{
private void clean(){
System.out.println("printer2 clean");
}
public void close(){
this.clean();
System.out.println("print2 close");
}
public void open(){
System.out.println("print2 open");
}
public void print(String s){
System.out.println("print2"+s);
}
}
class Test{
public static void main(String args[]){
//根据用户的选择
printer.open();
printer.print("test");
printer.close();
}
}
工厂
class PrinterFactory{
public static Printer getPrinter(int flag){
Printer printer = null;
//int flag = 0;
if(flag == 0){
printer = new printer1();
}
else(flag == 1){
printer = new CanonPrinter();
}
return printer;
}
}
class Test{
public static void main(String args[]){
//Printer gerPrinter(int flag)
int flag = 1;
Printer printer = PrinterFactory.getPrinter(flag);
printer.open();
printer.print("test");
printer.close();
}
}
Java中的异常
什么是异常?
try...catch...finally结构的使用方法
class Test{
public static void main(String args[]){
try{
int i = 1 / 0;
}
catch(Exception e){
e.printStackTrace();
}
finally{
System.out.println("finally");
}
System.out.println(5);
}
}
class Test{
public static void main(String args[]){
try{
Thread.sleep(1000);
}
catch(Exception e){
e.printStackTrace();
}
}
}
throw和throws的作用区别:
用代码表示:
class Person{
private int age;
public void setAge(int age) throws Exception{
if(age<0){
RuntimeException e = new RuntimeException("年龄不能小于0");
throw e;
}
this.age = age;
}
}
class Test{
public static void main(String args[]){
Person person = new Person();
try{
person.setAge(-1);
}
catch(Exception e){
System.out.println(e);
}
}
}
throws Exception谁调用谁处理
IO分为三种:
第一种: 输入流 输出流 第二种: 字节流 字符流 第三种分类: 节点流 处理流
IO当中的核心类
InputStream OutputStream FileInputStream FileOutputStream
字节流的核心类
InputStream OutputStream
记住这个
InputStream:
int read(byte[] b,int off,int len)
OutputStream:
void write(byte[] b,int off,int len)
FileInputStream
class Test{
public static void main(String args[]){
FileInputStream fis = null;
try{
fis = new FileInputStream("e:/read.txt");
byte[] buffer = new byte[100];
fis.read(buffer,0,buffer.length);
for(int i = 0;i
节点流和处理流
处理流使用实例
节点流与处理流的关系
一行一行的读取
BufferedReader使用方法,生成BufferedReader对象的方法
BufferedReader int = new BufferedReader(new FileReader(“from.int”));
处理流,Reader,Writer以及他们所有的子类都属于字符流
BufferedReader属于字符流,处理流,然后呢?它又是处理流
FileReader 和 BufferedReader
class Test{
public static void main(String args[]){
FileReader fileReader = null;
BufferReader bufferReader = null;
try{
fileReader = new FileReader("e:/read.txt");
bufferedReader = new BufferedReader(fileReader);
String line = null;
while(true){
line = bufferedReader.readLine();
if(line == null){
break;
}
System.out.println(line);
}
//String line = bufferedReader.readLine();
//System.out.println(line);
}
catch(Exception e){
System.out.println(e);
}
finally{
try{
bufferedReader.close();
fileReader.close();
}
catch(Exception e){
System.out.println(e);
}
}
}
}
装饰者模式
interface worker{
public void doSomeWork();
}
class worker1 implements worker{
public void doSomeWork(){
System.out.println("work1");
}
}
class worker2 implements worker{
public void doSomeWork(){
System.out.println("work2");
}
}
class A implements worker{
private Worker worker;
public A(Worker worker){
this.worker=worker;
}
public void doSomeWork(){
System.out.println("哈哈");
worker.doSomeWork();
}
}
class Test{
public static void main(String args[]){
Worker1 worker1 = new Worker1();
A a = new A(worker1);
a.doSomeWork();
Worker2 worker2 = new Worker2();
A a2 = new A(worker2);
a2.doSomeWork();
}
}
什么是内部类?
class A{
class B{
}
}
如何生成内部类的对象?
class Test{
public static void main(String args[]){
A a = new A();
A.B b = new A().new B();
//或者A.B b = a.new B();
}
}
class A{
int i;
class B{
int j;
int funB(){
int result = i+j;
return result;
}
}
}
class Test{
public static void main(String args[]){
A a = new A();
A.B b = a.new B();
a.i=3;
a.j=1;
int result = b.funB();
System.out.println(result);
}
}
class A{
int i;
class B{
int j;
int funB(){
int result = A.this.i+this.j;
return result;
}
}
}
匿名内部类
interfacce A{
public void doSomething();
}
class B{
public void fun(A a){
System.out.println("B函数");
a.doSomething();
}
}
class Work implements A{
public void doSomething(){
System.out.println("doSomething");
}
}
class Test{
public static void main(String args[]){
Work work = new Work();
A a = work;
B b = new B();
b.fun(a);
}
}
匿名内部类
class Test{
public static void main(String args[]){
B b = new B();
b.fun(new A(){
public void doSomething(){
System.out.println("匿名内部类");
}
});
}
}
Java中的线程
多进程:就是指在操作系统中能同时运行多个任务程序
多线程:在同一个应用程序中多个顺序流同时执行
创建线程的方法
定义一个线程类,它继承类Thread并重写其中的方法run();
方法run()称为线程体;
由于Java只支持单继承,用这种方法定义的类不能再继承其他类。
class FirstThread extends Thread{
public void run(){
for(int i=0;i<100;i++){
System.out.println("FirstThread"+i);
}
}
}
class Test{
public static void main(Sting args[]){
FirstThread ft = new FirstThread();
ft.start();
for(int i = 0; i<100;i++){
System.out.println("main"+i):
}
}
}
提供接口Runnable的类作为线程的目标对象,在初始化一个Thread类或者Thread子类的线程对象时,把目标对象传递给这个线程实例,由该目标对象提供线程体。
class Test implements Runnable{
public void run(){
for(int i = 0;i<100;i++){
System.out.println("Runnable"+i);
}
}
}
class Test{
public static void main(String args[]){
Test test = new Test();
Thread t = new Thread(test);
System.out.println(t.getPriority());
t.start();
}
}
中断线程
Thread.sleep();
Thread.yield();//让出自己正在使用的CPU
设置线程的优先级
getPriority();
setPriority();
class Test implements Runnable{
public void run(){
for(int i = 0;i<100;i++){
System.out.println("Runnable"+i);
if(i==50){
try{
Thread.sleep(2000);
}
catch(Exception e){
System.out.println(e);
}
}
}
}
}
class Test{
public static void main(String args[]){
RunnableImp1 ri = new RunnableImp1();
Thread t = new Thread(ri);
t.setPriority(Thread.MAX_PRIORITY);
//t.setPriority(Thread.MIN_PRIORITY);
t.start();
System.out.println(t.getPriority());
}
}
class Test{
public static void main(String args[]){
MyThread myThread = new MyThread();
Thread t1 = new Thread(myThread);
Thread t2 = new Thread(myThread);
t1.setName("线程1");
t2.setName("线程2");
//分别启动
t1.start();
t2.start();
}
}
class MyThread implements Runnable{
int i = 100;
public void run(){
while(true){
System.out.println(Thread.currentThread().getName()+i);
i--;
Thread.yield();
if(i<0){
break;
}
}
}
}
//同步代码块
class MyThread implements Runnable{
int i = 100;
public void run(){
while(true){
synchronized(this){
System.out.println(Thread.currentThread().getName()+i);
i--;
Thread.yield();
if(i<0){
break;
}
}
}
}
}
深入synchronized关键字
class Service{
public void fun1(){
synchronized(this){
try{
Thread.sleep(3*1000);
}
catch(Exception e){
System.out.println("fun1");
}
}
public void fun2(){
synchronized(this){
System.out.println("fun2");
}
}
}
class MyThread1 implements Runnable{
private Service service;
public MyThread1(Service service){
this.service = service;
}
public void run(){
service.fun1();
}
}
class MyThread2 implements Runable{
private Service service;
public MyThread2(Service service){
this.service = service;
}
public void run(){
service.fun2();
}
}
class Test{
public static void main(String args[]){
Service service = new Service();
Thread t1=new Thread(new MyThread1(service));
Thread t2=new Thread(new MyThread2(service));
t1.start();
t2.start();
}
}
同步锁 锁住的是service
同步方法,同步代码块锁住this
class Service{
public synchronized void fun1(){
try{
Thread.sleep(3*1000);
}
catch(Exception e){
System.out.println(e);
}
System.out.println("fun1");
}
public void fun2(){
synchronized(this){
System.out.println("fun2");
}
}
}
JAVA当中的数组
class Test{
public static void main(String args[]){
//数组的静态声明
int arr [] = {5,2,7,8,9,0};
arr[3] = 10;
//System.out.println(arr[3]);
for(int i = 0;i<5;i++){
System.out.println(arr[i]);
}
}
}
class Test{
public static void main(String args[]){
int arr[] = {2,4,6,7,8};
System.out.println(arr.length);
}
}
数组的动态声明
class Test{
public static void main(String args[]){
//动态声明
int arr [] = new int [10];
System.out.println("arr数组长度"+arr.length);
for(int i = 0;i
类集框架是一组类和接口,位于java.util包,主要用户存储和管理对象,主要分为三大类—集合,列表和映射。
集合中的对象不按特定的方式排序,并且没有重复对象;
对象是没有顺序的,集合是没有顺序的
集合中对象按照索引位置排序,可以有重复的对象。
可以按照顺序取,也可以指定取。
集合中的每一个元素包含一个键对象和一个值对象,键不可以重复,值可以重复。
数据结构 键值对
interface
Iterator Collection
ListIterator List Set Map
LinkeList ArrayList HashSet SortedSet HashMap SortedMap
LinkedHashSet TreeSet LinkedHashMap TreeMap
Comparable Comparator Collections Arrays
//arrayList默认10,可无限长,关于泛型
public class Test{
public static void main(String args[]){
//ArrayList arrayList = new ArrayList();
ArrayList arrayList = new ArrayList();
arrayList.add("a");
arrayList.add("b");
arrayList.add("c");
//String s = arrayList.get(1);
//System.out.println(s);
for(int i=0;i<3;i++){
String s = arrayList.get(i);
System.out.println(s);
}
}
}
优化
public class Test{
public static void main(String args[]){
ArrayList arrayList = new ArrayList();
arrayList.add("a");
arrayList.add("b");
arrayList.add("c");
arrayList.add("d");
for(int i = 0; i
类集框架
集合 无序 不可重复
列表 有序 可重复
映射
Collection和Iterator接口
Set与HashSet的使用方法
boolean add(Object o) 向集合当中加入一个对象
void clear() 删除集合当中的所有对象
boolean isEmpty() 判断集合是否为空
remove(Object o) 从集合中删除一个对象的引用
int size() 返回集合中元素的数目
Set继承了Collection
public class Test{
public static void main(String args[]){
//HashSet hashSet = new HashSet();
//Set set = new HashSet();
//别管就是转,方便
Set set = new HashSet();
set.add("a");
set.add("b");
set.add("c");
set.add("d");
int i = set.size();
System.out.println(i);
}
}
不可以重复
public class Test{
public static void main(String args[]){
//HashSet hashSet = new HashSet();
//Set set = new HashSet();
//别管就是转,方便
Set set = new HashSet();
boolean b1 = set.isEmpty();
System.out.println(b1);
set.add("a");
set.add("b");
set.add("c");
set.add("d");
set.add("c");
boolean b2 = set.isEmpty();
System.out.println(b2);
int i = set.size();
System.out.println("clear之前的长度"+i);
set.clear();
int j = set.size();
System.out.println(j);
}
}
取数据,迭代 iterate器 (Iterator)
public class Test{
public static void main(String args[]){
//HashSet hashSet = new HashSet();
//Set set = hashSet;
//Iterator <-- Collection <-- Set <-- HashSet
//hasNext() next()
Set set = new HashSet();
set.add("a");
set.add("b");
set.add("c");
set.add("d");
set.add("c");
Iterator it = set.iterator();
boolean b1 = it.hasNext();
if(b1){
String s = it.next();
System.out.println(s);
}
boolean b2 = it.hasNext();
if(b2){
String s = it.next();
System.out.println(s);
}
}
}
迭代器的使用
it.hasNext();
还有没有下一个元素,如果这个游标后面有元素就返回true,否则,false;
it.next();
返回游标所指位置的下一个元素,取出,用hasNext()看有没有,next取
优化
public class Test{
public stattic void main(String args[]){
Set set = new HashSet();
set.add("a");
set.add("b");
set.add("c");
set.add("d");
set.add("c");
Iterator it = set.iterator();
while(it.hasNext()){
String s = it.next();
System.out.println(s);
}
}
}
Map
什么是映射(Map)
映射中的每一个元素包含一个键对象和一个值对象,键不可以重复,值可以重复。
public class Test{
public static void main(String args[]){
HashMap hasMap = new HashMap();
Map map = hasMap;
map.put("1","a");
map.put("2","b");
map.put("3","c");
map.put("4","d");
int i = map.size();
System.out.println(i);
}
}
public class Test{
public static void main(String args[]){
HashMap hasMap = new HashMap();
Map map = hasMap;
map.put("1","a");
map.put("2","b");
map.put("3","c");
map.put("4","d");
map.put("3","e");
int i = map.size();
System.out.println(i);
String s = map.get("3");
System.out.println(ss);
}
}
equals函数的作用
复写equals函数的方法
“==”操作符的作用
User u1 = new User();
User u2 = new User();
User u3 = u1;
equals比较两个对象的内容是否相等
对象的内容相等需要符合两个条件:
1.对象的类型相同(可以使用instanceof操作符进行比较);
2.两个对象的成员变量的值完全相同;
class Test{
public static void main(String[] args){
User u1 = new User();
User u2 = new User();
boolean b1 = u1.equals(u2);
System.out.pritln(b1);
}
}
作者Info:
【作者】:Jeskson
【原创公众号】:达达前端小酒馆。
【转载说明】:转载请说明出处,谢谢合作!~
关于目前文章内容即涉及前端,PHP知识点,如果有兴趣即可关注,很荣幸,能被您发现,真是慧眼识英!也感谢您的关注,在未来的日子里,希望能够一直默默的支持我,我也会努力写出更多优秀的作品。我们一起成长,从零基础学编程,将 Web前端领域、数据结构与算法、网络原理等通俗易懂的呈现给小伙伴。分享 Web 前端相关的技术文章、工具资源、精选课程、热点资讯。
若本号内容有做得不到位的地方(比如:涉及版权或其他问题),请及时联系我们进行整改即可,会在第一时间进行处理。
这是一个有质量,有态度的博客