interface, class, abstract class synthesis examples

Source: Internet
Author: User

Requirements already in:

Here is the analysis and code:

/*
Coach and athlete case (student analysis then explain)
Table tennis players and basketball players.
Table tennis coaches and basketball coaches.
In order to go abroad to exchange, with table tennis related personnel need to learn English.
Please use the knowledge you have learned:
Analysis, in this case there are abstract classes, which interfaces, which specific classes.

Throughout the analysis process, I have been explaining it through drawing.
*/
Define an English-speaking interface
Interface Speakenglish {
Speak english
public abstract void speak ();
}

Abstract class for defining people
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.) {
This.age = age;
}

Go to bed
public void sleep () {
System.out.println ("People are going to sleep");
}

Eat
public abstract void Eat ();
}

Define the athlete abstract class
Abstract class Player extends person {
Public Player () {}

Public Player (String Name,int age) {
Super (Name,age);
}

Learn
public abstract Void Study ();
}

Define instructor Abstract class
Abstract class Coach extends person {
Public Coach () {}

Public Coach (String Name,int age) {
Super (Name,age);
}

Teach
public abstract void teach ();
}

Define the specific class of table tennis players
Class Pingpangplayer extends Player implements Speakenglish {
Public Pingpangplayer () {}

Public Pingpangplayer (String Name,int age) {
Super (Name,age);
}

Eat
public void Eat () {
SYSTEM.OUT.PRINTLN ("Table tennis players eat Chinese cabbage, drink millet gruel");
}

Learn
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");
}
}

Define the specific class of basketball players
Class Basketballplayer extends Player {
Public Basketballplayer () {}

Public Basketballplayer (String Name,int age) {
Super (Name,age);
}

Eat
public void Eat () {
System.out.println ("Basketball players eat beef, drink milk");
}

Learn
public void Study () {
System.out.println ("basketball players learn how to dribble and shoot");
}
}

Define table tennis coach specific class
Class Pingpangcoach extends Coach implements Speakenglish {
Public Pingpangcoach () {}

Public Pingpangcoach (String Name,int age) {
Super (Name,age);
}

Eat
public void Eat () {
SYSTEM.OUT.PRINTLN ("Table tennis instructor eat cabbage, drink rice gruel");
}

Teach
public void Teach () {
SYSTEM.OUT.PRINTLN ("Table tennis Instructor teaches how to serve and catch");
}

Speak english
public void Speak () {
SYSTEM.OUT.PRINTLN ("Table tennis instructor speaks English");
}
}

Define basketball coach Specific class
Class Basketballcoach extends Coach {
Public Basketballcoach () {}

Public Basketballcoach (String Name,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 Interfacedemo {
public static void Main (string[] args) {
Test athletes (table tennis players and basketball players)
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 ("----------------");
Data (left to you) through the parameter structure

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 (); There is no such method

Test coach Do it Yourself
}
}

Results:

interface, class, abstract class synthesis examples

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.