1. Java中的多态
Java有三大特性:封装 继承 多态
多态: 多种形态。是面向对象的三大特性之一。多态是建立在封装和继承之上
1.1 方法的多态【非重点】
方法的重写和重载就是方法的多态的体现形式
class Person {
public void eat () {
System.out.println("吃饭");
}
public void eat (String food) {
System.out.println("吃" + food);
}
}
class Man extends Person {
@Override
public void eat() {
System.out.println("吃枸杞");
}
@Override
public void eat(String food) {
System.out.println("吃"+ food);
}
}
public class Demo1 {
public static void main(String[] args) {
}
}
1.2 对象的多态【重点】
1.一个对象 的编译类型和运行类型是不一样
2.编译类型是在定义对象的时候,就已经确定好的
3.运行类型是可以改变的
4.编译类型看=的左边 运行类型看 =的右边
Person person = new Person();
多态: 父类引用指向子类的对象
Animal animal = new Dog();
Animal anima1 = new Cat();
class Animal {
public void cry () {
System.out.println("动物在叫......");
}
}
class Dog extends Animal {
@Override
public void cry() {
System.out.println("汪汪汪......");
}
//自己独有的方法
public void eat () {
System.out.println("狗吃shit");
}
}
class Cat extends Animal {
@Override
public void cry() {
System.out.println("喵喵喵.....");
}
public void sleep () {
System.out.println("睡觉打呼噜");
}
}
public class Demo1 {
public static void main(String[] args) {
//多态 父类的引用指向子类的对象
Animal animal = new Dog();
animal.cry();
//父类的引用是指向不了子类独有的方法的,就意味着
//父类的引用不能调用子类独有的方法
//animal.eat();
Animal animal2 = new Cat();
animal2.cry();
//animal2.sleep();
}
}
对象的多态注意事项:
多态:
1.必须有继承
2.必须有重写
3.=左边是父类引用 =右边是子类的对象
1.3父类的引用指向子类的对象在开发中如何使用
之前有一个作业 : 人喂狗,狗吃饭。 人喂猫,猫吃饭
class Person {
// public void feed (Dog dog) {
// dog.eat();
// }
// public void feed (Cat cat) {
// cat.eat();
// }
//在开发中,一个方法的参数是父类的引用。但是真正传的值是子类的对象
public void feed (Animal animal) {
animal.eat();
}
}
interface Animal {
public void eat ();
}
class Dog implements Animal{
@Override
public void eat () {
System.out.println("狗在吃骨头......");
}
}
class Cat implements Animal{
@Override
public void eat () {
System.out.println("猫吃鱼......");
}
}
public class Demo1 {
public static void main(String[] args) {
Person person = new Person();
/**
* Animal animal = new Cat();
* public void feed (Animal animal) {
animal.eat();
}
*/
person.feed(new Cat());
/**
* Animal animal = new Dog();
* public void feed (Animal animal) {
animal.eat();
}
*/
person.feed(new Dog());
}
}
1.4多态的转型【重点】
1.4.1多态的向上转型
本质就是:父类的引用指向子类对象
语法格式:
父类 父类引用 = new 子类();
将子类的对象赋值给了父类的引用。小范围(子类) 转为大范围(父类)自动转
父类的引用可以调用父类的所有成员方法,可以调用子类的重写父类的方法,但是不能调用子类独有的方法。
class Person {
public void eat () {
}
}
class Student extends Person {
@Override
public void eat() {
System.out.println("学生吃饭,吃披萨");
}
public void sleep () {
System.out.println("中午不谁下午崩溃!!!");
}
}
public class Demo1 {
public static void main(String[] args) {
Person person = new Student();//向上转型
person.eat();
//person.sleep();
}
}
1.4.2多态向下转型
语法格式:
父类类型 父类引用 = new 子类(); 子类类型 子类引用 = (子类类型)父类的引用;
多态向下转型 需要强转 即:先向上转型,再向下转型
class TestA {
//Object 是所有类的父类
public void test (Object obj ) {
//必须给我打印一个String类型的狗蛋,我不要Object类型的狗蛋
// String str = (String) obj;
// System.out.println(str);
int i1 = (int)obj;
System.out.println(i1);
}
}
public class Demo4 {
public static void main(String[] args) {
TestA testA = new TestA();
//testA.test("狗蛋");
Object object = "狗蛋";
testA.test(98);
testA.test('狗');
}
}
1.5 instanceof 关键字 (不是重点)
比较操作符,返回值是布尔类型的数据
语法格式:
对象引用 instanceof 运行类型
用来判断对象的运行类型(=右边), 是否是xx类型或者xx类型的子类
目的:是为了在强转的时候不出现问题
class AA {}
class BB extends AA {}
public class Demo1 {
public static void main(String[] args) {
BB bb = new BB();
BB bb1 = new BB();
AA aa = new AA();
//instanceof 的左边放的对象的引用,右边是类
System.out.println(bb instanceof BB);//true
System.out.println(bb1 instanceof BB);//true
//判断对象bb是否是AA的对象或者子类的对象
System.out.println(bb instanceof AA);//true
System.out.println(aa instanceof AA);//true
System.out.println(aa instanceof BB);//false
//总结: 左边 的辈分小(对象的引用) 右边辈分大(类) 屡试不爽
AA aa2 = new BB();
System.out.println(aa2 instanceof AA);//true
System.out.println(aa2 instanceof BB);//true
//
Object object = new Object();
System.out.println(object instanceof AA);//false
System.out.println(object instanceof Object);//true
String string = "qwer";
System.out.println(string instanceof Object);//true
}
}