instance()方法
判断两个类之间是否存在父子关系
1能否编译通过,看X和Y是否存在父子关系,存在则可以;
2结果是T OR F,看引用对象,如果X引用的对象是Y的子类,则为ture;
** //object >> person >> student
//object >> person >> teacher**
Object s1 = new student();
System.out.println(s1 instanceof person);
System.out.println(s1 instanceof student);
System.out.println(s1 instanceof Object);
System.out.println(s1 instanceof teacher);
System.out.println(s1 instanceof String);
System.out.println("===========================================");
person s2 = new student();
System.out.println(s2 instanceof person);
System.out.println(s2 instanceof student);
System.out.println(s2 instanceof Object);
System.out.println(s2 instanceof teacher);
//person类和string不属于同一分支,比较则会报错
//System.out.println(s2 instanceof String);//报错
System.out.println("===========================================");
student s3 = new student();
System.out.println(s2 instanceof person);
System.out.println(s2 instanceof student);
System.out.println(s2 instanceof Object);
//System.out.println(s2 instanceof teacher);报错
转换
父转子需要强制转换,子转父不需要,直接转,但可能丢失自己本来的方法
静态static
1:静态的方法和属性可以直接调用,非静态的方法和属性则需要创建对象去调用
2:静态代码块:在构造其前执行,顺序为静态代码块,匿名代码块,构造方法,static代码块只执行一次,匿名代码块可以赋初值
````java
public class person {
{
System.out.println("匿名代码快");
}
static{
System.out.println("静态代码块");
}
public person() {
System.out.println("gouzaoqi");
}
public class application {
public static void main(String[] args) {
person pes = new person();
System.out.println("====================================");
person pes1 = new person();
}
}
`静态代码块
匿名代码快
gouzaoqi
====================================
匿名代码快
gouzaoqi
抽象类接口
抽象类:
抽象类中方法可以写抽象方法,也可以写普通方法,但不能被创建,可以被继承,继承抽象类时,需要将其的抽象方法重写
接口:
interface,就像人间的法律,制定后大家都遵守
接口中的方法都是抽象类,即写方法时,可将public abstract 省略;