• 定义类Human,具有若干属性和功能;定义其子类Man、Woman; 在主类Test中分别创建子类、父类和上转型对象,并测试其特性。


    package human;

    public class Human
    {
    //成员属性
    private String name;
    private int age;
    private double height;
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public int getAge() {
    return age;
    }
    public void setAge(int age) {
    this.age = age;
    }
    public double getHeight() {
    return height;
    }
    public void setHeight(double height) {
    this.height = height;
    }
    //成员方法
    public void speak()
    {
    System.out.println("你喜欢什么运动呢?");
    }
    public void yundong()
    {
    System.out.println("我喜欢打篮球");
    }

    }


    package human;

    public class Man extends Human
    {
    //重写父类的方法
    public void speak()
    {
    System.out.println("我是男生,我叫"+this.getName()+"我的年龄是:"+this.getAge()+"我的身高为:"+this.getHeight());
    }
    public void yundong()
    {
    System.out.println("我最喜欢的运动是玩游戏,德玛西亚....");
    }
    }


    package human;

    public class Woman extends Human
    {
    //重写父类方法
    public void speak()
    {
    System.out.println("我是女生,我叫"+this.getName()+"我的年龄是:"+this.getAge()+"我的身高为:"+this.getHeight());
    }
    public void yundong()
    {
    System.out.println("我最喜欢的运动是羽毛球");
    }
    }


    package human;

    public class Text_human
    {

    public static void main(String[] args)
    {
    //实例化man对象
    Man m = new Man();
    m.setName("张小明");
    m.setAge(18);
    m.setHeight(178);
    m.speak();
    m.yundong();

    //实例化woman对象
    Woman wm = new Woman();
    wm.setName("张小红");
    wm.setAge(16);
    wm.setHeight(160);
    wm.speak();
    wm.yundong();

    //实例化对象
    Human h=new Human();
    h.speak();
    h.yundong();

    //定义上转型对象
    Human hm=new Man();
    hm.setName("tom");
    hm.setAge(19);
    hm.setHeight(178);
    hm.speak();
    hm.yundong();

    }

    }

  • 相关阅读:
    方法的调用,管道的演示,返回多个值,包的概念
    GO的安装
    React--后台管理系统--项目框架的搭建
    将字符串转换为整数--在处理-保留任意小数
    安装react-app脚手架
    git参考手册
    Python切片
    python字符串常用方法、分割字符串等
    python-字符串格式化
    python-列表增删改查、排序、两个list合并、多维数组等
  • 原文地址:https://www.cnblogs.com/smile-dream/p/5897535.html
Copyright © 2020-2023  润新知