栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

static,instanceof,强制转换,导包

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

static,instanceof,强制转换,导包

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


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

    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/332613.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号