static,instanceof,强制转换,导包

这篇博客探讨了Java中的类型转换,包括从父类到子类的向上转型和从子类到父类的向下转型,强调了强制转换的风险。同时,详细介绍了`static`关键字的使用,包括静态变量、静态方法以及静态代码块的执行顺序,并展示了它们在类生命周期中的作用。此外,还提到了`instanceof`运算符在判断对象类型时的应用。
摘要由CSDN通过智能技术生成

强制转换。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();

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值