多态性:可以理解为一个事物的多种表型形态
1、方法的重载和方法的重写
2、对象的多态性---可以直接应用在抽象类和接口上
3、子类对象的多态性
Person p2=new Man();
子类对象的多态性使用的前提:1)要有类的继承 2)要有子类对父类进行重写
4、程序运行分为编译状态和运行状态
对于多态性来说,编译时,“看左边”,看此引用变量理解为父类的类型
运行时,“看右边”,关注于真正对象的实体,
Java引用变量有两个类型:编译时类型和运行时类型
编译时类型由 声明该变量时使用的类型决定,运行时类型由实际赋给该变量的对象决定。
5、属性无多态性
Person p2=new Man();
//虚拟方法调用,通过父类的引用指向子类的对象实体,当调用时,实际执行的是子类的=重写父类的方法。
p2.eat();
p2.walk();
//p2.entertainment();
//编译时会报错,这个方法person中无定义
//在编译时,p2被认为是
System.out.println("这是谁的属性"+p2.id);//这是谁的属性142
//这是父类Person的id,不是Man的,属性无多态性
代码:
public class Person {
private String name = "oooXXX";
private String sex;
private int age;
int id = 142;//身份证
public Person() {
//super();//有没有都行,会默认自动调用
System.out.println("这是person的无参构造器!!");
}
public Person(String sex){
this();
this.sex=sex;
System.out.println("这是person的有参构造器");
}
public void setName(String name) {
this.name = name;
}
public void setSex(String sex) {
this.sex = sex;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public String getSex() {
return sex;
}
public int getAge() {
return age;
}
public void eat() {
System.out.println("吃饭喽!!!");
}
public void walk() {
System.out.println("走起来!!!");
}
public void sleep() {
System.out.println("人在睡觉!!!");
}
}
public class Man extends Person{
private boolean smoke;
int id=1001;
public Man(){
super();
System.out.println("这是Man的无参构造方法");
}
public Man(boolean smoke){
super();
this.smoke=smoke;
System.out.println("这是Man的有参构造方法");
}
public void setSmoke(boolean smoke){
this.smoke=smoke;
}
public boolean getSmoke(){
return smoke;
}
//方法重写
public void eat(){
System.out.println("男人在吃饭!!!");
}
public void walk() {
System.out.println("男人在走路!!!");
}
public void entertainment(){
System.out.println("男人请客吃饭");
}
}
public class Woman extends Person {
private boolean beauty;
public Woman() {
super();
System.out.println("这是Woman的无参构造方法");
}
public Woman(boolean beauty) {
super();
this.beauty = beauty;
System.out.println("这是Woman的有参构造方法");
}
public void setBeauty(boolean smoke) {
this.beauty = smoke;
}
public boolean getSmoke() {
return beauty;
}
//方法重写
public void eat() {
System.out.println("女人吃饭!!!");
}
public void walk() {
System.out.println("女人走路!!!");
}
public void shopping(){
System.out.println("女人爱购物");
}
}
public class DuotaiTeat {
public static void main(String[] args){
Person p1=new Person();
p1.eat();
p1.walk();
Man m1=new Man();
m1.eat();
m1.walk();
//子类对象的多态性,父类的引用指向子类对象
Person p2=new Man();
//虚拟方法调用,通过父类的引用指向子类的对象实体,当调用时,实际执行的是子类的=重写父类的方法。
p2.eat();
p2.walk();
//p2.entertainment();
//编译时会报错,这个方法person中无定义
//在编译时,p2被认为是
System.out.println("这是谁的属性"+p2.id);//这是谁的属性142
//这是父类Person的id,不是Man的,属性无多态性
Person m2=new Woman();
// p2.shopping();
Woman w=(Woman) m2;//向下转型,使用强转换符
w.shopping();
//Woman m11=(Woman) m1;
//错误,不能将男人转换为女人
System.out.println("-------");
if(m2 instanceof Woman){
Woman ww=(Woman) m2;//向下转型,使用强转换符
ww.shopping();
}
//对象1 instanceof 类1:判断对象1是否是类1的一个实例,是的话就返回true;否则返回false
//若a是A类的实例,那么a也一定是A类的父类的实例。
if(m2 instanceof Man){
Man ww=(Man) m2;//向下转型,使用强转换符
ww.entertainment();
}
if(m2 instanceof Person){
System.out.println("若a是A类的实例,那么a也一定是A类的父类的实例。");
}
}
}
结果:
这是person的无参构造器!!
吃饭喽!!!
走起来!!!
这是person的无参构造器!!
这是Man的无参构造方法
男人在吃饭!!!
男人在走路!!!
这是person的无参构造器!!
这是Man的无参构造方法
男人在吃饭!!!
男人在走路!!!
这是谁的属性142
这是person的无参构造器!!
这是Woman的无参构造方法
女人爱购物
-------
女人爱购物
若a是A类的实例,那么a也一定是A类的父类的实例。

本文深入探讨了Java中的多态性概念,包括方法重载、重写和对象的多态性,展示了如何通过子类对象实现多态性,并解释了属性在多态中的表现。通过具体的代码示例,说明了编译时类型与运行时类型的差异,以及虚拟方法调用的机制。
351

被折叠的 条评论
为什么被折叠?



