什么是多态:父类的引用指向了子类的实例
多态的实现方法;
1.使用父类作为方法的形参实现多态
2.使用父类作为方法的返回值实现多态
继承多态:当这个作为参数的父类是普通类或者抽象类时
接口多态:当这个作为参数的父类是—个接口时,构成接口多态
举例:
抽象类
public static void main(String[] args) { AbsStudent student = new Student(); Person person = new Person(); person.run(student); System.out.println(" - -- - - - -- - "); person.run(new Student()); System.out.println(" - - - -- -- ---"); AbsStudent stu2 = new AbsStudent(){ public void study(){ System.out.println("好好学习,天天向上. . . "); } }; // AbsStudent1 stu3 = new AbsStudent1(); } } abstract class AbsStudent{ public abstract void study(); } abstract class AbsStudent1{ } class Student extends AbsStudent{ @Override public void study() { System.out.println("好好学习,天天向上. . . "); } } class Person{ public void run(AbsStudent stu){ stu.study(); System.out.println("person . . . . ."); } }
接口
public static void main(String[] args) { Student student = new Student(); Person person = new Person(); person.run(student); System.out.println(" - -- - - - -- - "); person.run(new Student()); IStudent stu2 = new IStudent() { @Override public void study() { System.out.println("好好学习,天天向上. . .jieeefa"); } }; person.run(stu2); person.run(new IStudent() { @Override public void study() { System.out.println("好好学习,天天向上. . .jieeefa"); } }); } } class Student implements IStudent{ public void study(){ System.out.println("好好学习,天天向上. . . "); } } interface IStudent{ void study(); } class Person{ public void run(IStudent stu){ stu.study(); System.out.println("person . . . . ."); } }
基本数据类型
基本数据类型作为返回值,就是我们讲的值传递这块没什么区别,也不涉及到多态
引用类型
普通类
当一个方法的返回值是一个普通类,实际返回的就是该类的对象,我们可以使用该类的对象类接收
public static void main(String[] args) { Student student = new Student(); Person p1 = student.study(); Person p2 = student.eat(); System.out.println(p1); System.out.println(p2); } } class Person{ } class Student { public Person study(){ return new Person(); } public Person eat(){ Person person = new Person(); return person; } }
抽象类
public static void main(String[] args) { Student student = new Student(); AbsPerson p1 = student.study(); AbsPerson p2 = student.eat(); Person p3 = (Person) student.eat(); if (p2 instanceof Person){ Person p4 = (Person)p2; } System.out.println(p1); System.out.println(p2); } } abstract class AbsPerson{ } class Person extends AbsPerson{ } class Student { public AbsPerson study(){ return new Person(); } public AbsPerson eat(){ // Person person = new Person(); return new AbsPerson() {}; } }
接口:当一个为法的返回值是一个接口时,实际返回的是该接口的实现类对象
public static void main(String[] args) { Student student = new Student(); IPerson p1 = student.study(); IPerson p2 = student.eat(); Person p3 = (Person) student.eat(); if (p2 instanceof Person){ Person p4 = (Person)p2; } System.out.println(p1); System.out.println(p2); } } interface IPerson{ } class Person implements IPerson{ } class Student { public IPerson study(){ return new Person(); } public IPerson eat(){ // Person person = new Person(); return new IPerson() {}; } }
public static void main(String[] args) {
Student student = new Student();
IPerson p1 = student.study();
IPerson p2 = student.eat();
Person p3 = (Person) student.eat();
if (p2 instanceof Person){
Person p4 = (Person)p2;
}
System.out.println(p1);
System.out.println(p2);
p1.eat().sleep().run();
}
}
interface IPerson{
IPerson sleep();
IPerson run();
IPerson eat();
}
class Person implements IPerson{
public IPerson sleep(){
return this;
}
public IPerson run(){
return this;
}
public IPerson eat(){
return this;
}
}
class Student {
public IPerson study(){
return new Person();
}
public IPerson eat(){
// Person person = new Person();
return new IPerson(){
@Override
public IPerson sleep() {
return this;
}
@Override
public IPerson run() {
return this;
}
@Override
public IPerson eat() {
return this;
}
};
}
}