数据类型 (一) 基本数据类型 1. 数值类型
2. boolean类型 (二) 引用数据类型 1. 类 2. 接口 3.数组 类型转换 低—————————————————————>高 byte,short,char–>int–>long–>float–>double
变量 (一) 变量类型
(二) 变量命名规范
所有变量、方法、类名:见名知意
类成员变量:首字母小写和驼峰原则:monthSalary除了第一个单词以外,后面的单词首字母大写
局部变量:首字母小写和驼峰原则
常量:大写字母和下划线:MAX_VALUE
类名:首字母大写和驼峰原则:Man,GoodMan
方法名:首字母小写和驼峰原则:run(),runRun()
常量 1 2 static final double PI = 3.14 ;
基本运算符 1 2 3 4 5 6 7 8 int a = 10 ;int b = 20 ;System.out.println(a+b+"" ); System.out.println("" +a+b);
包机制 1 2 3 4 package pk1.pk2.pk3....;import package1.pk2.pk3....;
Doc文档
JAVA流程控制 (一) Scanner 1 2 3 4 5 6 7 8 9 10 11 12 Scanner scanner = new Scanner (System.in);System.out.println("使用next()接收:" ); if (scanner.hasNext()){ String str = scanner.next(); System.out.println("输出内容为:" +str); } scanner.close();
1.next:
一定要读取到有效字符后才可以结束输入。
对输入有效字符之前遇到的空白,next()方法会自动将其去掉。
只有输人有效字符后才将其后面输入的空白作为分隔符或者结束符。
next()不能得到带有空格的字符串
2.nextLine:
以Enter为结束符,也就是说nextLine()方法返回的是输入回车之前的所有字符
可以获得空白。
1 2 3 4 5 6 7 8 9 Scanner scanner = new Scanner (System.in);System.out.println("请用户输入:" ); String str = scanner.next();System.out.println("输出内容为:" +str); scanner.close();
3.Scanner进阶 1 2 3 4 5 6 7 8 9 10 11 12 Scanner scanner = new Scanner (System.in); double sum = 0 ;double x;int n = 0 ;while (scanner.hasNextFloat()) { x = scanner.nextFloat(); sum = sum+x; n++; } System.out.println("平均为:" +sum/n);
(二) 顺序结构 (三) 循环结构 增强for循环
1 2 3 4 for (int i : arry) { System.out.println(i); }
方法
java方法是语句的集合,它们在一起执行一个功能。
方法是解决一类问题的步骤的有序组合
方法包含于类或对象中
方法在程序中被创建,在其他地方被引用
非静态方法可以访问类中的静态方法
静态方法只可调用静态方法 不可调用非静态方法
方法的重构:同一方法名,但是传递参数的类型 不相同
可变参数 1 public static void add (double ...a)
数组 1 2 3 4 5 6 7 8 int [] a = {1 ,2 ,3 ,4 ,5 };int [] arry;arry = new int [10 ]; arry[0 ] = 1 ;
冒泡排序 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public static int [] sort(int [] array){ int temp = 0 ; for (int i = 0 ; i < array.length - 1 ; i++) { for (int j = 0 ; j < array.length - 1 - i; j++) { if (array[j+1 ]<array[j]) { temp=array[j]; array[j] = array[j+1 ]; array[j+1 ] = temp; } } } return array; }
稀疏矩阵 “C:\Users\31849\Desktop\文档\学习笔记\JavaSE\稀疏矩阵-java实现.md”
类与对象 对象的创建与使用
必须使用new 关键字创建对象,构造器 Person Mike = new Person()
对象的属性 : Mike.age
对象的方法: Mike.sleep()
构造器 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 public class Person { String name; int age; public Person () { } public Person (String name, int age) { this .name = name; this .age = age; } }
封装
属性私有 get/set (快捷键alt+insert)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class Person { private String name; private int age; public String getName () { return name; } public void setName (String name) { this .name = name; } }
继承
pubic
private
protected
default
1 2 3 4 5 6 public class Student extends Person {}
Super
super调用父类的构造方法,必须在构造方法的第一个
super必须只能出现在子类的方法或者构造方法中!
super和this不能同时调用构造方法!
父类没有无参构造器,子类也无法进行无参构造
重写
需要有继承关系,子类重写父类的方法
方法名必须相同
参数的列表必须相同
修饰符:可以扩大,但是不能缩小。public>protected>Default>private
抛出的异常:范围可以被缩小,但是不能扩大;ClassNotFoundException –> Exception
1 2 3 4 5 6 7 8 9 10 11 12 public class Person { public void say () { System.out.println("Person->say" ); } }
多态
同一个方法可以根据发送的对象(父/子)的不同采用多中不同的行为方式
多态是方法的多态,属性没有多态
**父类和子类,需要有联系 ,注意类型转换异常 **
stastic:静态方法,属于类,不属于实例
final:常量,无法改变,重写
private:私有方法
存在条件:继承关系,方法需要重写,父类引用指向子类对象; Father f1 = new Son();
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public static void main (String[] args) { Student s1 = new Student (); Person s2 = new Student (); Object s3 = new Student (); s1.say(); s2.say(); Student }
instance of 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Person s2 = new Student ();System.out.println(s2 instanceof Student); System.out.println(s2 instanceof Person); System.out.println(s2 instanceof Object); System.out.println(s2 instanceof Teacher); System.out.println(s2 instanceof String); ((Student)s2).eat();
static关键字 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class Person { { } static { } public Person () { } }
抽象类
继承了抽象类的所有方法(包括抽象方法)的子类,必须实现它的方法,除非子类也是抽象类
不可以new这个抽象类,只能通过子类来实现
抽象方法必须定义在抽象类中
抽象类中可以写普通方法
抽象类例如模板,子类继承后,需要自己实例化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 public abstract class Person { public abstract void something () ; }
接口
接口就是多个类的公共规范
接口是一种引用数据类型,最重要的内容就是其中的:抽象方法
1 2 3 4 5 6 7 8 public interface 接口名称{ public abstract int methodAbs1 () ; int methodAbs2 () ; }
接口的实现 1 2 3 4 5 6 7 8 9 10 demo1AbstractImpl abs1 = new demo1AbstractImpl ();
接口的默认方法 1 2 3 4 5 6 7 8 9 public default 返回值类型 默认方法名称(){ }
接口中的静态方法
不能通过实现类对象来调用接口中的静态方法
只能通过接口名.静态方法名(参数)来调用
接口中的私有方法 1 2 3 4 5 6 7 8 9 10 private void common () { System.out.println("abc" ); System.out.println("def" ); } private static void common () { System.out.println("abc" ); System.out.println("def" ); }
接口中的成员变量
必须由public static final 修饰 (可以省略)
必须进行赋值
名称应为大写字母
1 public static final int NUM = 10 ;
接口实现注意事项
接口的多继承
内部类 成员内部类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public class Person { public void doing () { System.out.println("我是外部类方法" ); } public class Inner { public void in () { System.out.println("我是内部类方法" ); Person.this .doing(); } } } Person person = new Person ();Inner inner = person.new Inner ();Person.Inner p = new Person ().new Inner ();
匿名内部类
直接在方法中使用匿名内部类,将抽象方法实例化,获得实例对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public abstract class Student { public abstract void test () ; public static void main (String[] args) { Student student1 = new Student () { @Override public void test () { System.out.println("我是匿名内部类" ); } };
lambda 表达式
接口内部 仅有一个待实现的抽象方法(不包括默认方法)
仅能传入隐式变量(不改变的变量,final关键字)
1 2 StudentInterface student3 = (返回值) -> {方法体}
方法引用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 StudentInterface student = (a,b) -> a + b;StudentInterface student2 = Integer::sum; Student2 student2 = new Student2 (); StudentInterface student = student2::test;
异常
1 2 3 4 5 6 7 8 9 10 private static int test1 (int a,int b) { if (b==0 ) throw new ArithmeticException ("除数不能为0" ); return a/b; } private static void test2 () throws Exception{ throw new Exception ("我是编译时异常!" ); }
捕获异常 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 try { test1(10 ,0 ); }catch (ArithmeticException e){ System.out.println("捕获到异常" ); } System.out.println("程序正常运行" ); } try { test1(10 ,0 ); }catch (ArithmeticException e){ System.out.println("捕获到数学运算异常" ); }catch (RuntimeException e){ System.out.println("捕获到运行异常" ); }catch (Exception e){ System.out.println("捕获到异常" ); }finally { System.out.println("捕获完毕" ); }
泛型类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public class Fan <T,U....> { String name; int age; T value; U ability; public Fan (String name, int age, T value {//有参构造 this .name = name; this .age = age; this .value = value; } public T getValue() { return value; } } Fan<String> stringFan = new Fan <>("张三" , 6 , "优秀" );