Java class, abstract class, interface of the comprehensive small exercise
Directory
Use [TOC] to generate a table of contents:
A comprehensive small practice catalog problem Description code block for Java class abstract class interface
Problem Description
Coaches and sportsmen cases (students analyze and then explain)
table tennis players and basketball players.
Table tennis coaches and basketball coaches.
in order to communicate abroad, people who are related to table tennis need to learn English.
Please use what you have learned:
analyze, what are the abstract classes, which interfaces, and what specific classes are in this case.
code block
Define an English-speaking interface interface Speakenglish {//speak English public abstract void Speak ();}
Defines the abstract class of the human 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;
//Sleeping public void sleep () {System.out.println ("People are going to sleep");
//Eat public abstract void eat ();
}//Definition Athlete abstract class extends player {public player () {} public player (String Name,int age) {
Super (Name,age);
//Learn public abstract void study ();
}//Definition Coach abstract class extends person {public coach () {} public coach (String Name,int age) {
Super (Name,age);
} Teach public abstract void teach (); }//Definition table tennis player specific class Pingpangplayer extends player implements Speakenglish {public Pingpangplayer () {} public P
Ingpangplayer (String Name,int age) {super (name,age);
//Eat public void Eat () {System.out.println ("Table tennis players eat Chinese cabbage, drink congee");
//Learning public void study () {System.out.println ("Table tennis players learn how to serve and catch");
//Speak English public void speak () {System.out.println ("table tennis player speaks English"); }//Definition basketball player specific class Basketballplayer extends player {public basketballplayer () {} public Basketballplayer (STR
ing Name,int age) {super (name,age);
//Eat public void Eat () {System.out.println ("basketball player eats beef, drink milk");
//Learning public void study () {System.out.println ("basketball player learns how to dribble and shoot"); }///Define table tennis coaches specific classes Class Pingpangcoach extends coach implements Speakenglish {public Pingpangcoach () {} public Pin Gpangcoach (String Name,int age) {Super (NAMe,age);
//Eat public void Eat () {System.out.println ("Table tennis coaches eat cabbage, drink rice gruel");
//Teach public void teach () {System.out.println ("Table tennis Coach teaches how to serve and catch");
//Speak English public void speak () {System.out.println ("Table tennis instructor speaks English"); }//Definition basketball coach specific class Basketballcoach extends Coach {public Basketballcoach () {} public Basketballcoach (String n
Ame,int age) {super (name,age);
//Eat public void Eat () {System.out.println ("basketball coach eats mutton, drink goat's milk");
//Teach public void teach () {System.out.println ("basketball coach teaches how to dribble and shoot");
} class InterfaceTest3 {public static void main (string[] args) {//Test athlete (table tennis player and basketball player)//table tennis player
Pingpangplayer PPP = new Pingpangplayer ();
Ppp.setname ("Wang Hao");
Ppp.setage (33);
System.out.println (Ppp.getname () + "---" +ppp.getage ());
Ppp.eat ();
Ppp.sleep ();
Ppp.study ();
Ppp.speak (); SYSTEM.OUT.PRINTLN ("----------------");
Constructs the data by means of a parameter (left to you)//basketball player Basketballplayer bp = new Basketballplayer ();
Bp.setname ("Yao Ming");
Bp.setage (34);
System.out.println (Bp.getname () + "---" +bp.getage ());
Bp.eat ();
Bp.sleep ();
Bp.study (); Bp.speak (); No this method//test coach do it yourself}}