(附代码)根据输入信息构建不同人种类,统一打印不同国家地区的身高、体重信息(泛型数组应用,多态、重载、泛型、反射等概念体验)

本文主要是介绍(附代码)根据输入信息构建不同人种类,统一打印不同国家地区的身高、体重信息(泛型数组应用,多态、重载、泛型、反射等概念体验),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


实现效果:

根据用户输入的一组信息,批量构建不同人种类,这里我要求用户在每个人的身高体重信息前输入一个数字作为标识符(1 for America, 2 for Beijing, 3 forChina, 4 for others)。

把这些人按人种归类后,统一打印不同国家地区的身高、体重信息,给出平均身高、平均体重,并让不同人种以独有形式speakHello、演示特技。

最后再打印一次全世界所有人的身高、体重信息,给出平均身高、平均体重,一起sayHello。

测试截图:

首先输入一批人的信息,前后两个人的信息可以换行或空格隔开,不影响输入,这里为了看得清楚换行隔开。

 

输入结束后,系统把这些人按人种归类,统一打印不同国家地区的身高、体重信息,给出平均身高、平均体重,并让不同人种以独有形式speakHello、演示特技。最后再打印一次全世界所有人的身高、体重信息,给出平均身高、平均体重,一起sayHello。


 

程序清单:

TestPeople.java

 import java.util.ArrayList;

import java.util.Scanner;

 

public class TestPeople {

 

        @SuppressWarnings("null")

        publicstatic void main(String[] args) {

                 //TODO Auto-generated method stub

                 Scannerscan = new Scanner(System.in);

                 System.out.println("Enterthe adress (1 for America, 2 for Beijing, 3 for China, 4 for others), ");

                 System.out.println("heightand weight of people: ");

                

                 inttag;

                 doubleh, w;

                

                 ArrayList<AmericanPeople>A = new ArrayList<AmericanPeople>();

                 ArrayList<BeijingPeople>B = new ArrayList<BeijingPeople>();

                 ArrayList<ChinaPeople>C = new ArrayList<ChinaPeople>();

                 ArrayList<People>P = new ArrayList<People>();

                

                 AmericanPeoplea = null;

                 BeijingPeopleb = null;

                 ChinaPeoplec = null;

                 Peoplep = null;

                

                 while(scan.hasNext()) {

                         tag= scan.nextInt();

                         h= scan.nextDouble();

                         w = scan.nextDouble();

                        

                         if(tag == 1) {

                                  a= new AmericanPeople(h, w);

                                  A.add(a);

                                  P.add(a);

                         }

                         elseif (tag == 2) {

                                  b= new BeijingPeople(h, w);

                                  B.add(b);

                                  P.add(b);

                         }

                         elseif (tag == 3) {

                                  c= new ChinaPeople(h, w);

                                  C.add(c);

                                  P.add(c);

                         }

                         else{

                                  p= new People(h, w);

                                  P.add(p);

                         }

                 }

                

                 System.out.println("Listof American people:");

                 System.out.println("Height   Weight");

                 for(AmericanPeople ap : A) {

                         System.out.printf("%.2fm    %.2fkg\n", ap.height, ap.weight);

                 }

                 System.out.printf("Theaverage of height is %.2fm.\n", a.averageHeight(a.sumah, a.an));

                 System.out.printf("Theaverage of weight is %.2fkg.\n", a.averageWeight(a.sumaw, a.an));

                 System.out.println("Andthey say:");

                 a.speakHello();

                 a.AmericanBoxing();

                 System.out.printf("\n\n");

                

                 System.out.println("Listof Beijing people:");

                 System.out.println("Height   Weight");

                 for(BeijingPeople bp : B) {

                         System.out.printf("%.2fm    %.2fkg\n", bp.height, bp.weight);

                 }

                 System.out.printf("Theaverage of height is %.2fm.\n", b.averageHeight(b.sumbh, b.bn));

                 System.out.printf("Theaverage of weight is %.2fkg.\n", b.averageWeight(b.sumbw, b.bn));

                 System.out.println("Andthey say:");

                 b.speakHello();

                 b.BeijingOpera();

                 System.out.printf("\n\n");

                

                 System.out.println("Listof China people:");

                 System.out.println("Height   Weight");

                 for(ChinaPeople cp : C) {

                         System.out.printf("%.2fm    %.2fkg\n", cp.height, cp.weight);

                 }

                 System.out.printf("Theaverage of height is %.2fm.\n", c.averageHeight(c.sumch, c.cn));

                 System.out.printf("Theaverage of weight is %.2fkg.\n", c.averageWeight(c.sumcw, c.cn));

                 System.out.println("Andthey say:");

                 c.speakHello();

                 c.ChinaMartial();

                 System.out.printf("\n\n");

                

                 System.out.println("Listof All people:");

                 System.out.println("Height   Weight");

                 for(People pp : P) {

                         System.out.printf("%.2fm    %.2fkg\n", pp.height, pp.weight);

                 }

                 System.out.printf("Theaverage of height is %.2fm.\n", p.averageHeight(p.sumh, p.n));

                 System.out.printf("Theaverage of weight is %.2fkg.\n", p.averageWeight(p.sumw, p.n));

                 System.out.println("Andthey say:");

                 p.speakHello();

                 System.out.printf("\n\n");

 

        }

 

}


 

