zoukankan      html  css  js  c++  java
  • 继承和多态举例

    程序1

    需求:建立一个人类(Person)和学生类(Student)功能如下:

    1)Person包含4个数据成员name、addr、gender和age,分别表示姓名、地址、类别和年龄。设计一个输出方法talk()来显示这4个属性。

    2)Student类继承Person类,并增加成员Math和English来存放数学和英语成绩,用一个6参数构造方法、一个2参数构造方法、一个无参构造方法和覆写输出方法talk()用于显示6种属性。对于构造方法参数个数不足以初始化数据成员的,在构造方法中采用自己指定默认值来实施初始化。

    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;
    	}
    	
    }
    

      运行结果:

    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:

    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)通过赋值操作,将这些子类乐器对象向上类型转换为Instrument类型,然后经过父类对象ins调用其play方法,这时,实际调用的是各个子类对象的play方法。

      2)父类对象依据被赋值的每个子类对象的类型,做出恰当的相应(即与对象具体类别相适应的反应),这就是对象多态性的关键思想。同样的消息或者接口(本例中都是play)在发送给不同的对象时,会产生多种形式的结果。

      3)继承是子类使用父类的方法,多态则是父类使用子类的方法。确切的说,多态是父类使用被子类覆盖的同名方法,如果子类的方法是全新的,父类不存在同名的方法,则父类也不能使用子类自己独有的方法。

  • 相关阅读:
    Log4php使用指南
    【JQuery】使用JQuery 合并两个 json 对象
    【前端】JS截取字符串常用方法详细整理
    【.Net】net 反射15分钟速成
    【.Net】win10 uwp unix timestamp 时间戳 转 DateTime
    【ASP.NET Core】ASP.NET Core 依赖注入
    【ASP.NET 框架系列】您所经历的,但未必研究的那些技术
    Visual Studio 中设置npm
    【数据库】SQL分组多列统计(GROUP BY后按条件分列统计)
    【数据库】同一字段根据不同条件更新的sql语句的写法
  • 原文地址:https://www.cnblogs.com/Andya/p/5700138.html
Copyright © 2011-2022 走看看