Two-time distribution examples

Source: Internet
Author: User

Package Com.starain.duotai;

/* Press F11 to quickly debug debug as
* Press CTRL+F11 for run as*/

/* Abstract class */
Abstract class Driver {
Public Driver () {
Constructor method, the method name and class name are exactly the same, no return type, even if the void type is not possible, can be overloaded but cannot inherit
}

public abstract void Driverssay ();//abstract method only declaration, but not implemented
}

Class Maledriver extends Driver {///Maledriver classes inherit abstract class Driver, must be overridden by all abstract methods

/* Override abstract method Driverssay in driver abstract class */
@Override
public void Driverssay () {
System.out.println ("1111");//If it is a Maledriver instance object, call Driverssay after output 1111
Drive
}
}

Class Femaledriver extends Driver {///Femaledriver classes inherit abstract class Driver, must be overridden by all abstract methods

/* Override abstract method Driverssay in driver abstract class */
@Override
public void Driverssay () {
System.out.println ("2222");//If it is a Femaledriver instance object, call Driverssay after output 2222
Drive
}

}

Abstract class Drivertool {

Private String type;

Public Drivertool () {

}

Public Drivertool (String type) {
This.settype (type);
}

public void SetType (String type) {
This.type = type;
}

Public String GetType () {
return type;
}

public abstract void Drivermethod (Driver Driver);
}

Class Car extends Drivertool {
Public Car () {

}

@Override
public void Drivermethod (Driver Driver) {
Driver.driverssay ();
}

}

Class Bus extends Drivertool {
Public Bus () {

}

@Override
public void Drivermethod (Driver Driver) {
Driver.driverssay ();
}

}

public class Duotaitest {
public static void Main (string[] args) {
Driver driver1 = new Maledriver ();
Driver driver2 = new Femaledriver ();
Drivertool tool1 = new Car ();
Drivertool tool2 = new Bus ();
Tool1.drivermethod (Driver1);
Tool2.drivermethod (DRIVER2);
Driver1.driverssay ();
Driver2.driverssay ();
}
}

Two-time distribution examples

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.