Java one, inheritance-related

Source: Internet
Author: User

. Implement the following class inheritance relationships, and write Music classes to test these classes.

 Public class Instrument {    publicvoid  play ()    {        System.out.println ("Play instrument"  );    }}

 public  class  wind extends   instrument { public  Span style= "color: #0000ff;" >void   play () {System.out.println ( "Play Wind" );  public  void   Play2 () {System.out.println ( "Calls Wind's Play2" 
 public  class  Brass "extends   instrument { public  Span style= "color: #0000ff;" >void   play () {System.out.println ( "Play brass" );  public  void   Play2 () {System.out.println ( "Calls brass Play2" 
 public  class   Music { static  void   tune (instrument i) {i.pl    Ay ();  public  static  void   main (String args[]) {wind W  = new<        /span> Wind ();        Tune (w);    Tune ( new   Brass ()); }}

Write a Java application that consists of 3 classes: class a ,classB , and main class E . Which

Class B is a subclass of class A , new member variables and member methods are added to subclass B , and the parent class is hidden

A member variable and overrides a member method of parent Class A. In main class E , in the main method, create class B

object and assign it to the object A of the parent class A , use the On Transformation object a to test some of the features of the object being transformed.

 Public class A {    int a1=5;      Public void MA ()    {        System.out.println ("a.ma");}    }

 public  class  B extends   A { int  b1=6;     int  a1=7;  public  void   MB () {System.out.println ( "B.MB" );  public  void   MA () {System.out.println ( "b.ma" ); }    }
 Public class Tseta {    publicstaticvoid  main (string[] args) {        b b=new  B ();        A=b;        System.out.println ("b.a1=" +b.a1);        System.out.println ("b.b1=" +b.b1);        B.ma ();        B.MB ();        System.out.println ("a.a1=" +a.a1);        A.ma ();            }}

Write a Java application that consists of 3 classes:theMonkey class,thepeople class, and the main class

E.

 Public class Monkey {    private  String s;      Public String GetS () {        return  s;    }      Public void sets (String s) {        this. s = s;    }       Public void speak ()    {        System.out.println ("babbling ...");}        }

 public  class  people "extends   Monkey { public  void   Speak () {System.out.println (" Sample, good! It's going to talk!    ");  public  void   think () {System.out.println () Don't talk! Think hard!    "); }    }
 Public class E {    publicstaticvoid  main (string[] args) {        Monkey m=New  Monkey ();        M.speak ();        People p=new  people ();        P.speak ();        P.think ();            }}

Java one, inheritance-related

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.