千锋逆战班学习日志


千锋逆战班学习第18天
努力或许没有收获,但不努力一定没收获,加油。
今天我学了Java课程的接口。
中国加油!!!武汉加油!!!千锋加油!!!我自己加油!!!

总结-接口

  1. 接口的多态:不再关注具体类型,而是关注行为。
  2. 常见关系:
    • 类与类:
      • 单继承
      • extends 父类名称
    • 类与接口
      • 多实现
      • Implements 接口名1,接口名2,接口名n
    • 接口与接口
      • 多继承
      • extends 父接口1,父接口,父接口n
  3. 常量接口
    • 将多个常用于状态或固定值的变量,以静态常量的形式定义在接口中统一管理,提高代码可读性。
  4. 宏观概念:接口是一种标准。
    耦合度:模块与模块之间的关联程度,关联的越密切,耦合越高,关联的越松散,耦合越低。
  5. 接口回调:先有接口的使用者,后有接口的实现类。
  6. 接口的好处
    (1)程序的耦合度降低。
    (2)更自然的使用多态。
    (3)设计与实现完全分离。
    (4)更容易搭建程序框架。
    (5)更容易更换具体实现。

Question_8-3

有如下代码:

interface IA {
	void ma();
}
interface IB extends IA {
	void mb();
}

interface IC {
	void mc();
}
interface ID extends IB,IC {
	void md();
}

1.如果有一个类ClassE实现ID接口,ClassE不抽象,则要实现哪些方法?

class ClassE implements ID{
	public void ma() {}
	public void mb() {}
	public void mc() {}
	public void md() {}
}

把下面代码补充完整:

public class TestClassE {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		IC ic = new ClassE();
		//调用ma方法
		______________
		//调用mb方法
		______________
		//调用mc方法
		______________
		//调用md方法
		______________
	}
}

填:
ClassE ce = (ClassE)ic;
ce.ma();
ce.mb();
ce.mc();
ce.md();
3.输出以下代码结果

public class TestClassE {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		IC ic = new ClassE();
		System.out.println(ic instanceof IA);
		System.out.println(ic instanceof IB);
		System.out.println(ic instanceof IC);
		System.out.println(ic instanceof ID);
		System.out.println(ic instanceof ClassE);
	}
}

在这里插入图片描述

Question_8-4

有输出如下代码结果:

interface IA {
	void ma();
}
interface IB extends IA {
	void mb();
}
class MySuper implements IA{
	public void ma() {}
}
class MySub extends MySuper implements IB{
	public void ma() {}
	public void mb() {}
}
public class TestMain {
	public static void main(String[] args) {
		MySuper ms = new MySub();
		System.out.println(ms instanceof IA);
		System.out.println(ms instanceof IB);
		System.out.println(ms instanceof MySuper);
		System.out.println(ms instanceof MySub);
	}
}

在这里插入图片描述

Question_8-5

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

Question_8-6

有输出如下代码结果:

interface Light{
	void shine();
}
class RedLight implements Light{
	public void shine() {
		System.out.println("Red Light shine in Red");
	}
}
class YellowLight implements Light{
	public void shine() {
		System.out.println("Yellow Light shine in Yellow");
	}
}
class GreenLight implements Light{
	public void shine() {
		System.out.println("Green Light shine in Green");
	}
}
class Lamp{
	private Light light;
	public void setLight(Light light) {
		this.light = light;
	}
	public void on() {
		light.shine();
	}
}
public class TestLamp {
	public static void main(String[] args) {
		Light[] Is = new Light[3];
		Is[0] = new RedLight();
		Is[1] = new YellowLight();
		Is[2] = new GreenLight();
		Lamp lamp = new Lamp();
		for(int i = 0;i < Is.length ; i++) {
			lamp.setLight(Is[i]);
			lamp.on();
		}
	}
}

结果
在这里插入图片描述

Question_8-7

有输出如下代码结果:

interface JavaTeacher{
	void teach();
}
class TeacherA implements JavaTeacher{
	public void teach() {
		System.out.println("TeacherA teach Java");
	}
}
class TeacherB implements JavaTeacher{
	public void teach() {
		System.out.println("TeacherB teach Java");
	}
}
class School{
	public static JavaTeacher getTeacher(int i) {
		if(i == 0) {
			return new TeacherA();
		}else {
			return new TeacherB();
		}
	}
}
public class TestSchool {
	public static void main(String[] args) {
		JavaTeacher jt = School.getTeacher(0);
		jt.teach();
		jt = School.getTeacher(10);
		jt.teach();
	}
}

结果
在这里插入图片描述

Question_8-1

代码填空:

abstract class Animal{
	public abstract void eat();
}
interface Pet{
	void play();
}
class Dog extends Animal implements Pet{
	public void eat() {
		System.out.println("Dog eat Bones");
	}
	public void play() {
		System.out.println("play with Dog");
	}
}
class Cat extends Animal implements Pet{
	public void eat() {
		System.out.println("Cat eat fish");
	}
	public void play() {
		System.out.println("play with Cat");
	}
}
class Wolf extends Animal{
	public void eat() {
		System.out.println("Dog eat Bones");
	}
}
public class TestMain {
	public static void main(String[] args) {
		Animal as[] = new Animal[3];
		as[0] = new Dog();
		as[1] = new Cat();
		as[2] = new Wolf();
		//调用as数组中所有动物eat方法

		//调用as数组中所有宠物的play方法
		
	}
}

//1处填入:
for(int i = 0 ; i < as.length ; i++) {
as[i].eat();
}
//2处填入:
for(int i = 0 ; i < as.length ; i++) {
if(as[i] instanceof Pet) {
Pet pet = (Pet)as[i];
pet.play();
}
}

Question_8-9

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

public class Test17 {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SalariedEmployee s1 = new SalariedEmployee("Tom",2,3000.0);
		HourlyEmployee h1 = new HourlyEmployee("Bob",2,30.0,170);
		SalesEmployee s2 = new SalesEmployee("Jack",9,300000.0,0.09);
		BasePlusSalesEmployee b1 = new BasePlusSalesEmployee("Aim",11,500000.0,0.07,800);
		Employee[] employee = new Employee[4];
		employee[0] = s1;
		employee[1] = h1;
		employee[2] = s2;
		employee[3] = b1;
		//for(int i = 0; i < employee.length ; i++) {
			//System.out.println(employee[i].getSalary(9));
		//}
		double overpay = statisticsOfovertimepay(employee);
		System.out.println(overpay);
	}
	public static double statisticsOfovertimepay(Employee[] employee) {
		double overpay = 0;
		for(int i = 0; i < employee.length ; i++) {
			if(employee[i] instanceof OvertimePay) {
				OvertimePay op = (OvertimePay)employee[i];
				overpay = overpay + op.overtimePay(0);
			}
		}
		return overpay;	
		}

}
abstract class Employee{
	private String name;
	private int month;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getMonth() {
		return month;
	}
	public void setMonth(int month) {
		this.month = month;
	}
	public Employee() {}
	public abstract double getSalary(int mouth);
}