People.java

 

public class People {

    double height, weight;

   

    static double sumh = 0;

    static double sumw = 0;

    static int n = 0;

   

    People(doubleh,doublew) {

        height = h;

        weight = w;

       

        sumh += h;

        sumw += w;

        n++;

    }

   

    void speakHello() {

        System.out.println("Hello!");

    }

   

    double averageHeight(doublesumh,intn){

        return (double)sumh/n;

    }

   

    double averageWeight(doublesumw,intn){

        return (double)sumw/n;

    }

 

}

 

ChinaPeople.java

 

public class ChinaPeople extends People{

 

    static double sumch = 0;

    static double sumcw = 0;

    static int cn = 0;

 

    ChinaPeople(doubleh,doublew) {

        super(h,w);

        // TODO Auto-generated constructor stub

        sumch += h;

        sumcw += w;

        cn++;

    }

   

    /*Override*/

    void speakHello() {

        System.out.println("你好!");

    }

   

    double averageHeight(doublesumch,intcn){

        return (double)sumch/cn;

    }

   

    double averageWeight(doublesumcw,intcn){

        return (double)sumcw/cn;

    }

   

    void ChinaMartial() {

        System.out.println("嚯嚯哈嘿,快使用双截棍!");

    }

 

}

 

AmericanPeople.java

 

 

public class AmericanPeople extends People{

 

    static double sumah = 0;

    static double sumaw = 0;

    static int an = 0;

 

    AmericanPeople(doubleh,doublew) {

        super(h,w);

        // TODO Auto-generated constructor stub

        sumah += h;

        sumaw += w;

        an++;

    }

   

    /*Override*/

    void speakHello() {

        System.out.println("Hallo!!");

    }

   

    double averageHeight(doublesumah,intan){

        return (double)sumah/an;

    }

   

    double averageWeight(doublesumaw,intan){

        return (double)sumaw/an;

    }

   

    void AmericanBoxing() {

        System.out.println("Ruff!!! I will beat u!");

    }

 

}

 

BeijingPeople.java

 

public class BeijingPeople extends People{

 

    static double sumbh = 0;

    static double sumbw = 0;

    static int bn = 0;

 

    BeijingPeople(doubleh,doublew) {

        super(h,w);

        // TODO Auto-generated constructor stub

        sumbh += h;

        sumbw += w;

        bn++;

    }

   

    /*Override*/

    void speakHello() {

        System.out.println("Hallo!!");

    }

   

