强制转换。instanceof类型属于的知识
package Oopt.Instanceof;
public class Application {
public static void main(String[] args) {
//高类型 低类型
Person xmls = new Student();
//转换为这个类型我们就可以使用这个类型的方法了
//高转低强制转换
// Student xmls1 = (Student) xmls;
// xmls1.go();
// xmls1.run();
//高转低强制转换
// ((Student)xmls) .go();
Student student = new Student();
student.go();
//子类转换为父类,可能丢失本来的一些方法;
Person person=student;
person.run();
/*
1.父类引用指向子类对象
2.把子类转换为父类,向上转型
3.把父类转换为子类,向下转型;强制转换
*/
}
}
//object类型
// //object>person>teacher
// //object>person>student
// 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 person=new Student();
// System.out.println(person instanceof Student);//true
// System.out.println(person instanceof Person);//ture
// 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);//ture
// System.out.println(student instanceof Object);//true
// // System.out.println(student instanceof Teacher);//false
几台导入包。
//被final修饰的类不可以被继承(static)
package Oopt.Static;
//静态导入包
import static java.lang.Math.PI;
import static java.lang.Math.random;
public final class Animal {//被final修饰的类不可以被继承
public static void main(String[] args) {
System.out.println(random());
System.out.println(PI);
}
}
关于static
package Oopt.Static;
public class Person {
private static int age;//静态变量
private double score;//非静态变量
public void run(){
//非静态的方法可以直接访问静态方法
go();
}
public static void go(){
System.out.println();
}
//静态方法不能直接调用非静态方法
public static void main(String[] args) {
}
}
不能用类名直接调用非静态变量
package Oopt.Static;
//加了static就是静态变量;
public class Student {
private static int age;//静态变量
private double score;//非静态变量
public static void main(String[] args) {
Student s1 = new Student();
System.out.println(Student.age);
// System.out.println(Student.score);//Non-static field 'score' cannot be referenced from a static context
System.out.println(s1.age);
System.out.println(s1.score);
}
}
static。调用顺序
package Oopt.Static;
public class Teacher {
//第二个
{
//代码块(匿名代码块)
System.out.println("匿名代码块");
}
//第一个执行和类一起加载,只被执行一次
static {
System.out.println("静态代码块");
}
//第三个
public Teacher() {
System.out.println("构造方法");
}
public static void main(String[] args) {
Teacher teacher = new Teacher();
System.out.println("============");
Teacher teacher1 = new Teacher();
}
}