Contents [hide]
输出结果
My name is FirstName LastName My sex is male My specialty is cardiology
说明
About Base class (Human class), please refer to Create class and object.
程式码
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | /* 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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 |
作者: 栗田 創。1st Class, inc的首席执行官/首席技术官。搜索引擎工程师、网站管理员
证书: AWS Solution Architect,Professional Scrum Master I ,一级IT工程师(日本国家证书),TOEIC900+,东京大学経済学部経営学科卒业
请关注我的Weblio帐户以获取更新
IT相关服务:VPS排名