接口(练习题)

Java学习思维导图
1.代码改错:

package com.qf.q8.t1;
interface IA{
	void m1();
	int a = 100;
}
class MyClass implements IA{
	void m1() {}//	public void m1() {}
}
public class TestInterface {
	public static void main(String[] args) {
		IA ia = new MyClass();
		ia.m1();
		System.out.println(IA.a);
	}
}

2.代码填空:

package com.qf.q8.t2;
interface IA{
	void m1();
	void m2();
}
//1/*abstract*/ class MyClassA implements IA {
	public void m1() {}
}
class MyClassB extends MyClassA{
	//2  public void m2() {}
}

3.有如下代码:

package com.qf.q8.t3;

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是抽象的,则需要实现哪些方法?
package com.qf.q8.t3;

public class ClassE implements ID{
	public void md() {
		
	}
	public void ma() {
		
	}
	public void mb() {
		
	}
	public void mc() {
		
	}
}
II.把下面的代码补充完整
package com.qf.q8.t3;

public class TestClassE {
	public static void main(String[] args) {
		IC ic = new ClassE();
		//调用ma方法
		(IA)ic.ma();
		//调用mb方法
		(IB)ic.mb();
		//调用mc方法
		ic.mc();
		//调用md方法
		(ID)ic.md();
	}
}
III.写出下面代码的输出结果
package com.qf.q8.t3;

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

4.有如下代码:

package com.qf.q8.t4;
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

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

package com.qf.q8.t6;
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();
		}
	}
}
Red Light shine in Red
Yellow Light shine in Yellow
Green Light shine in Green

7.写出下面代码执行的结果

package com.qf.q8.t7;
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.代码填空:

package com.qf.q8.t8;


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
		for(int i = 0 ;i<as.length;i++) {
			as[i].eat();
		}
		//调用as数组中所有宠物的play方法
		//2
		for(int i = 0 ;i<as.length;i++) {
			if(as[i]instanceof Pet) {
				Pet pet = (Pet)as[i];
				pet.play();
			}
		}
		
	}
}

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

公司给SalariedEmployee每月另外发放2000元加班费,给BasePlusEmployee发放1000元加班费
改为原有代码,加上以上的逻辑。并写一个方法,打印出本月公司总共发放了多少加班费。
package com.qf.q8.t9;

public class TestEmplyee {
	public static void main(String[] args) {
		Employee[] e =new Employee[4];
		e[0] = new SalariedEmployee("john",5,5000);
		e[1] = new HourlyEmployee("tom",10,25,170);
		e[2] = new SalesEmployee("kunyan",7,200000,0.03);
		e[3] = new BasePlusSalesEmployee("kunyan",8,1000000,0.02,5000);
		double sum=0;
		for(int i=0;i<e.length;i++) {
			if(e[i] instanceof Overtime) {
				Overtime ot = (Overtime)e[i];
				sum+=ot.getOvertimePay();
			}
			
			System.out.println(e[i].getName()+"工资为"+e[i].getSalary(5));
		}
		   System.out.println("本月公司总共发放加班费"+sum);
	}
}
interface Overtime{
	double getOvertimePay();
}
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(String name, int month) {
			this.name = name;
			this.month = month;
		}
		public double getSalary(int month) {
			if(this.month==month) return 100;
			else return 0;
		}
}
class SalariedEmployee extends Employee implements Overtime{
	private double salary;
	public SalariedEmployee(String name,int month,double salary) {
		super(name,month);
		this.salary = salary;
	}
	public void setSalary(double salary) {
		this.salary = salary;
	}

	public double getSalary(int month) {
		return salary+super.getSalary(month);
	}
	public double getOvertimePay() {
		return 2000;
	}
}
class HourlyEmployee extends Employee{
	private int hours;
	private double hourlySalary;
	public HourlyEmployee(String name,int month,int hours,double hourlySalary) {
		super(name,month);
		this.hours=hours;
		this.hourlySalary = hourlySalary;
	}
	public double getSalary(int month) {
		if(getHours()>160) {
			return 160*this.hourlySalary+(this.hourlySalary-160)*1.5*this.hourlySalary;
		}else {
			return  this.hourlySalary*this.hours+super.getSalary(month);
		}
	}
	public int getHours() {
		return hours;
	}
	public void setHours(int hours) {
		this.hours = hours;
	}
	public double getHourlySalary() {
		return hourlySalary;
	}
	public void setHourlySalary(double hourlySalary) {
		this.hourlySalary = hourlySalary;
	}
}

class SalesEmployee extends Employee{
	private double sales;
	private double rate;
	public SalesEmployee(String name,int month,double sales,double rate) {
		super(name,month);
		this.sales=sales;
		this.rate = rate;
	}
	public double getOvertimePay() {
		return 0;
	}
	public double getSalary(int month) {
		return this.sales*this.rate+super.getSalary(month);
	}
	
	public double getSales() {
		return sales;
	}
	public void setSales(double sales) {
		this.sales = sales;
	}
	public double getRate() {
		return rate;
	}
	public void setRate(double rate) {
		this.rate = rate;
	}
}

class BasePlusSalesEmployee extends SalesEmployee implements Overtime{
	private double baseSalary;
	public BasePlusSalesEmployee(String name, int month, double sales, double rate,double baseSalary) {
		super(name, month, sales, rate);
		this.baseSalary = baseSalary;
	}
	public double getSalary(int month) {
		return this.baseSalary+super.getSalary(month);
	}
	public double getOvertimePay() {
		return 1000;
	}
}

10.有下列代码:

package com.qf.q8.t10;

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
class MyService implements ServiceInterface{
	public void doService1() {}
	public void doService2() {}
	public void doService3(){}
}
II.第二种方式可以让MyService继承AbstractService类,即:class MyService extends AstractService
class MyService extends AbstractService{
	public void doService1() {}
	public void doService2() {}
	public void doService3() {}
}
请问:这两种方式有什么区别?AbstractService类有什么作用?
实现接口的必须实现接口中的抽象方法
继承类不需要实现所有的方法,可以重写。

11.验证哥德巴赫猜想

输入一个大于6的偶数,请输出这个偶数能被分解为哪两个质数的和。
如10=3+7 12=5+7
要求:两个人一组合作完成。一个人负责把一个整数n拆分成两个整数的和,另一个人负责写一个函数,判断某一个整数a是否是质数。
package com.qf.q8.t11;

import java.util.Scanner;

interface MathTool{
	boolean isPrime(int n);
}
class MathToolImpl implements MathTool{
	public boolean isPrime(int n) {
		for(int i=2;i<=Math.sqrt(n);i++) {
			if(n%i==0) return false;
	}
		return true;
	}
}
	public class Gedebahe {
	public static void main(String[] args) {
		System.out.println("请输入一个大于6的偶数:");
		int n = new Scanner(System.in).nextInt();
		if(n%2!=0 || n<=6) {
			System.out.println("parameter error!");
		}
		MathTool mt = new MathToolImpl();
		for(int i=2;i<=n/2;i++) {
				if(mt.isPrime(i)&&mt.isPrime(n-i)) {
						System.out.print(n+"="+i+"+"+(n-i));
						System.out.println();
			}
		}
	}
	}
发布了14 篇原创文章 · 获赞 20 · 访问量 2361

猜你喜欢

转载自blog.csdn.net/weixin_44255829/article/details/104503076