编程1
import java.util.*;
abstract class Rodent//抽象类
{
public abstract String findFood();//抽象方法
public abstract String chewFood();
}
class Mouse extends Rodent
{
public String findFood()
{ return "大米"; }
public String chewFood()
{ return "Mouse"+"吃"+findFood(); }
}
class Gerbil extends Rodent
{
public String findFood()
{ return "种子"; }
public String chewFood()
{ return "Gerbil"+"吃"+findFood(); }
}
class Hamster extends Rodent
{
public String findFood()
{ return "坚果"; }
public String chewFood()
{ return "Hamster"+"吃"+findFood(); }
}
public class Main{
public static void main(String[] args)
{
Rodent[] rodents = new Rodent[3];
rodents[0] = new Mouse();
rodents[1] = new Gerbil();
rodents[2] = new Hamster();
System.out.println(rodents[0].chewFood());
System.out.println(rodents[1].chewFood());
System.out.println(rodents[2].chewFood());
}
}
Mouse吃大米
Gerbil吃种子
Hamster吃坚果
编程2
用接口改造上一题,实现要求的所有功能
接口详细教程Java 接口 | 菜鸟教程 (runoob.com)
import java.util.*;
interface Rodent
{
public String findFood();
public String chewFood();
}
class Mouse implements Rodent
{
public String findFood()
{ return "大米"; }
public String chewFood()
{ return "Mouse"+"吃"+findFood(); }
}
class Gerbil implements Rodent
{
public String findFood()
{ return "种子"; }
public String chewFood()
{ return "Gerbil"+"吃"+findFood(); }
}
class Hamster implements Rodent
{
public String findFood()
{ return "坚果"; }
public String chewFood()
{ return "Hamster"+"吃"+findFood(); }
}
public class Main{
public static void main(String[] args)
{
Rodent[] rodents = new Rodent[3];
rodents[0] = new Mouse();
rodents[1] = new Gerbil();
rodents[2] = new Hamster();
System.out.println(rodents[0].chewFood());
System.out.println(rodents[1].chewFood());
System.out.println(rodents[2].chewFood());
}
}
Mouse吃大米
Gerbil吃种子
Hamster吃坚果
编程3
import java.util.*;
interface Rodent
{
public String findFood();
public String chewFood();
}
interface Fly
{
public String fly();
}
class Mouse implements Rodent
{
public String findFood()
{ return "大米"; }
public String chewFood()
{ return "Mouse"+"吃"+findFood(); }
}
class Gerbil implements Rodent
{
public String findFood()
{ return "种子"; }
public String chewFood()
{ return "Gerbil"+"吃"+findFood(); }
}
class Hamster implements Rodent
{
public String findFood()
{ return "坚果"; }
public String chewFood()
{ return "Hamster"+"吃"+findFood(); }
}
class Pteromyini implements Rodent,Fly
{
public String findFood()
{ return "野果"; }
public String chewFood()
{ return "Pteromyini"+"吃"+findFood(); }
public String fly()
{ return "会飞"; }
}
public class Main{
public static void main(String[] args)
{
Rodent[] rodents = new Rodent[4];
rodents[0] = new Mouse();
rodents[1] = new Gerbil();
rodents[2] = new Hamster();
rodents[3] = new Pteromyini();
System.out.println(rodents[0].chewFood());
System.out.println(rodents[1].chewFood());
System.out.println(rodents[2].chewFood());
System.out.println(rodents[3].chewFood()+" "+((Pteromyini)rodents[3]).fly());
}
}
Mouse吃大米
Gerbil吃种子
Hamster吃坚果
Pteromyini吃野果 会飞
编程4
这道题给我看懵了,直接上AI
import java.util.Date;
public class MaxFinder {
// 实现找到Comparable对象数组中的最大元素的方法
public static Comparable max(Comparable[] a) {
if (a == null || a.length == 0) {
return null; // 返回null表示数组为空或未初始化
}
Comparable max = a[0]; // 假设第一个元素是最大的
for (int i = 1; i < a.length; i++) {
if (max.compareTo(a[i]) < 0) {
max = a[i]; // 发现更大的元素,更新max
}
}
return max;
}
public static void main(String[] args) {
// 测试字符串数组
String[] strings = {"apple", "orange", "banana", "zoo", "cherry", "date", "fig", "grape", "honeydew", "kiwi"};
System.out.println("最大的字符串: " + max(strings));
// 测试整数数组
Integer[] integers = {20, 35, 1, 67, 55, 23, 99, 18, 42, 88};
System.out.println("最大的整数: " + max(integers));
// 测试日期数组
Date[] dates = new Date[10];
for (int i = 0; i < dates.length; i++) {
// 创建从现在开始的不同天的日期
dates[i] = new Date(System.currentTimeMillis() + i * 24 * 60 * 60 * 1000);
}
System.out.println("最新的日期: " + max(dates));
}
}
最大的字符串: zoo
最大的整数: 99
最新的日期: Mon Apr 22 21:35:46 CST 2024