千锋逆战班,接口习题

在千锋学习的第18天
熬过这段孤独我就是王者!加油
今天我学习了接口,他是以后经常要是用到的。
下面是我做的接口的习题。
在这里插入图片描述
第一问:需要实现ma(),mb(),mc(),md()方法
第二问:先ClassE newE = (ClassE)ic,然后调用 newE.ma();newE.mb();newE.mc();newE.md();
第三问:true,true,true,true,true

在这里插入图片描述
结果为:true,true,true,true

在这里插入图片描述
答案为:ABCE

6.输出以下代码结果
在这里插入图片描述

运行结果为:Red Light shine in Red,Yellow Light shine in Yellow,Green Light shine in Green

在这里插入图片描述
结果为:TeacherA teach Java ,TeacherB teach Java

在这里插入图片描述
结果为:
(1)
for(int i = 0 ;i<as.length;i++){
as[i].eat();
}
(2)
for(int j = 0; j< as.length;j++){
if(as[i] instanceof Pet){
Pet pet = (Pet)a[i]
pet.play();
}
}

在这里插入图片描述
代码如下:

package q9;



public class Testsalary {

	public static void main(String[] args) {		
		
		
		Employee[] emp = new Employee[]{
				new SalariedEmployee("tom",10,4000.0),
				new HourlyEmployee("tuone",11,18.0,110),
				new HourlyEmployee("semdi",11,14.0,260),
				new SalesEmployee("lily",8,202000,0.06),
				new BasePlusSalesEmployee("小红",6,20000,0.04,3000.0)
				};
		
		System.out.println("本月总加班费为" + sum(emp));
		double dd = emp[2].getSalary(11);
		System.out.println(dd);
	}
	public static void print(Employee e,int month){
		System.out.println(e.getName() + "的"+ month +"月份工资为" + e.getSalary(month));
	}
	public static double sum(Employee[] emp){
		double sums = 0;//本月发放的加班费

		for(int i=0; i< emp.length;i++){
			
			if(emp[i] instanceof SalariedEmployee){
				sums = sums +2000;
			}
			else if(emp[i] instanceof BasePlusSalesEmployee){
				sums = sums + 1000;
			}
		}
		return sums;
	}

}



class Employee{
	private String name;
	private int month;
	public Employee(){}
	public Employee(String name, int month) {
		super();
		this.name = name;
		this.month = month;
	}
	
	public double getSalary(int month){
		if(this.month == month){
			return 100.0;
		}else{
			return 0.0;
		}
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getMonth(){
		
		return this.month;
	}
	public void setMonth(int month){
		this.month = month;
	}
}
class SalariedEmployee extends Employee{//固定工资员工
	
	private double salary;
	public SalariedEmployee(){}
	public SalariedEmployee(String name,int month,double salary){
		
		super(name,month);
		this.salary = salary;
	}
	public double getSalary(int month){
		double bns = super.getSalary(month);
		return this.salary + bns+2000;
	}
	public double getSalary() {
		return salary;
	}
	public void setSalary(double salary) {
		this.salary = salary;
	}
	
}

class HourlyEmployee extends Employee{//按小时拿工资的员工, 每月工作超出160小时的部分按照1.5倍工资发放。
	private double hourySalary;
	private int hours;
	public HourlyEmployee(){}
	public HourlyEmployee(String name,int month , double hourySalary,int hours){
		super(name,month);
		this.hourySalary = hourySalary;
		this.hours = hours;
		
	}
	public double getSalary(int month){
		double bns = super.getSalary(month);
		if(this.hours>0 && this.hours <160){
			return this.hourySalary * 160 + bns;
		}
		else{
			return this.hourySalary * 160 + (this.hours-160) * this.hourySalary *1.5 +bns;
		}
	
	}
	public double getHourySalary() {
		return hourySalary;
	}
	public void setHourySalary(double hourySalary) {
		this.hourySalary = hourySalary;
	}
	public int getHours() {
		return hours;
	}
	public void setHours(int hours) {
		this.hours = hours;
	}
	
}

class SalesEmployee extends Employee{//销售,工资由月销售额和提成率决定。
	private double sales;//月销售额
	private double rate;// 提成率
	public SalesEmployee(){}
	public SalesEmployee(String name,int month,double sales,double rate){
		super(name,month);
		this.rate = rate;
		this.sales = sales;
	}
	
	public double getSalary(int month){
		
		double bns = super.getSalary(month);
		return this.sales * this.rate + bns;
	

	}
	public double getSales() {
		return sales;
	}

	public double getRate() {
		return rate;
	}

}
class BasePlusSalesEmployee extends SalesEmployee{//有固定底薪的销售人员,工资由底薪加上销售提成部分。
	private double baseSalary;
	public BasePlusSalesEmployee(){}
	public BasePlusSalesEmployee(String name,int month , double sales ,double rate,double baseSalary) {
		super(name,month,rate,sales);
		this.baseSalary = baseSalary;
	}
	public double getSalary(int month){
		double bns = super.getSalary(month);
	
		return this.baseSalary + bns+1000;

	}

	
}


在这里插入图片描述

第一种情况需要实现接口后,重写接口内所有的抽象方法,第二种因为在父类中都实现了接口中的抽象方法,所以在MyService类中不需要重写抽象方法了。AbstractService就是提前重写了接口中的抽象方法而且它的子类也不需要实现接口了

在这里插入图片描述

代码为:

package q11;


import java.util.*;
public class Test11 {

	public static void main(String[] args) {
		Goldbach cc = new Goldbach();
		int[] arrs = cc.numarr(cc.nums());
		if(arrs[0] != 0){
			System.out.println(arrs[0] + "和" + arrs[1]);
		}else{
			System.out.println("该数没有两个质数和");
		}
		
	}
	
	
}

class Goldbach implements Test111{
	
	//判断输入的数是否为大于6的偶数
	public int nums(){
			
		Scanner sc = new Scanner(System.in);
		while(true){
			System.out.println("请输入一个大于6的偶数:");
			int num = sc.nextInt();
			if(num % 2 ==0 && num > 6){
				return num;
			}
					
		}
	}
	public int[] numarr(int a){
		for(int i =1 ; i <= a /2 ; i++){
			if(m(i) && m(a - i)){
				return new int[]{i,a-i};
			}
		}
		return new int[0];
	}
	
	//实现了接口判断质数true就是质数false不是质数
	@Override
	public boolean m(int a) {
		for(int i = 2 ;i <= Math.sqrt(a);i++){//一个数的每对因数他们其中最小的肯定是小于等于该数的算数平方根,仔细品品
			
			if(a % i ==0){
				return false;
			}
		}
		return true;
	
	}
	
}

接口代码

package q11;
/**
 * 
 * 判断质数 
 * @author Dell
 *
 */
public interface Test111 {
	
	boolean m(int a);
}

欢迎大家指正。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值