4.57 Interface Exercises

Source: Internet
Author: User

1:

/* Cat dog case, add extra function Analysis of high jump: from concrete to abstract Cat: name, age to eat, sleep Dog: name, age to eat, sleep because there is a common function, so, extract a parent class: Animal: Name, age to eat (); sleep () {} Cat: Inherit from Animal dog: The Extra function inherited from animal high jump is a new extension function, so to define an interface interface: High jump part of the cat: the realization of high jump part of the dog: the realization of high jump; From abstraction to concrete use: Define high jump interfaces using specific class *///interface  jumpping {//  High jump function Public abstract void jump ();}   Define abstract class abstract class animal {//  name private string name;//  age Private  int age;public animal ()  {}public animal (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;}   Eat ();p ublic abstract void eat ();//  sleeping () {}public void sleep ()  { System.out.println ("Sleeping");}}   Specific Cat class Class cat extends animal {public&nbsP Cat ()  {}public cat (string name, int age)  {super (name, age);} Public void eat ()  {system.out.println ("Cat eats Fish");}}   Specific Dog class Class dog extends animal {public dog ()  {}public dog (String  name, int age)  {super (name, age);} Public void eat ()  {system.out.println ("Dog eats Meat");}}   Cat class jumpcat extends cat implements jumpping {public  with high jump function Jumpcat ()  {}public jumpcat (string name, int age)  {super (name, age);} Public void jump ()  {system.out.println ("High Jump Cat");}}   Dog class jumpdog extends dog implements jumpping {public  with high jump function Jumpdog ()  {}public jumpdog (string name, int age)  {super (name, age);} Public void jump ()  {system.out.println ("High Jump Dog");}} Class interfacetest {public static void main (StriNg[] args)  {//  define high jump cat and Test jumpcat jc = new jumpcat (); Jc.setname ("Doraemon"); Jc.setage (3); System.out.println (Jc.getname ()  +  "---"  + jc.getage ()); Jc.eat (); Jc.sleep (); Jc.jump (); System.out.println ("-----------------"); Jumpcat jc2 = new jumpcat ("Garfield",  2); System.out.println (Jc2.getname ()  +  "---"  + jc2.getage ()); Jc2.eat (); Jc2.sleep (); Jc2.jump () ;//  defines the high jump dog and carries out the test by doing it himself. }}

2:

/* Teacher and student case, add extra function Analysis of smoking: from concrete to abstract teacher: name, age, eat, sleep students: name, age, eat, sleep due to the common function, extract a parent class, human. Human: Name, age eat (); sleeping () {} The extra function of smoking is not a person or teacher, or a student should have it at first, so define it as an interface smoking interface. Part of the teacher smoking: Realizing the Smoking interface part students smoking: Realizing the Smoking interface realization: From abstraction to concrete use: specific *///defines the smoking interface interface smoking {//  the abstract method of smoking public  Abstract void smoke ();}   Definition abstract Human abstract class person {//  name private string name;//  age 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;}   Eat ();p ublic abstract void eat ();//  sleeping () {}public void sleep ()  { System.out.println ("Sleep-Awake");}}   Concrete Teacher Class class teacher extends Person {public teacher ()  {}public teacher (string name, int age)  {super (name, age);} Public void eat ()  {system.out.println ("eating cabbage");}}   Specific Student class class student extends person {public student ()  {}public  Student (string name, int age)  {super (name, age);} Public void eat ()  {system.out.println ("Eat Pork");}}   Smoking teacher Class smokingteacher extends teacher implements smoking {public  smokingteacher ()  {}public smokingteacher (string name, int age)  {super (name ,  age);} Public void smoke ()  {system.out.println ("Smoking Teacher");}   Smoking Students Class smokingstudent extends student implements smoking {public  smokingstudent ()  {}public smokingstudent (string name, int age)  {super (name ,  age);} Public void smoke ()  {SYSTEM.OUt.println ("smoking Student");}} Class interfacetest2 {public static void main (String[] args)  {//  Test student smokingstudent ss = new smokingstudent (); Ss.setname ("abc"); Ss.setage (27); System.out.println (Ss.getname ()  +  "---"  + ss.getage ()); Ss.eat (); Ss.sleep (); Ss.smoke (); System.out.println ("-------------------"); Smokingstudent ss2 = new smokingstudent ("EFG",  30); System.out.println (Ss2.getname ()  +  "---"  + ss2.getage ()); Ss2.eat (); Ss2.sleep (); Ss2.smoke () ;//  Test Teacher}}


4.57 Interface Exercises

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.