    double averageHeight(doublesumbh,intbn){

        return (double)sumbh/bn;

    }

   

    double averageWeight(doublesumbw,intbn){

        return (double)sumbw/bn;

    }

   

    void BeijingOpera() {

        System.out.println("~啊啊啊~啊啊啊啊啊~");

    }

 

}

 

这篇关于(附代码)根据输入信息构建不同人种类,统一打印不同国家地区的身高、体重信息(泛型数组应用,多态、重载、泛型、反射等概念体验)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/789426

相关文章

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

Python实现Excel批量样式修改器(附完整代码)

《Python实现Excel批量样式修改器(附完整代码)》这篇文章主要为大家详细介绍了如何使用Python实现一个Excel批量样式修改器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录前言功能特性核心功能界面特性系统要求安装说明使用指南基本操作流程高级功能技术实现核心技术栈关键函

PHP应用中处理限流和API节流的最佳实践

《PHP应用中处理限流和API节流的最佳实践》限流和API节流对于确保Web应用程序的可靠性、安全性和可扩展性至关重要,本文将详细介绍PHP应用中处理限流和API节流的最佳实践,下面就来和小编一起学习... 目录限流的重要性在 php 中实施限流的最佳实践使用集中式存储进行状态管理(如 Redis)采用滑动

SpringBoot实现不同接口指定上传文件大小的具体步骤

《SpringBoot实现不同接口指定上传文件大小的具体步骤》:本文主要介绍在SpringBoot中通过自定义注解、AOP拦截和配置文件实现不同接口上传文件大小限制的方法,强调需设置全局阈值远大于... 目录一  springboot实现不同接口指定文件大小1.1 思路说明1.2 工程启动说明二 具体实施2

JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法

《JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法》:本文主要介绍JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法,每种方法结合实例代码给大家介绍的非常... 目录引言:为什么"相等"判断如此重要?方法1:使用some()+includes()(适合小数组)方法2

Three.js构建一个 3D 商品展示空间完整实战项目

《Three.js构建一个3D商品展示空间完整实战项目》Three.js是一个强大的JavaScript库,专用于在Web浏览器中创建3D图形,:本文主要介绍Three.js构建一个3D商品展... 目录引言项目核心技术1. 项目架构与资源组织2. 多模型切换、交互热点绑定3. 移动端适配与帧率优化4. 可

深入浅出Spring中的@Autowired自动注入的工作原理及实践应用

《深入浅出Spring中的@Autowired自动注入的工作原理及实践应用》在Spring框架的学习旅程中,@Autowired无疑是一个高频出现却又让初学者头疼的注解,它看似简单,却蕴含着Sprin... 目录深入浅出Spring中的@Autowired:自动注入的奥秘什么是依赖注入?@Autowired

Redis实现高效内存管理的示例代码

《Redis实现高效内存管理的示例代码》Redis内存管理是其核心功能之一,为了高效地利用内存,Redis采用了多种技术和策略,如优化的数据结构、内存分配策略、内存回收、数据压缩等,下面就来详细的介绍... 目录1. 内存分配策略jemalloc 的使用2. 数据压缩和编码ziplist示例代码3. 优化的

redis-sentinel基础概念及部署流程

《redis-sentinel基础概念及部署流程》RedisSentinel是Redis的高可用解决方案,通过监控主从节点、自动故障转移、通知机制及配置提供,实现集群故障恢复与服务持续可用,核心组件包... 目录一. 引言二. 核心功能三. 核心组件四. 故障转移流程五. 服务部署六. sentinel部署

Python 基于http.server模块实现简单http服务的代码举例

《Python基于http.server模块实现简单http服务的代码举例》Pythonhttp.server模块通过继承BaseHTTPRequestHandler处理HTTP请求,使用Threa... 目录测试环境代码实现相关介绍模块简介类及相关函数简介参考链接测试环境win11专业版python