多态学习

package com.tu.oop.demo6;

public class Person {
public void run(){
System.out.println("Person类执行了!!!");
}
}
/*
//一个对象的实际类型是确定的
//new Student();
//new Person();

//可以指向的引用类型就不确定了:父类的引用指向子类

//Student能调用的方法都是自己的或者继承父类的!
Student s1 = new Student();
//Person父类型,可以指向子类,但是不能调用子类独有的方法
Person s2 = new Student();
Object s3 = new Student();

//对象能执行哪些方法,主要看对象左边的类型,和右边关系不大
((Student)s2).eat();//子类重写了父类的方法,执行子类的方法
s1.run();
s1.eat();
}
*/





/**
* 多态注意事项:
* 多态是方法的多态,属性没有多态
* 父类和子类有联系,类型转换异常:ClassCastException!
* 存在条件:①继承条件 ②方法需要重写 ③父类引用指向子类对象 Father f1 = new Son();
* 不能重写的方法:
* static 方法,属于类,它不属于实例
* final 常量
* private 方法

*/
=======================================================

package com.tu.oop.demo6;

public class Student extends Person{
// @Override
// public void run() {
// System.out.println("Student执行了!!!");
// }
// public void eat(){
// System.out.println("eat执行了!!!");
// }
public void go(){
System.out.println("Student里面的go方法执行了!!!");
}
}

/**
* 编译能否通过看“=”的左边与 instanceof 比较的右边类是否存在父子关系
* 如果与instanceof右边存在父子关系则编译通过
* 运行结果看“=”的右边,也就是new后面的对象,与 instanceof 比较的右边类是否存在父子关系
* 如果与instanceof右边存在父子关系则输出结果为true,否则为false
*/
//Object > String
//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);//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);//编译报错!



*/
/*
public static void main(String[] args) {
//类型之间的转换:(基本类型转换高转低需要强转) 父类代表高类 子类代表低类

//高 低 (此处Student低类转换为Person高类则不需要强制转换)
Person obj = new Student();

//student将这个对象转换为Student类型,我们就可以使用Student类型的方法了!(所以此处为高转低需要强制转换)
//子类转换为父类,可能丢失自己的本来的一些方法!
Student student = (Student) obj;
student.go();
//((Student) obj).go();
}
}
/*
1、父类引用指向子类对象
2、把子类转换为父类,向上转型,需要强转,可能丢失自己的本来的一些方法!
3、把父类转换为子类,向下转型,自动转换不需要强转
4、方便方法的调用,减少重复的代码!简洁
*/
posted @   tuyin  阅读(39)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示