Human family class (Class inheritance and interface implementation) ===API

Source: Internet
Author: User

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

Related Article

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.