Java学习
instanceof
a instanceof Class
- instanceof 用来测试一个对象是否为一个类的实例
- 对象必须为引用类型,不能是基本类型
- 如果a能转换成右边的class类型,则返回true,否则返回false
public class Application {
public static void main(String[] args) {
//Object > Person > Teacher
//Object > Person > Student
//Object > String
Object object = new Student();
System.out.println(object instanceof Student);//T,父强转子
System.out.println(object instanceof Person);//T父强转子
System.out.println(object instanceof Object);//T
System.out.println(object instanceof Teacher);//F,无关
System.out.println(object instanceof String);//F,无关
System.out.println("==========================");
Person person = new Student();
System.out.println(person instanceof Student);//T父强转子
System.out.println(person instanceof Person);//T
System.out.println(person instanceof Object);//T子转父
System.out.println(person instanceof Teacher);//F无关
//System.out.println(person instanceof String);//编译错误
System.out.println("==========================");
Student student = new Student();
System.out.println(student instanceof Student);//T
System.out.println(student instanceof Person);//T子转父
System.out.println(student instanceof Object);//T子转父
// System.out.println(student instanceof Teacher);//编译错误
// System.out.println(student instanceof String);//编译错误
}
}
--------------------------------------------------------
public class Person {
public void run(){
System.out.println("run");
}
}
--------------------------------------------------------
public class Student extends Person{
}
--------------------------------------------------------
public class Teacher extends Person{
}
static
public class Student {
private static int age;//静态 多线程!
private double score;//非静态
public void run(){
//go();//非静态可以直接调用静态方法
// 因为static是和Student类一起加载,Student创建出来static也存在
}
public static void go(){
//静态可以调用静态的方法,但不能调用非静态
//不能调用run();Student加载的时候还没有run方法
}
public static void main(String[] args) {
Student s1 = new Student();
System.out.println(Student.age);//直接用类名,一眼可以看出是静态
// System.out.println(Student.score);//非静态要用对象去调用
System.out.println(s1.age);
System.out.println(s1.score);
//============================================
go();//静态方法直接调用
s1.run();//非静态要用对象来调用
}
}
- 代码块、构造器、static
public class Person {
//第二
{
//匿名代码块
//在构造器之前就存在了
//可以赋初值
System.out.println("匿名代码块");
}
//第一
static{
//静态代码块,可以初始化一些东西
//和类一起加载,永久只执行一次,且是最早执行的
System.out.println("静态代码块");
}
//第三
public Person() {
System.out.println("构造器");
}
public static void main(String[] args) {
Person p1 = new Person();
System.out.println("====================");
Person p2 = new Person();
}
}
- 新知识
//静态导入包
import static java.lang.Math.*;//.*直接导入所有方法
public class Test {
public static void main(String[] args) {
System.out.println(Math.random());//导包前
System.out.println(random());//导包后
}
}
final
- 被final关键字修饰的类不能成为任何类的父类**(final后断子绝孙)**
抽象类
- 抽象类被继承的时候,子类必须重写抽象类中所有的抽象方法
- 抽象类不能被实例化对象,但可以实例化抽象类的子类,因为他的子类重写了抽象方法不再是抽象的。
- 抽象类可以写普通方法,但抽象方法一定写在抽象类
- 虽然抽象类不能new,但是存在构造器
- 抽象类的存在可以让我们更好的对现实世界建模,对一系列的看上去有差别但其实本质一样的东西归类
//abstract 抽象类
public abstract class Action {
//约束~有人帮我们实现~
//抽象方法,只有方法名字,没有方法的实现
public abstract void doSomething();
public Action() {
System.out.println("抽象类构造器");
}
}
------------------------------------------------------
//抽象类被继承的时候,子类必须重写抽象类中所有的抽象方法
public class A extends Action{
@Override
public void doSomething() {
}
public A(){
super();
}
public static void main(String[] args) {
A a = new A();
System.out.println(a);
}
}
接口
- 接口就是规范,定义的是一组规则,无法自己写方法,约束和实现分离
- implements 实现接口
- 实现类必须实现接口中的方法
- 一个类可以实现多个接口
想成为架构师就要有非常强的抽象思维!
接口作用:
1.接口是一种规则,也就意味着实现接口的类要遵守
2.接口只定义规则,实现要在实现接口的类中完成
3.方法都是public abstract
4.属性都是public static final
5.接口不能被实例化,因为接口不是类,没有构造方法
6.implements可以实现多个接口
7.必须实现接口中所有的方法
内部类
-
内部类就是在一个类的内部再定义一个类
-
成员内部类
public class Outer { private int id = 10; public void out(){ System.out.println("外部类的方法"); } public class Inner{ public void in(){ System.out.println("内部类的方法"); } //可以获得外部类的私有属性 public void getOuterId(){ System.out.println(id); } } } ------------------------------------------------- public class Application { public static void main(String[] args) { //先new一个Outer Outer outer = new Outer(); //再通过外部类来实例化内部类 Outer.Inner inner = outer.new Inner();//成员内部类 inner.in(); inner.getOuterId();//10 } }
-
静态内部类
-
局部内部类
方法里的class,类似于局部变量
public class Outer { //局部内部类 public void method(){ class Inner{ public void in(){ } } } }
-
匿名内部类
public class Test { public static void main(String[] args) { //匿名初始化类,不用将实例保存到变量中 new Apple().eat(); //匿名内部实现类 new UserService(){ @Override public void hello() { } }; } } class Apple{ public void eat(){ System.out.println("Apple"); } } interface UserService{ void hello(); }
-
异常机制
- Exception:程序里面的意外
- 检查性异常:程序员无法预见的。如打开一个不存在文件时异常发生。编译时不能被简单地忽略
- 运行时异常:运行时异常是可能被程序员避免的异常。编译时可以被忽略
- 错误:错误不是异常,而是脱离程序员控制的问题。通常在代码中被忽略
public class Test {
public static void main(String[] args) {
int a = 1;
int b = 0;
try {
new Test().test(1,0);
} catch (ArithmeticException e) {
e.printStackTrace();
}
//捕捉多个异常要从小到大
//Ctrl + Alt + T
try {//try监控区域
// if(b==0){
// throw new ArithmeticException();//throw主动抛出异常
// //已经try...catch就没有throw了
// }
System.out.println(a/b); //如果异常执行catch
}catch(Error e){
System.out.println("Error");
} catch(Exception e){//捕获异常
System.out.println("Exception");
}catch(Throwable t){
System.out.println("Throwable");
} finally {//处理善后工作,可有可无
System.out.println("finally");
}
}
public void test(int a, int b)throws ArithmeticException{
//throw主动抛出异常
if(b==0){
throw new ArithmeticException();//一般用于方法中
}
}
}
自定义异常
- 只需要继承Exception
//自定义异常类
public class MyException extends Exception{
//传递数字>10
private int detail;
public MyException(int a) {
this.detail = a;
}
//toString:异常的打印信息
@Override
public String toString() {
return "MyException{" +
"detail=" + detail +
'}';
}
}
-------------------------------------------------------
public class Test {
//写一个存在异常的方法
static void test(int a) throws MyException {
System.out.println("传递的参数为:"+ a);
if(a>10){
throw new MyException(a);//可抛出也可捕获
}
System.out.println("ok");
}
public static void main(String[] args) {
try {
test(11);
} catch (MyException e) {
//增加一些处理异常的代码
System.out.println("MyException=>"+ e);
}
}
}
狂神总结
- 处理运行时异常时,采用逻辑去合理规避同时辅助try-catch处理
- 在多重catch快后面,可以加一个catch(Exception)来处理可能会被遗漏的异常
- 对于不确定的代码,也可以加上try-cath,处理潜在异常
- 尽量去处理异常,切记只是简单地调用printStackTrace()去打印异常
- 根据业务需求和异常类型去处理异常
- 尽量添加finally语句块去释放占用的资源