instanceof运算符
instanceof运算符是二元运算符,左边是对象,右边是类;当对象是右边类或子类所创建的对象时,返回true,否则,返回false。
package com.oop;
import com.oop.demo06.Person;
import com.oop.demo06.Student;
import com.oop.demo06.Teacher;
public class Application {
public static void main(String[] args){
Object object = new Student();
System.out.println(object instanceof Student);//true
System.out.println(object instanceof Person);//true
System.out.println(object instanceof Object);//true
System.out.println(object instanceof Teacher);//false
System.out.println(object instanceof String);//false
System.out.println("--------------------------");
Person p = new Student();
System.out.println(p instanceof Student);//true
System.out.println(p instanceof Person);//true
System.out.println(p instanceof Object);//true
System.out.println(p instanceof Teacher);//false
//System.out.println(p instanceof String);//编译报错!由于对象p与String没有关系
System.out.println("--------------------------");
Student s = new Student();
System.out.println(s instanceof Student);//true
System.out.println(s instanceof Person);//true
System.out.println(s instanceof Object);//true
//System.out.println(s instanceof Teacher);//编译报错
//System.out.println(s instanceof String);//编译报错
System.out.println("--------------------------");
}
}
类型转换
- 把子类转换成父类为向上转型:Parent f = new Child();
- 把父类转换成子类为向下转型,需强制转型:(Child)f
- 子类转换成父类,可能会丢失自己本来的一些方法。
- 父类转换成子类后,才能调用子类独有的方法。
static关键字
- 被static关键字修饰的方法或者变量不需要依赖于对象来进行访问,只要类被加载了,就可以通过类名去进行访问。
- static可以用来修饰类的成员方法、类的成员变量,另外可以编写static代码块来优化程序性能。
static方法
- static方法就是没有this的方法。在static方法内部不能调用非静态方法,反过来是可以的。而且可以在没有创建任何对象的前提下,仅仅通过类本身来调用static方法。这实际上正是static方法的主要用途。
- static方法一般称作静态方法,由于静态方法不依赖于任何对象就可以进行访问,因此对于静态方法来说,是没有this的,因为它不依附于任何对象,既然都没有对象,就谈不上this了。并且由于这个特性,在静态方法中不能访问类的非静态成员变量和非静态成员方法,因为非静态成员方法/变量都是必须依赖具体的对象才能够被调用。
- 虽然在静态方法中不能直接访问非静态成员方法和非静态成员变量,但是在非静态成员方法中是可以直接访问静态成员方法/变量的。
静态方法为什么不能直接调用非静态方法?
因为静态方法与类相关,类一加载静态方法就存在了,而非静态方法与对象相关,此时非静态方法并不存在,所以在静态方法里直接调用非静态方法会出现编译错误。若想在静态方法里调用非静态方法,需在调用方法之前实例化对象。
参见–方法的调用
为什么main方法为静态方法?
因为程序在执行main方法的时候没有创建任何对象,因此只有通过类名来访问。
static变量
- static变量也称作静态变量,静态变量和非静态变量的区别是:静态变量被所有的对象所共享,在内存中只有一个副本,它当且仅当在类初次加载时会被初始化。而非静态变量是对象所拥有的,在创建对象的时候被初始化,存在多个副本,各个对象拥有的副本互不影响。
- static成员变量的初始化顺序按照定义的顺序进行初始化。
package com.oop.demo06;
public class Student {
private static int age;//静态变量
private double score;//非静态变量
public void run(){
go();
System.out.println("非静态方法");
}
public static void go(){
//run();//编译出错
System.out.println("静态方法");
}
public static void main(String[] args) {
go();//静态方法调用静态方法
Student.go();//静态方法调用静态方法,与上一代码同义
//run();//静态方法调用非静态方法,编译出错
new Student().run();//先实例化对象,再调用非静态方法
System.out.println("------------");
System.out.println(age);//调用静态变量
System.out.println(Student.age);//调用静态变量
//System.out.println(score);//调用非静态变量,编译出错
System.out.println(new Student().score);//调用非静态变量
}
}
static代码块
- static关键字还有一个比较关键的作用就是 用来形成静态代码块以优化程序性能。static块可以置于类中的任何地方,类中可以有多个static块。在类初次被加载的时候,会按照static块的顺序来执行每个static块,并且只会执行一次。
- 很多时候会将一些只需要进行一次的初始化操作都放在static代码块中进行。
package com.oop.demo06;
public class Person {
//2
{
//代码块(匿名代码块)
System.out.println("匿名代码块");
}
//1
static{
//静态代码块
System.out.println("静态代码块");
}
//3
public Person() {
System.out.println("构造方法");
}
public void run(){
System.out.println("run");
}
public static void main(String[] args) {
System.out.println("-----------");
Person person1 = new Person();
System.out.println("-----------");
Person person2 = new Person();
}
}
运行结果:
静态代码块
-----------
匿名代码块
构造方法
-----------
匿名代码块
构造方法
由上述运行结果可知,静态代码块最先运行、与对象无关且只运行一次。
静态导入包
观察两个代码的差异
package com.oop.demo06;
public class Test {
public static void main(String[] args) {
System.out.println(Math.random());
//System.out.println(random());//编译出错
System.out.println(Math.PI);
}
}
package com.oop.demo06;
import static java.lang.Math.random;
import static java.lang.Math.PI;
public class Test {
public static void main(String[] args) {
System.out.println(random());
System.out.println(PI);
}
}
注意事项
- Java中的static关键字不会影响到变量或者方法的作用域。在Java中能够影响到访问权限的只有private、public、protected(包括包访问权限)这几个关键字。
- 对于静态方法来说没有this,那么在非静态方法中能够通过this访问静态成员变量。
public class Main {
static int value = 33;
public static void main(String[] args) throws Exception{
new Main().printValue();
}
private void printValue(){
int value = 3;
System.out.println(this.value);
}
}
this代表当前对象,那么通过new Main()来调用printValue的话,当前对象就是通过new Main()生成的对象。而static变量是被对象所享有的,因此在printValue中的this.value的值毫无疑问是33。在printValue方法内部的value是局部变量,根本不可能与this关联,所以输出结果是33。在这里永远要记住一点:静态成员变量虽然独立于对象,但是不代表不可以通过对象去访问,所有的静态方法和静态变量都可以通过对象访问(只要访问权限足够)。
3. static不允许用来修饰局部变量。