多态和instanceof关键字

多态和instanceof关键字

多态

即同一方法可以根据发送对象的不同而采取不同行为。

一个对象的实际类型是确定的,但是可以指向对象的引用类型有很多。

多态存在的条件:

  • 类之间有继承关系。

  • 子类重写了父类的方法。

  • 声明父类对象的时候引用指向之类对象。

多态指方法的多态,属性没有多态性。

 

instanceof

instanceof关键字通常用来判断一个引用类型的变量是否是另一个类型的子类或者间接子类,或一个接口的实现类,如果是则返回true,否则为false。

instanceof会将左边的变量尝试转换成右边的类型进行判断,所以不能判断完全不相干的变量和类型。

格式:变量名 instanceof 类型名

类示例代码:

public class Person {
​
}
public class Student extends Person {
​
}
public class Teacher extends Person {
​
}

 

主程序示例代码:

public class Application {
    public static void main(String[] args) {
        //声明一个Object对象接收Student对象。
        Object object = new Student();
        Person person = new Person();
​
        System.out.println(object instanceof Student);
        //打印结果:true
        System.out.println(object instanceof Person);
        //打印结果:true
        System.out.println(object instanceof Teacher);
        //打印结果:false
        System.out.println(object instanceof String);
        //打印结果:false
​
        System.out.println("==============================>");
​
        System.out.println(person instanceof Student);
        //打印结果:false
        System.out.println(person instanceof Person);
        //打印结果:true
        System.out.println(person instanceof Teacher);
        //打印结果:false
        System.out.println(person instanceof String);
        //报错java: 不兼容的类型: Person无法转换为java.lang.String
    }
}

通过以上示例可以发现,编译器会将instanceof左边的变量尝试转换成右边的类型去做判断,所以当类型不能转换时候,会报错误。

 

类型转换

子类对象转换成父类对象称为向上转型。

父类对象转换成子类对象称为向下转型。

类示例代码:

public class Person {
    public void father() {
        System.out.println("我是父类方法。");
    }
}
public class Student extends Person {
    public void son() {
        System.out.println("我是子类方法。");
    }
}

 

主程序示例代码:

public class Application {
    public static void main(String[] args) {
        //声明子类对象
        Student student = new Student();
        //子类可以使用自己的方法和父类方法
        student.son();
        student.father();
        
        //强制将子类对象转换成父类对象(冗余代码)
        ((Person)student).father();
        //强制转换后无法调用子类独有的方法,报错提示无法找到该方法
        ((Person)student).son();
    }
}
public class Application {
    public static void main(String[] args) {
        //声明父类对象
        Person person = new Person();
​
        //父类只能调用自己的方法
        person.father();
        //强制转换前无法调用子类独有的方法,报错提示无法找到该方法
        person.son();
​
        //强制转换后可以使用子类的方法和父类自己的方法(冗余代码)
        ((Student) person).son();
        ((Student) person).father();
    }
}

通过以上示例代码可以发现:

  1. 如无设计上的特殊要求子类的向上转换是冗余的,即不需要的。

  2. 同理,父类调用自己的方法也不需要转换类型。

  3. 子类向上转换成父类时,会丢失自己的独有方法。

  4. 父类只有向下转换成子类时才能使用子类的独有方法。

 

拓展

类加载相关

类代码:

public class Person {
    //匿名代码块
    {
        System.out.println("我是匿名代码块");
    }
​
    //静态代码块
    static {
        System.out.println("我是静态代码块");
    }
​
    //构造函数
    public Person() {
        System.out.println("我是构造函数");
    }
}

 

主程序代码:

public class Application {
    public static void main(String[] args) {
        //第一次创建Person类对象
        Person p1 = new Person();
        System.out.println("=======================>");
        //第二次创建Person类对象
        Person p2 = new Person();
​
        //主程序执行结果:
        /*
        我是静态代码块
        我是匿名代码块
        我是构造函数
        =======================>
        我是匿名代码块
        我是构造函数
         */
    }
}

我们可以通过以上代码发现类的一些加载顺序:

  1. 最先执行静态代码块。

  2. 然后执行匿名代码块。

  3. 最后执行构造函数。

同一个类加载的时候静态代码块只会执行一次,程序运行中加载类时如果静态代码块被执行过了,则不会再执行。

posted @   乌鸦の学习  阅读(44)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示