instanceof和类型转换
- 父类的引用指向子类的对象
- 把子类转换为父类,向上转型:不用强制转换(子类可直接调用父类公共的方法)
- 把父类转换为子类,向下转型:需要强制转换 (可能会丢失一些方法)
- 方便方法的调用,减少重复的代码!简洁
代码:(注释也要多看)
package com.Demo03;
public class Application {
public static void main(String[] args) {
/*
//Object > Person > Student
//Object > Person > Teacher
//Object > String
Object object = new Student();
//System.out.println(X instanceof Y); //能不能编译通过!看X和Y有没有关系(父子,曾祖等)
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 person = new Student();
System.out.println(person instanceof Student);//true
System.out.println(person instanceof Person);//true
System.out.println(person instanceof Object);//true
System.out.println(person instanceof Teacher);//false
//System.out.println(person instanceof String);//编译时就报错
System.out.println("==================================");
Student student = new Student();
System.out.println(student instanceof Student);//true
System.out.println(student instanceof Person);//true
System.out.println(student instanceof Object);//true
//System.out.println(student instanceof Teacher);//编译时就报错
//System.out.println(student instanceof String);//编译时就报错
*/
//类型之间的转换:父 子
//高 低
Person student = new Student();//子类转换为父类,可能丢失自己本来的一些方法;例如:Student类中的go()方法不能调用,
//student.go();//不行,需要类型转换
//student 将这个对象转换为Student类型,我们就可以使用Student类型的方法了
Student student1 = (Student) student;
student1.go();//go
//上面两行代码的总结
((Student) student).go();
}
}
package com.Demo03;
public class Student extends Person{
public void go(){
System.out.println("go");
}
}
package com.Demo03;
public class Teacher extends Person{
}
package com.Demo03;
public class Person {
public void run(){
System.out.println("run");
}
}
static关键字
在代码中总结:(代码中的注释要多看)
- 被static修饰的变量
package com.Demo04;
//static
public class Test {
private static int age;//静态的变量 多线程会更加了解
private double score;//非静态的变量
public static void main(String[] args) {
Test t1 = new Test();
System.out.println(t1.score);
System.out.println(t1.age);
//System.out.println(Test.score);//非静态变量不能直接使用类名直接访问
//静态的变量可以使用类名直接访问 类名.变量名;
System.out.println(Test.age);
}
}
- 被static修饰的方法
package com.Demo04;
public class Test1 {
public void run(){
System.out.println("run非静态方法");
}
public static void go(){
System.out.println("go静态方法");
}
public static void main(String[] args) {
new Test1().run();
new Test1().go();
//Test1.run();//非静态方法不能直接使用类名直接访问,也不能 方法名();来直接访问
//run();
//静态方法可以使用类名直接访问 类名.方法名(); 或者 方法名();
Test1.go();
go();
}
}
- 匿名代码块,静态代码块
package com.Demo04;
public class Test2 {
//第二个执行 匿名代码块也是自动创建 也可以使用这种方式 赋初始值
{
System.out.println("匿名代码块");
}
//第一个执行 只能执行一次
static {
System.out.println("静态代码块");
}
//第三个执行
public Test2(){
System.out.println("构造方法");
}
public static void main(String[] args) {
Test2 t2 = new Test2();
System.out.println("==========");
Test2 test2 = new Test2();
}
}
- 静态导入包(了解即可)
package com.Demo04;
//静态导入包
import static java.lang.Math.random;
import static java.lang.Math.PI;
public class Test3 {
public static void main(String[] args) {
System.out.println(Math.random());
System.out.println(random());//静态导入包后可以直接使用random(),不需要Math.random()
System.out.println(PI);
}
}