#在千锋“逆战”学习第18天#Question8、接口 、用接口验证哥德巴赫猜想

总结

接口多态
多种类型的不同引用指向同一个对象是,表示看待对象的视角不同
不同类型所能看到对象的范围不同,只能调用自身类型中所声明的部分

类与类是单继承关系

类与接口是多实现关系,一个类可实现多个接口

接口与接口多继承关系 一个接口可继承多个接口

常量接口
将多个常用于表示状态或固定值的变量,以静态常量的形式定义在传统接口中统一管理,提高代码可读性。

接口回调原理
在这里插入图片描述
接口的好处
程序的耦合度降低
更自然的使用多态
设计与实现完全分离
更容易搭建程序框架
更容易更换具体实现

-----------------------------------------------------我是作业分割线

interface IA{ void ma();

}

interface IB extends IA{ void mb();

}

interface IC{ void mc();

}

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

I.如果有一个类 ClassE 实现 ID 接口,如果不希望 ClassE 是抽象的,则需要实现哪些方法?

答案:需要实现public ma();public mb();public mc();public md(); 接口可以多继承,类中要覆盖所有方法。

II. 把下面的代码补充完整

public class TestClassE{

public static void main(String args[]){ 

IC ic = new ClassE();

//调用 ma 方法

______________

//调用 mb 方法

______________

//调用 mc 方法

______________

//调用 md 方法

______________

}

}

答案:
(ClassE)ic.ma();
(ClassE)ic.mb();
ic.mc();
(ClassE)ic.md();

III. 写出下面代码的输出结果

public class TestClassE{

public static void main(String args[]){ 
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);

}

}

答案:
true
true
true
true
true

interface IA{ void ma();

}

interface IB{ void mb();

}

class MySuper implements IA{ public void ma(){}

}

class MySub extends MySuper implements IB{
 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);

}

}

问:该程序输出结果是什么?
true
true
true
true

5.关于接口和抽象类,下列说法正确的是:

A. 抽象类可以有构造方法,接口没有构造方法

B. 抽象类可以有属性,接口没有属性

C. 抽象类可以有非抽象方法,接口中都是抽象方法

D. 抽象类和接口都不能创建对象

E. 一个类最多可以继承一个抽象类,但是可以实现多个接口

答案:ACDE
解释下A ,抽象类和接口都不能new对象,接口明确没有构造方法,静态代码块,动态代码块。但是抽象类可以有构造方法,虽然不能创建对象,但是可以用来初始化抽象类中的共性属性,或被其他子类调用,且抽象类会被继承,子类构造函数中默认第一行是super();,调用父类无参构造方法。所以即是我们不写构造方法,抽象类也会默认提供一个无参构造方法,否则系统编译不会通过。

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[] ls = new Light[3];

ls[0] = new RedLight();

ls[1] = new YellowLight();

ls[2] = new GreenLight();

Lamp lamp = new Lamp();

for (int i = 0; i<ls.length; i++){

lamp.setLight(ls[i]);

lamp.on();

}

}

}

答案:
Red Light shine in Red
Yellow Light shine in Yellow
Green Light shine in Green

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(); 

}

}

答案:
TeacherA teach Java
TeacherB teach Java

8.代码填空

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(“Wolf eat meat”);

}

}

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 方法

//1

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

//2

}

}

//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) as[i]).play();
}
}
Animal中没有paly方法,所以需要强转为子类对象再调用paly方法,并且要去确定类型转换是否异常,因为wolf类没有pet接口。

9.在原有的 Chap6 中的 17 题基础之上修改代码

公司给 SalariedEmployee 每月另外发放 2000 元加班费,给 BasePlusSalesEmployee 发放 1000 元加班费改写原有代码,加入以上的逻辑。并写一个方法,打印出本月公司总共发放了多少加班费。

package com.qf.day17.t1;





public class TestClassE {
	
	
	public static void main(String[] args){
		
		
		
		
		
		Employee[] s = new Employee[5];
		s[0] = new SalarideEmployee("zhangsan",3,5000.0);
		s[1] = new HourlyEmployee("lisi",2,200);
		s[2] = new SalesEmployee("wangwu",6,30000);
		s[3] = new BasePlusSalesEmployee("zhouliu",8,5000,35000);
		s[4] = new SalesEmployee("zhaoqi",3,40000);
		
		for(int i = 0; i <s.length;i++){
			
				System.out.println(s[i].getName()+"的2月份工资为"+s[i].getSalary());
		
		}
		OverTimeMoney.Money();
	
	
	
	
	}
	
	
	
	
}
class Employee{
	private String name;
	private int month;
	public Employee(){}
	public Employee(String name,int month){
		this.name = name;
		this.month = month;
	}
	public String getName(){
		return this.name;
	}
	
