instanceof,类型转换和static关键字

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);
    }
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

等慢慢

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值