Ancestor class Animal.java
public class animal{
public void Eat () {
System.out.println (" animals need to eat ");
}
public void drink () {
System.out.println (" animals need water ");
}
public void Needsleep () {
System.out.println (" animals need to sleep ");
}
}
The second ancestor class inherits the ancestor class human. java
public class Human extends animal{
public void Eat () {
SYSTEM.OUT.PRINTLN (" human needs to eat ");
}
public void drink () {
SYSTEM.OUT.PRINTLN (" human needs water ");
}
public void Speak () {
SYSTEM.OUT.PRINTLN (" human need to speak ");
}
public void Needsleep () {
SYSTEM.OUT.PRINTLN (" human needs to sleep ");
}
}
Two-interface class (plus function)
Public interface Driving {public void Driving (); }
public interface kaoshi{public void Kaoshi (); }
Male humans inherit human beings and implement another two interface function, their own added function method (polymorphic) Man.java
public class Mans extends Human implements driving,kaoshi{
public void driving () {
System.out.println (" men must learn to drive ");
}
public void Kaoshi () {
System.out.println (" men need exams ");
}
public void Joinarmy () {
System.out.println (" men to join the Army ");
}
}
Women inherit human beings and their own increased functional methods (polymorphism) Woman. java
public class Woman extends Human {
public void Selectbeauty () {
System.out.println (" Women want a beauty pageant ");
}
}
Testhuman.java
Import java.util.ArrayList;
Import Java.util.Iterator;
public class testhuman{
public static void Main (string[] args) {
Man M =new Man ();
M.eat ();
M.driving ();
M.joinarmy ();
M.kaoshi ();
Woman w=new Woman ();
W.needsleep ();
W.selectbeauty ();
literal meaning: An instance of instanceof== (m instanceof Animal) refers to an instance of Animal as M
instanceof: Whether the left class is a subclass of the right class (whether it belongs to the right class family)
if (M instanceof Animal)
System.out.println (" men belong to Animals ");
Else
System.out.println (" men do not belong to animals ");
if (w instanceof Animal)
System.out.println (" women belong to Animals ");
Else
System.out.println (" women do not belong to animals ");
class belongs to the class family
ArrayList alt=new ArrayList ();
Alt.add (" Zhang San ");
Alt.add (" John Doe ");
Alt.add (" Bachelor ");
Iterator it=alt.iterator ();// returns an iterator
while (It.hasnext ()) {
System.out.println (It.next ());
}
}
}
Human family class (Class inheritance and interface implementation) ===API