	public double getSalary(){
		
		int thismonth = 2;
		
		if(thismonth == this.month){
			return 100.0;
		}else{
			return 0.0;
		}
	}
}

class OverTimeMoney{
	public static void Money(){
		System.out.println("本月发放的加班费共计"+(SalarideEmployee.count+BasePlusSalesEmployee.count));
	}
}
class SalarideEmployee extends Employee implements OverTimePay{
	public static double count;
	private double monthSalary;
	public SalarideEmployee(){}
	
	public SalarideEmployee(String name,int month,double monthSalary){
		super(name,month);
		this.monthSalary = monthSalary;
	}


	public double getSalary(){
		getOverTimePay();
		return this.monthSalary+super.getSalary();
	}


	public void getOverTimePay() {
		this.monthSalary += 2000.0;
		SalarideEmployee.count+=2000.0;
		
	}
}

class HourlyEmployee extends Employee{
	private int hours;
	private double hourSalary=20;
	
	public HourlyEmployee(){}
	
	public HourlyEmployee(String name,int month,int hours){
		super(name,month);
		this.hours = hours;
	}
	public double getSalary(){
		if(hours <= 160){
			return hours*this.hourSalary+super.getSalary();
		}else{
			return 160*this.hourSalary+(hours-160)*this.hourSalary*1.5+super.getSalary();
		}
		
	}
}

class SalesEmployee extends Employee{
	private double monthSales;
	private double royaltyRate = 0.2;
	
	public SalesEmployee(){}
	public SalesEmployee(String name,int month,double monthSales){
		super(name,month);
		this.monthSales = monthSales;
	}
	public double getSalary(){
		return monthSales*royaltyRate+super.getSalary();
	}

	public void setMonthSales(double monthSales) {
		this.monthSales = monthSales;
	}
}
class BasePlusSalesEmployee extends SalesEmployee implements OverTimePay{
	public static double count = 0;
	private double basePlus;
	public BasePlusSalesEmployee(){}
	public BasePlusSalesEmployee(String name,int month,double basePlus,double monthSales){
		super(name,month,monthSales);
	
		this.basePlus = basePlus;

	}
	public void getOverTimePay(){
		this.basePlus +=1000.0;
		BasePlusSalesEmployee.count+=1000.0;
	}
	public double getSalary(){
		getOverTimePay();
		return basePlus+super.getSalary();
	}
	
}


interface OverTimePay{
	public abstract void getOverTimePay();
}

在这里插入图片描述
10.有下列代码:

interface ServiceInterface{

void doService1(); 
void doService2(); 
void doService3();

}

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

public void doService2(){}
public void doService3(){}

}

需要一个实现 ServiceInterface 接口的类 MyService。

I.第一种方式可以让 MyService 实现 ServiceInterface 接口,即: class MyService implements ServiceInterface

AI.第二种方式可以让 MyService 继承 AbstractService 类,即:

class MyService extends AbstractService

请问:这两种方式有什么区别?AbstractService 类有什么作用?

答:第一种方式是MyService类直接实现接口,第二种方式是通过继承父类,实现父类中的方法来间接的实现接口。
AbstractService类作为父类去实现MyService接口的方法,实现后的方法是会被子类继承过来的。

11.验证歌德巴赫猜想

输入一个大于 6 的偶数,请输出这个偶数能被分解为哪两个质数的和。

如 10=3+7 12=5+7

要求:两个人一组合作完成。一个人负责把一个整数 n 拆分成两个整数的和,另一个人负责写一个函数,判断某一个整数 a 是否是质数 。

package com.qf.day18.t2;
import java.util.Scanner;
public class Test11 {
	public static void main(String[] args){
		int a;
		
	while(true){
		
		System.out.println("请输入一个大于6的偶数");
		Scanner input = new Scanner(System.in);
		a = input.nextInt();
		
		if(a > 6 && a % 2 ==0){
			
			break;
			
		}else{
			System.out.println("输入有误,请重新输入");
		}
	}
		MathTool mt = new MathToollmpl();
		for(int i = 2;i < a/2;i++){
			if(mt.isPrime(i)&&mt.isPrime(a-i)){
				System.out.println(a+"="+i+"+"+(a-i));
			}
		}
	}
}

interface MathTool{
	boolean isPrime(int i );
	
}
class MathToollmpl implements MathTool{
	public boolean isPrime(int i){
		for(int n = 2;n <= i/2;n++){
			if(i%n == 0){
				return false;
			}
			
		}
		return true;
	}
}
发布了7 篇原创文章 · 获赞 1 · 访问量 138

猜你喜欢

转载自blog.csdn.net/llpiong/article/details/104514912
今日推荐