今天讲了Java接口和java的多态;
什么是接口,接口的特点是什么,它与抽象类又有什么联系。都可以通过一个案例,篮球运动员的案例去解释。
BasketballPlayer basketballPlayer =new BasketballPlayer();
basketballPlayer.name="姚明";
basketballPlayer.age=40;
basketballPlayer.speak();
Person person=new Person();
person.study();
person.teach();
}
}
class Person{
String name;
int age;
String gender;
public Person() {
}
public Person(String name, int age, String gender) {
super();
this.name = name;
this.age = age;
this.gender = gender;
}
public void eat() {
System.out.println("吃饭");
}
public void sleep() {
System.out.println("睡觉");
}
}
interface SpeakEnglish{
}
abstract class Coach extends Person{
}
class BasketballCoach extends Coach implements SpeakEnglish{
public void teach() {
System.out.println("教扣篮");
}
public void speak() {
System.out.println("说英语");
}
}
abstract class Player extends Person{
}
class BasketballPlayer extends Player implements SpeakEnglish{
public void study() {
System.out.println("学习扣篮");
}
public void speak() {
System.out.println("说英语");
}
}
还有就是多态,多态的变现形式,特点,还有就是应用场景,优缺点。可以直接嗯用花木兰,手机类例子,来总结,并且里面有关向上转型和向下转型。
|
|