好的,面向对象的三大特征终于要写完了~
面向对象的三大基本特征:封装(encapsulation)、继承(inheritance)、多态(polymorphism)
1、多态的概念
多态就是多种形态、状态。
实现条件:继承、重写。
注意事项:
(1)静态方法
静态方法不能被重写,所以静态方法也没有多态性。
(2)成员变量
成员变量的值取决于引用所属的类,不具备多态性。
(3)成员方法
编译时:检查引用所属的类中是否有所调用的方法。
运行时:调用实际对象所属类中的重写方法。
2、多态的转型
(1)向上转型upcasting
子类转父类(系统自动完成)
(2)向下转型downcasting
父类转子类,使用强制类型转换符()
public class Test01 {
public static void main(String[] args) {
Person person1 = new Person();
person1.eat();
person1.sleep();
System.out.println("----------------");
Student student1 = new Student();
student1.eat();
student1.sleep();
System.out.println("----------------");
// 向上转型upcasting(子类转父类)
Person person2 = new Student("zxx",20);
person2.eat();
person2.sleep();
// 向下转型downcasting(父类转子类)
// 强制类型转换符()
Student student2=(Student) person2;
student2.study();
System.out.println("----------------");
Person person3 = new Boy();
person3.eat();
person3.sleep();
}
}
public class Person {
private String name;
private int age;
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void eat() {
System.out.println("人在吃饭");
}
public void sleep() {
System.out.println("人在睡觉");
}
}
public class Student extends Person {
private String number;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String name, int age) {
super(name, age);
// TODO Auto-generated constructor stub
}
public Student(String number) {
super();
this.number = number;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public void study() {
System.out.println("学生在学习");
}
@Override
public void eat() {
System.out.println("学生在安静的吃饭");
}
@Override
public void sleep() {
System.out.println("学生在安静的睡觉");
}
}
public class Boy extends Person {
@Override
public void eat() {
System.out.println("男生在狼吞虎咽");
}
@Override
public void sleep() {
System.out.println("男生在呼呼大睡");
}
}
类型转换的重点:
1、弄清楚继承关系,谁是父类谁是子类
2、弄清楚创建对象调用的是谁的构造函数
3、多态与instanceof
instanceof:判断对象是否是指定类所创建的实例(返回值是boolean类型)
// instance 实例 对象
public class Test {
public static void main(String[] args) {
Animal animal1 = new Animal();
animal1.howl();
boolean result =animal1 instanceof Animal;
System.out.println(result);
System.out.println("----------------");
Dog dog1 = new Dog();
dog1.howl();
dog1.eatBone();
System.out.println("----------------");
// 父类引用指向子类对象
Animal animal2 = new Dog();
animal2.howl();
// instanceof判断对象是否是指定类所创建的实例(返回值是boolean类型)
boolean result1 =animal2 instanceof Animal;
System.out.println(result1);
boolean result2 =animal2 instanceof Dog;
System.out.println(result2);
System.out.println("----------------");
if(animal2 instanceof Dog) {
((Dog) animal2).eatBone();
}
Dog dog2=(Dog) animal2;
dog2.howl();
dog2.eatBone();
// Exception in thread "main" java.lang.ClassCastException:
//com.etime8.Animal cannot be cast to com.etime8.Dog
// 这是数据类型转换的错误
//Animal animal3 = new Animal();
//Dog dog3=(Dog) animal3;
//dog3.howl();
//dog3.eatBone();
System.out.println("----------------");
// Animal是Dog的父类,Dog满足Animal类的条件
Animal animal = new Cat();
test1(animal1);
// 匿名对象
test1(new Dog());
MyInterfaceImpl1 impl1=new MyInterfaceImpl1();
MyInterfaceImpl2 impl2 = new MyInterfaceImpl2();
test2(impl1);
test2(impl2);
}
public static void test1(Animal animal) {
}
public static void test2(MyInterface myInterface) {
}
public static Animal test3() {
Animal animal = new Animal();
// Animal是父类,Dog是子类
// 所以返回值是Animal类时Dog类同样符合条件
Dog dog = new Dog();
return dog;
}
public static Fruit test4() {
return new Mango();
}
}
public class Animal {
public void howl() {
System.out.println("动物在嚎叫");
}
}
public class Dog extends Animal {
@Override
public void howl() {
System.out.println("小狗在嚎叫");
}
public void eatBone() {
System.out.println("小狗爱骨头");
}
}
public class Cat extends Animal {
@Override
public void howl() {
System.out.println("小猫在喵喵喵");
}
public void eatFish() {
System.out.println("小猫爱吃鱼");
}
}
4、内部类
在类中定义的内部类叫成员内部类,在函数中定义的内部类叫局部内部类。
4.1、内部类访问方法
(1)访问外部类的字段:外部类类名.this.字段
(2)访问外部类的方法:外部类类名.this.方法
(3)访问内部类的字段:this.字段
(3)访问内部类的方法:this.方法
4.2、内部类访问特点
(1)内部类可以直接访问外部类的成员,包括外部类的私有对象;
(2)外部类要访问内部类的成员,必须创建对象。
好吧,多态就先写这些,懂的都懂。