End the interface section in the form of a case.
/* 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 are common functions, so, we extracted a parent class: Animals: Name, age to eat ();//eat differently, defined as abstract method sleep () {} Cat: Inherit from Animal dog: The Extra function inherited from animal high jump is a new extension function, so we want to define an interface interface: High jump part cat: Realization of high jump part of the dog: the realization of high jump; from abstraction to concrete, this specific meaning is realized after the concrete content use: Use concrete class */// Define high Jump Interface interface Jumpping {//high jump function public abstract void jumps ();} Definition 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.) {this.age = age;} To eat (); defined as abstract method public abstract void Eat ();//Sleep () {}public void sleep () {System.out.println ("sleep Up");}} Specific cat class Cat extends Animal {//cats that do not have high jump function cat public cat () {}public cat (String Name,int age) {super (name,age);} public void Eat () {System.out.println ("Cat eats Fish");}} Specific dog class, with Cat class Dog extends Animal {public Dog () {}public Dog (String name,int age) {super (name,age);} public void Eat () {System.out.println ("Dog eats Meat");}} Have a high jumpfunction of the cat, in inheriting a class while implementing the interface class Jumpcat extends cat implements jumpping {public jumpcat () {}public jumpcat (String Name,int age) {s Uper (name,age);} public void jumps () {System.out.println ("High Jump Cat");}} A dog with a high jump function implements the interface class Jumpdog extends dog implements jumpping {public Jumpdog () {}public Jumpdog (String name,int a) while inheriting a class GE) {super (name,age);} public void jumps () {//The abstraction in the docking port is overridden to implement SYSTEM.OUT.PRINTLN ("High Jump Dog");}} Class Interfacetest {public static void main (string[] args) {//define the high jump cat and test jumpping jum = new Jumpcat ();//polymorphic way to achieve jumpcat JC = New Jumpcat ();//own Class object form Jc.setname ("Doraemon"); Jc.setage (3); System.out.println (Jc.getname () + "---" +jc.getage ()); Jc.eat (); Jc.sleep (); Jc.jump (); Jum.jump ();//Polymorphic Way, Realized the special high jump function System.out.println ("-----------------"); Jumpcat jc2 = new Jumpcat ("Garfield", 2); System.out.println (Jc2.getname () + "---" +jc2.getage ()); Jc2.eat (); Jc2.sleep (); Jc2.jump ();//define High jump dog jumpping jum1 = new Jumpdog (); Jumpdog JD = new Jumpdog (); Jd.setage (4); Jd.setname ("Yechen"); System.out.println (Jd.getname () + "-----" +jd.getage ()); Jd.eat (); JD.SLEep (); Jd.jump (); Jum1.jump (); System.out.println ("-----------------"); Jumpdog jd1 = new Jumpdog ("Chuga", 23);//create objects with yourself. System.out.println (Jd1.getage () + "------" +jd1.getname ()); Jd1.eat (); Jd1.sleep (); Jd1.jump ();}}
So far, the object-oriented segment is almost over. The next article (http://blog.csdn.net/qq_32059827/article/details/51338014) will summarize the object-oriented content in the form of a case in a small case.
Object-oriented (interface finishing) for the first season of Java entry