Java编程题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java编程题相关的知识,希望对你有一定的参考价值。
java写一个猴子类,必须提供它的名称,种类,体重,出生年月,生活地点等属性以及吃香蕉、跑动等方法,要求必须继承一个基类(基类自定义,子类重载基类的某些方法);
参考技术A 第一题:<pre t="code" l="java">public class People
private String name;
public String speak()
return "";
public String getName()
return name;
public void setName(String name)
this.name = name;
public class American extends People
private String name;
@Override
public String speak()
return "i’m speak English";
public String getName()
return name;
public void setName(String name)
this.name = name;
public class Chinese extends People
private String name;
@Override
public String speak()
return "我说中国话";
public String getName()
return name;
public void setName(String name)
this.name = name;
import java.util.HashMap;
import java.util.Map;
public class Demo01
public static Map<?, ?> recruit(People[] people)
Map<String, String> resultMap = new HashMap<String, String>();
for (People p : people)
if ("我说中国话".equals(p.speak()))
resultMap.put(p.getName(), "中国人");
else
resultMap.put(p.getName(), "美国人");
return resultMap;
public static void main(String[] args)
Chinese chinese01 = new Chinese();
chinese01.setName("小明");
Chinese chinese02 = new Chinese();
chinese02.setName("小红");
American american = new American();
american.setName("Tom");
People[] peoples = new People[] chinese01, chinese02, american ;
Map<String, String> resultMap = new HashMap<String, String>(3);
resultMap = (Map<String, String>) recruit(peoples);
for (Map.Entry<String, String> entry : resultMap.entrySet())
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + ":" + value);
/*
运行结果
Tom:美国人
小明:中国人
小红:中国人
*/ 第二题:<pre t="code" l="java">public class Engineer
private String name;
public String writerCode()
return "";
public String getName()
return name;
public void setName(String name)
this.name = name;
public class JavaEngineer extends Engineer
private String name;
@Override
public String writerCode()
return "在写Java代码";
public String getName()
return name;
public void setName(String name)
this.name = name;
public class iosEngineer extends Engineer
private String name;
@Override
public String writerCode()
return "在写iOS代码";
public String getName()
return name;
public void setName(String name)
this.name = name;
import java.util.HashMap;
import java.util.Map;
public class Demo02
public static Map<?, ?> recruit(Engineer[] Engineer)
Map<String, String> resultMap = new HashMap<String, String>();
for (Engineer e : Engineer)
if ("在写Java代码".equals(e.writerCode()))
resultMap.put(e.getName(), "Java工程师");
else
resultMap.put(e.getName(), "iOS工程师");
return resultMap;
public static void main(String[] args)
JavaEngineer JavaEngineer01 = new JavaEngineer();
JavaEngineer01.setName("Sunny");
JavaEngineer JavaEngineer02 = new JavaEngineer();
JavaEngineer02.setName("Jain");
iOSEngineer iOSEngineer = new iOSEngineer();
iOSEngineer.setName("Tony");
Engineer[] Engineers = new Engineer[] JavaEngineer01, JavaEngineer02, iOSEngineer ;
Map<String, String> resultMap = new HashMap<String, String>(3);
resultMap = (Map<String, String>) recruit(Engineers);
for (Map.Entry<String, String> entry : resultMap.entrySet())
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + ":" + value);
/*
运行结果:
Sunny:Java工程师
Jain:Java工程师
Tony:iOS工程师
*/
以上是关于Java编程题的主要内容,如果未能解决你的问题,请参考以下文章