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

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


实现效果:

根据用户输入的一组信息,批量构建不同人种类,这里我要求用户在每个人的身高体重信息前输入一个数字作为标识符(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

相关文章

Java AOP面向切面编程的概念和实现方式

《JavaAOP面向切面编程的概念和实现方式》AOP是面向切面编程,通过动态代理将横切关注点(如日志、事务)与核心业务逻辑分离,提升代码复用性和可维护性,本文给大家介绍JavaAOP面向切面编程的概... 目录一、AOP 是什么?二、AOP 的核心概念与实现方式核心概念实现方式三、Spring AOP 的关

JavaScript对象转数组的三种方法实现

《JavaScript对象转数组的三种方法实现》本文介绍了在JavaScript中将对象转换为数组的三种实用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友... 目录方法1:使用Object.keys()和Array.map()方法2:使用Object.entr

Java集合之Iterator迭代器实现代码解析

《Java集合之Iterator迭代器实现代码解析》迭代器Iterator是Java集合框架中的一个核心接口,位于java.util包下,它定义了一种标准的元素访问机制,为各种集合类型提供了一种统一的... 目录一、什么是Iterator二、Iterator的核心方法三、基本使用示例四、Iterator的工

Java 线程池+分布式实现代码

《Java线程池+分布式实现代码》在Java开发中,池通过预先创建并管理一定数量的资源,避免频繁创建和销毁资源带来的性能开销,从而提高系统效率,:本文主要介绍Java线程池+分布式实现代码,需要... 目录1. 线程池1.1 自定义线程池实现1.1.1 线程池核心1.1.2 代码示例1.2 总结流程2. J

利用Python操作Word文档页码的实际应用

《利用Python操作Word文档页码的实际应用》在撰写长篇文档时,经常需要将文档分成多个节,每个节都需要单独的页码,下面:本文主要介绍利用Python操作Word文档页码的相关资料,文中通过代码... 目录需求:文档详情:要求:该程序的功能是:总结需求:一次性处理24个文档的页码。文档详情:1、每个

JS纯前端实现浏览器语音播报、朗读功能的完整代码

《JS纯前端实现浏览器语音播报、朗读功能的完整代码》在现代互联网的发展中,语音技术正逐渐成为改变用户体验的重要一环,下面:本文主要介绍JS纯前端实现浏览器语音播报、朗读功能的相关资料,文中通过代码... 目录一、朗读单条文本:① 语音自选参数,按钮控制语音:② 效果图:二、朗读多条文本:① 语音有默认值:②

Vue实现路由守卫的示例代码

《Vue实现路由守卫的示例代码》Vue路由守卫是控制页面导航的钩子函数,主要用于鉴权、数据预加载等场景,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着... 目录一、概念二、类型三、实战一、概念路由守卫(Navigation Guards)本质上就是 在路

uni-app小程序项目中实现前端图片压缩实现方式(附详细代码)

《uni-app小程序项目中实现前端图片压缩实现方式(附详细代码)》在uni-app开发中,文件上传和图片处理是很常见的需求,但也经常会遇到各种问题,下面:本文主要介绍uni-app小程序项目中实... 目录方式一:使用<canvas>实现图片压缩(推荐,兼容性好)示例代码(小程序平台):方式二:使用uni

JAVA实现Token自动续期机制的示例代码

《JAVA实现Token自动续期机制的示例代码》本文主要介绍了JAVA实现Token自动续期机制的示例代码,通过动态调整会话生命周期平衡安全性与用户体验,解决固定有效期Token带来的风险与不便,感兴... 目录1. 固定有效期Token的内在局限性2. 自动续期机制:兼顾安全与体验的解决方案3. 总结PS

Java Instrumentation从概念到基本用法详解

《JavaInstrumentation从概念到基本用法详解》JavaInstrumentation是java.lang.instrument包提供的API,允许开发者在类被JVM加载时对其进行修改... 目录一、什么是 Java Instrumentation主要用途二、核心概念1. Java Agent