15-4 類別?繼??

OiH~YAҦpuHvO@OAӡuǥ͡vOt@OAѩuǥ͡vOuHvSҡA]ڭ̥iNuHvݦOO]Base Class^Aӡuǥ͡vhOlO]Derived Class^A]uǥ͡vOiH~ӡuHvOʽMkC~ӡAiHڭ̨ϥ²檺覡Ӵyzu@ɪUYC

Y˵ڭ̪dҵ{ؿAiHݨӥؿG@person M @studentA䤤 @person OyzuHvOA @student hOyzuǥ͡vOCCӤHOѩmWBʧOBB魫өwq]غc禡i @person ؿU person.m^AھڳoǰѼơAڭ̴NiHغcuHvAdҦpUG

Example 1: 15-ɦV{]p/person01.mp1 = person('إ', 'k', 1.93, 95) p2 = person('', 'k', 2.26, 134) fprintf('إ BMI = %g\n', bmi(p1)); fprintf(' BMI = %g\n', bmi(p2));p1: mW = إ, ʧO = k, = 1.93 m, 魫 = 95 kg p2: mW = , ʧO = k, = 2.26 m, 魫 = 134 kg إ BMI = 25.504 BMI = 26.2354

bWzdҤAbmi() O@ person OkAiHp@ person BMI (Body Mass Index) ȡAwqq]H笰^鶴]Hج^誺ȡC~b @personؿUAڭ̤]wqFܪ󪺨禡 display.mAŪ̥iHۦ˵䤺eC

ۧڭ̦@ student OAoO person OlOAҥH~ӤFҦ person OʽMkA~ӪYDnOWdb student غc禡ApUG

Example 2: 15-ɦV{]p/@student/student.mfunction s = student(name, gender, height, weight, department, year) %STUDENT Student class constructor p = person(name, gender, height, weight); % Person class s.department = department; % student Sʽ s.year = year; % student Sʽ s = class(s, 'student', p); % wq s student AB~Ӧ p O

bWzغc禡AiHD student O@]tөʽ]name, gender, height, weight, department, year^A䤤e|өʽ]name, gender, height, weight^O~Ӧ person OAӫөʽ]department, year^hO student OSʽC~Aѩ student ~ personA] student ]iHϥΩҦ person kAdҦpUG

Example 3: 15-ɦV{]p/student01.ms1 = student('ix', 'k', 1.84, 68, '|pt', 3) fprintf('ixӪ BMI = %g\n', bmi(s1));s1: mW = ix, ʧO = k, = 1.84 m, 魫 = 68 kg ixӪ BMI = 20.0851

pGA˵ @student ؿAiHoıOSwqkAѩ student ~ personA]bWzdҤA student s1 |۰ʿ person ܤkAPɧڭ̤]iHIs bmi() 禡ӭp s1 BMI ȡAƩwq @person ؿUC]MAڭ̤]iHb @student ؿUsgt@ɮ display.mAΨ student A student N|ΦkܡAӤ|ϥ person ܤkC^


MATLAB{]pGig