Create derived class and object

Contents

Output

My name is FirstName LastName
My sex is male
My specialty is cardiology


Explanation

About Base class (Human class), please refer to Create class and object.


Code examples


Java

/*
Human.class must be compiled earlier and must be put in the path of environmental value CLASSPATH=... or in same directory
*/
import java.util.*;
class Doctor extends Human{
    private String specialty;

    public Doctor(Map<String, String> opt){
        super(opt);
        this.specialty = opt.get("specialty");
    }

    public void saySpecialty() {
        System.out.println("My specialty is " + this.specialty);
    }

    public static void main(String args[]) {
        Map<String, String> opt = new HashMap<String, String>();
        opt.put("name", "FirstName LastName");
        opt.put("sex", "male");
        opt.put("specialty", "cardiology");
        Doctor doctor1 = new Doctor(opt);
        doctor1.sayName();
        doctor1.saySex();
        doctor1.saySpecialty();
    }
}

JavaScript

const Human = require('../human_class/Human.js');
class Doctor extends Human {
	constructor(opt) {
		super(opt);
        this.specialty = opt["specialty"];
    }

	saySpecialty() {
        console.log("My specialty is " + this.specialty);
    }
}
module.exports = Doctor;

if(!module.parent) {
    let doctor1 = new Doctor({"name":"FirstName LastName", "sex":"male", "specialty":"cardiology"});
    doctor1.sayName();
    doctor1.saySex();
    doctor1.saySpecialty();
}

Perl

package Doctor;
use strict;
use Data::Dumper;
use warnings;
use File::Basename;
use lib dirname(__FILE__).'/../human_class';
use base qw(Human);

sub new(){
  my $class = shift;
  my $op = shift;
  my $self = Human->new( $op );
  $self = bless $self, $class;
  $self->{'specialty'} = $op->{'specialty'};
  return $self;
}

sub saySpecialty(){
  my $self = shift;
  if($self->{'specialty'}){
    print "My specialty is ".$self->{'specialty'}."\n";
  }
}

if ($0 eq __FILE__) {
  my $doctor1 = new Doctor({'name' => "FirstName LastName", "sex" => "male", "specialty" => "cardiology"});
  $doctor1->sayName();
  $doctor1->saySex();
  $doctor1->saySpecialty();
}
else{
  1;
}

PHP

<?php
namespace FirstClass\Example2;
require __DIR__. '/../human_class/Human.php';
use FirstClass\Example1\Human;

class Doctor extends Human{
  private $specialty;

  public function __construct($opt){
    parent::__construct($opt);
    $this->specialty = $opt["specialty"];
  }

  public function saySpecialty(){
    print "My specialty is ".$this->specialty."\n";
  }
}

if (!isset(debug_backtrace()[0])) {
  $doctor1 = new Doctor(["name"=>"FirstName LastName", "sex"=>"male", "specialty"=>"cardiology"]);
  $doctor1->sayName();
  $doctor1->saySex();
  $doctor1->saySpecialty();
}


Python

import sys
sys.path.append('../human_class')
from Human import Human

class Doctor(Human):
	def __init__(self, opt):
		super().__init__(opt)
		self.specialty = opt["specialty"]

	def saySpecialty(self):
		print("My specialty is " + self.specialty)

if __name__ == "__main__":
	doctor1 = Doctor({"name":"FirstName LastName", "sex":"male", "specialty":"cardiology"})
	doctor1.sayName()
	doctor1.saySex()
	doctor1.saySpecialty()

Ruby

require_relative "../human_class/Human.rb"

class Doctor < Human
    def initialize(opt)
        super
        @specialty = opt["specialty"]
    end

    def saySpecialty
        puts "My specialty is " + @specialty.to_s
    end
end

if $0 == __FILE__
    doctor1 = Doctor.new({"name" => "FirstName LastName", "sex" => "male", "specialty" => "cardiology"})
    doctor1.sayName
    doctor1.saySex
    doctor1.saySpecialty
end