继承和多态举例
程序1
需求:建立一个人类(Person)和学生类(Student)功能如下:
1)Person包含4个数据成员name、addr、gender和age,分别表示姓名、地址、类别和年龄。设计一个输出方法talk()来显示这4个属性。
2)Student类继承Person类,并增加成员Math和English来存放数学和英语成绩,用一个6参数构造方法、一个2参数构造方法、一个无参构造方法和覆写输出方法talk()用于显示6种属性。对于构造方法参数个数不足以初始化数据成员的,在构造方法中采用自己指定默认值来实施初始化。
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | package com.liaojianya.chapter2; /** * This program demonstrates the use of polymorphisn. * @author LIAO JIANYA * 2016年7月24日 */ public class PersonStudent { public static void main(String[] args) { Person p = new Student( "王二" , "上海" , "female" , 44 , 100 , 53 ); System.out.println(p.talk()); Person p1 = new Student( 55 , 66 ); System.out.println(p1.talk()); Person p2 = new Student(); System.out.println(p2.talk()); } } class Person { String name; String addr; String gender; int age; public Person() { } public Person(String name, String addr, String gender, int age) { this .name = name; this .addr = addr; this .gender = gender; this .age = age; } public String talk() { return "My name is " + name + ", address is " + addr + ", my gender is " + gender + ", I'm " + age + " years old." ; } } class Student extends Person { float Math; float English; public Student(String name, String addr, String gender, int age, float Math, float English) { super (name, addr, gender, age); this .Math = Math; this .English = English; } public Student( float Math, float English) { this .name = "wangyuan" ; this .addr = "nanjing" ; this .gender = "male" ; this .age = 33 ; this .Math = Math; this .English = English; } public Student() { this .name = "王小元" ; this .addr = "南京" ; this .gender = "男" ; this .age = 23 ; this .Math = 98 .5f; this .English = 56 .6f; } public String talk() { return super .talk() + "My Math score is " + Math + ", and my English score is " +English; } } |
运行结果:
1 2 3 | My name is 王二, address is 上海, my gender is female, I'm 44 years old.My Math score is 100.0 , and my English score is 53.0 My name is wangyuan, address is nanjing, my gender is male, I'm 33 years old.My Math score is 55.0 , and my English score is 66.0 My name is 王小元, address is 南京, my gender is 男, I'm 23 years old.My Math score is 98.5 , and my English score is 56.6 |
程序2:
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | package com.liaojianya.chapter2; /** * 定义一个Instrument乐器类,并定义其公有的方法play(),再分别定义其子类Wind(管乐器) * Percussion(打击乐器),Stringed(弦乐器),覆写play方法,实现每种乐器独有的play方式。 * 最后在测试类中使用多态的方法执行每个子类的play()方法。 * @author LIAO JIANYA * 2016年7月24日 */ public class InstrumentTest { public static void main(String[] args) { Instrument ins; Wind w = new Wind(); ins = w; ins.play(); Percussion p = new Percussion(); ins = p; ins.play(); Stringed s = new Stringed(); ins = s; ins.play(); } } class Instrument { public void play() { System.out.println( "乐器演奏!" ); } } class Wind extends Instrument { public void play() { System.out.println( "管乐器演奏!" ); } } class Percussion extends Instrument { public void play() { System.out.println( "打击乐器演奏 !" ); } } class Stringed extends Instrument { public void play() { System.out.println( "弦乐器演奏!" ); } } |
运行结果:
1 2 3 | 管乐器演奏! 打击乐器演奏 ! 弦乐器演奏! |
分析:
1)通过赋值操作,将这些子类乐器对象向上类型转换为Instrument类型,然后经过父类对象ins调用其play方法,这时,实际调用的是各个子类对象的play方法。
2)父类对象依据被赋值的每个子类对象的类型,做出恰当的相应(即与对象具体类别相适应的反应),这就是对象多态性的关键思想。同样的消息或者接口(本例中都是play)在发送给不同的对象时,会产生多种形式的结果。
3)继承是子类使用父类的方法,多态则是父类使用子类的方法。确切的说,多态是父类使用被子类覆盖的同名方法,如果子类的方法是全新的,父类不存在同名的方法,则父类也不能使用子类自己独有的方法。
烧不死的鸟就是凤凰
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET10 - 预览版1新功能体验(一)