java题目18:定义排序功能抽象方法的接口A和求平均值功能抽象方法的接口B,定义类同时实现接口A和B,在主类主方法中使用该接口或类的对象调用方法对、数2,3,8,-6,1,9按升序排序并计算平均值

每日小语

我们试图用我们仅有的天分去表达我们的深层感受,去表达我们对前人所有贡献的感激,去为历史长河加上一点什么,这就是推动我的力量。——乔布斯

思考

定义一个排序功能抽象方法的接口A和求平均值功能值抽象方法的接口B,然后定义一个类同时实现接口A和B,在主类主方法中使用该接口或类的对象调用方法对、数2,3,8,-6,1,9按升序排序并计算平均值,最后输出排序结果和平均值。(Interface18)

如何定义抽象接口

public interface MyInterface {
    // 声明一个常量
    public static final int MY_CONSTANT = 10;

    // 声明一个抽象方法
    public void myMethod();
}

定义一个类同时实现接口A和B

//定义一个排序功能抽象方法的接口A和求平均值功能值抽象方法的接口B,然后定义一个类同时实现接口A和B,在主类主方法中使用该接口或类的对象调用方法对数2,3,8,-6,1,9按升序排序并计算平均值,最后输出排序结果和平均值。(Interface18)
public class Interface18 
{
	static int[] array = {2,3,8,-6,1,9};
	public interface A
		{
			// 排序抽象方法
			public static void sort()
			{
				for(int i=0;i<6;i++)
				{
					for(int j=0;j<5;j++)
					{
						if(array[i]>array[j])
						{
							int t = array[j];
							array[j]=array[i];
							array[i]=t;
							System.out.print(array[i]+" ");
						}
					}
				}
				
			}
			
	    }
		public interface B
		{
			// 平均值抽象方法
			public static void average()
			{
				int a=0;
				for(int i=0;i<6;i++)
				{
					a=array[i]+a;
				}
				System.out.print("平均值为: "+a);
			}
		}
		// 定义一个类同时实现接口A和B
		class test implements A, B 
		{
			public void sort() 
			{
				System.out.println("实现接口A中的排序");
			}
			public void average() 
			{
	    		System.out.println("实现接口B中的求平均值");
			}
		}
		
}

也没划红线啊,可就是运行不了

终极代码

//定义一个排序功能抽象方法的接口A和求平均值功能值抽象方法的接口B,然后定义一个类同时实现接口A和B,在主类主方法中使用该接口或类的对象调用方法对数2,3,8,-6,1,9按升序排序并计算平均值,最后输出排序结果和平均值。(Interface18)
public class Interface18 
{ 
	int[] array = {2,3,8,-6,1,9};
	public interface A
		{
			// 排序抽象方法
			void sort();
		}
		public interface B
		{
			// 平均值抽象方法
			void average();
		}
		// 定义一个类同时实现接口A和B
		class test implements A, B 
		{
			public void sort() 
			{
				for(int i=0;i<6;i++)
				{
					for(int j=i+1;j<5;j++)//从i+1开始
					{
						if(array[i]>array[j])
						{
							int t = array[j];
							array[j]=array[i];
							array[i]=t;
						}
						
					}
					System.out.print(array[i]+" ");//注意放到外循环里
				}
			}
			public void average() 
			{
				int a=0;
				for(int i=0;i<6;i++)
				{
					a=array[i]+a;
				}
				System.out.println("平均值为: "+a);
			}
		}
		public static void main(String[] args)
		{
			Interface18 testObj = new Interface18();
			Interface18.test obj = testObj.new test();
			obj.sort();
			obj.average();

	    }
	
}	

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值