类和类的关系
继承关系,只能单继承,但是可以多层继承
类和接口的关系
实现关系,可以单实现,也可以多实现,还可以在继承一个类的同时实现多个接口
接口和接口的关系
继承关系,可以单继承,也可以多继承
接口1
public interface inter1 {
}
接口2
public interface inter2 {
}
接口3
public interface inter3 {
}
接口实现接口的关系
public interface inter3 extends inter1 {
}
类继承类以及类继承接口
public class interlmpl extends Object implements inter1 {
}
package heima;
public interface SpeakEnglish {
public abstract void speak();
}
package heima;
public abstract class Person {
private String name;
private int age;
public Person(){
}
public Person(String name,int age){
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 abstract void eat();
}
package heima;
public abstract class Coach extends Person {
public Coach(){}
public Coach(String name,int age){
super(name, age);
}
public abstract void teach();
}
package heima;
public abstract class Player extends Person{
public Player(){}
public Player(String name,int age){super(name, age);}
public abstract void study();
}
package heima;
public class BasketballCoach extends Coach{
@Override
public void teach(){
System.out.println("篮球教练教如何运球和投篮");
}
public void eat(){
System.out.println("篮球教练吃羊肉,喝羊奶");
}
public BasketballCoach() {
}
public BasketballCoach(String name, int age) {
super(name, age);
}
}
package heima;
public class BasketballPlayer extends Player{
public void study(){
System.out.println("篮球运动员学习如何运球和投篮");
}
public void eat(){
System.out.println("篮球运动员吃牛肉,喝牛奶");
}
public BasketballPlayer() {
}
public BasketballPlayer(String name, int age) {
super(name, age);
}
}
package heima;
public class PingPangCoach extends Coach implements SpeakEnglish{
public PingPangCoach() {
}
public PingPangCoach(String name, int age) {
super(name, age);
}
@Override
public void teach(){
System.out.println("乒乓球教练教如何发球和接球");
}
@Override
public void eat(){
System.out.println("乒乓球教练吃小白菜,喝大米粥");
}
public void speak(){
System.out.println("乒乓球教练说英语");
}
}
package heima;
public class PingPangPlayer extends Player implements SpeakEnglish{
public PingPangPlayer() {
}
public PingPangPlayer(String name, int age) {
super(name, age);
}
public void study(){
System.out.println("乒乓球运动员学习如何发球和接球");
}
public void eat(){
System.out.println("乒乓球运动员吃大白菜,喝小米粥");
}
public void speak(){
System.out.println("乒乓球运动员说英语");
}
}
以乒乓球运动员与篮球运动员为例
package heima;
/*
测试类
*/
public class PersonDemo {
public static void main(String[] args) {
//创建对象
PingPangPlayer ppp=new PingPangPlayer();
ppp.setName("王浩");
ppp.setAge(30);
System.out.println(ppp.getName()+","+ppp.getAge());
ppp.eat();
ppp.study();
ppp.speak();
System.out.println("---------------");
BasketballPlayer bp=new BasketballPlayer();
bp.setName("姚明");
bp.setAge(35);
System.out.println(bp.getName()+","+bp.getAge());
bp.eat();
bp.study();
}
}