class SalariedEmployee extends Employee{
	private double salary;
	public double getSalary() {
		return salary;
	}
	public void setSalary(double salary) {
		this.salary = salary;
	}
	public SalariedEmployee() {}
	public SalariedEmployee(String name,int month,double salary) {
		super.setName(name); 
		super.setMonth(month);
		this.salary = salary;	
	}
	public double getSalary(int mouth) {
		double salary = this.salary;
		if(super.getMonth() == mouth) {
			salary = salary +100.0;
		}
		return salary;
	}
}
class HourlyEmployee extends Employee{
	private double salary;
	private int hour;
	public double getSalary() {
		return salary;
	}
	public void setSalary(double salary) {
		this.salary = salary;
	}
	public int getHour() {
		return hour;
	}
	public void setHour(int hour) {
		this.hour = hour;
	}
	public HourlyEmployee() {}
	public HourlyEmployee(String name,int month,double salary,int hour) {
		super.setName(name);
		super.setMonth(month);
		this.salary = salary;	
		this.hour = hour;
	}
	public double getSalary(int mouth) {
		double salary = this.salary;
		int hour = this.hour;
		if(hour > 160) {
			salary = (salary * 160) + (1.5 * salary * (hour - 160)); 
		}else {
			salary = (salary * hour);
		}
		if(super.getMonth() == mouth) {
			salary = salary +100.0;
		}
		return salary;
	}
}
class  SalesEmployee extends Employee implements OvertimePay{
	private double monthlySales;
	private double ReyaltyRate;
	public double getMonthlySales() {
		return monthlySales;
	}
	public void setMonthlySales(double monthlySales) {
		this.monthlySales = monthlySales;
	}
	public double getReyaltyRate() {
		return ReyaltyRate;
	}
	public void setReyaltyRate(double reyaltyRate) {
		ReyaltyRate = reyaltyRate;
	}
	public SalesEmployee() {}
	public SalesEmployee(String name,int month,double monthlySales,double ReyaltyRate) {
		super.setName(name);
		super.setMonth(month);
		this.monthlySales = monthlySales;
		this.ReyaltyRate = ReyaltyRate;
	}
	public double getSalary(int mouth) {
		double salary = this.monthlySales;
		double reyaltyRate = this.ReyaltyRate;
		salary = salary * reyaltyRate;
		if(super.getMonth() == mouth) {
			salary = salary +100.0;
		}
		salary = overtimePay(salary);
		return salary;
	}
	public double overtimePay(double money) {
		money = money + 2000;
		return money;
	}
}
class BasePlusSalesEmployee extends SalesEmployee implements OvertimePay{
	private double baseSalary;
	public double getBaseSalary() {
		return baseSalary;
	}
	public void setBaseSalary(double baseSalary) {
		this.baseSalary = baseSalary;
	}
	public BasePlusSalesEmployee() {}
	public BasePlusSalesEmployee(String name,int month,double monthlySales,double ReyaltyRate,double baseSalary) {
		super.setName(name);
		super.setMonth(month);
		super.setMonthlySales(monthlySales);
		super.setReyaltyRate(ReyaltyRate);
		this.baseSalary = baseSalary;
	}
	public double getSalary(int mouth) {
		double salary = super.getMonthlySales();
		double reyaltyRate = super.getReyaltyRate();
		salary = salary * reyaltyRate;
		if(super.getMonth() == mouth) {
			salary = salary +100.0;
		}
		salary = salary + this.baseSalary;
		salary = overtimePay(salary);
		return salary;
	}
	public double overtimePay(double money) {
		money = money + 1000;
		return money;
	}
}

interface OvertimePay{
	double overtimePay(double money);
}

Question_8-10

有以下代码:

abstract class AbstractService implements ServiceInterface{
	public void doService1() {}
	public void doService2() {}
	public void doService3() {}
}
class MyService implements ServiceInterface{
	public void doService1() {

	}
	public void doService2() {

	}
	public void doService3() {

	}
}

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

Question_8-11

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

public class TestMain {
	public static void main(String[] args) {
		int i = 14;
		if(i > 6 && i % 2 ==0) {
			int[] ai = factorization(i);
			for(int j = 0; j < ai.length ; j++) {
				System.out.println(ai[j]);
			}
		}else {
			System.out.println("请输入大于6的偶数");
		}
			
	}
	public static int[] factorization(int i) {
		int[] ai = new int[2];
		Function f = new Function();
		for(int j = 1 ; j < i ; j++) {
			if(f.factorization(j) && f.factorization(i - j)) {
				ai[0] = j;
				ai[1] = i - j;
			}
		}
		return ai;
	}
}
interface MathTool{
	boolean factorization(int i);
}
class Function implements MathTool{
	public boolean factorization(int i) {
		for(int j = 2 ; j < i ; j++) {
			if(i % j == 0) {
				return false;
			}
		}
		return true;
	}
}

如有错误请多多包涵

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值