Interface接口
每个人生阶段都需要努力去扮好自己的角色,越努力越轻松,越坚强越幸运!
加油!
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 是抽象的,则需要实现哪些方法?
void md();
void ma();
void mb();
void mc();
2).把下面的代码补充完整
public class TestClassE{
public static void main(String args[]){
IC ic = new ClassE();
//调用 ma 方法______________
//调用 mb 方法______________
//调用 mc 方法______________
//调用 md 方法______________
}
}
ClassE.ma();
ClassE.mb();
ic.mc();
ClassE.md();
3)写出下面代码的输出结果
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.有如下代码:
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.写出下面代码运行结果
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();l
s[2] = new GreenLight();
Lamp lamp = new Lamp();
for (int i = 0; i<ls.length; i++){
lamp.setLight(ls[i]);l
amp.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{pu
blic 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 处应该填入的代码为_______________。
//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 p=(Pet)as[i];
p.play();
}
}
9.在原有的 Chap6 中的 17 题基础之上修改代码
公司给 SalariedEmployee 每月另外发放 2000 元加班费,给 BasePlusSalesEmployee 发放 1000 元加班费改写原有代码,加入以上的逻辑。并写一个方法,打印出本月公司总共发放了多少加班费。
interface OvertimePay {
double salesEmployee = 2000.0;
double basePlusSalesEmployee = 1000;
}
class Employee{
private String name;
private int month;
public Employee() {
}
public Employee(String name,int month) {
this.name=name;
this.month=month;
}
public double getSalary(int month) {
if(this.month==month)
return 100;
return 0.0;
}
class SalarieEmployee extends Employee{
private double salary;
public SalarieEmployee() {
}
public SalarieEmployee(String name,int month,double salary) {
super(name,month);
this.salary = salary;
}
public double getSalary(int month) {
return salary+super.getSalary(month);
}
}
class HourlyEmployee extends Employee{
private double hourlySalary;
private int hours;
public HourlyEmployee() {
}
public HourlyEmployee(String name,int month,double hourlySalary, int hours) {
super(name,month);
this.hourlySalary = hourlySalary;
this.hours = hours;
}
public double getSalary(int month) {
if (hours > 160){
return hourlySalary * 160 + 1.5 * hourlySalary * (hours - 160) + super.getSalary(month);
}
return hourlySalary * hours + super.getSalary(month);
}
}
class SalesEmployee extends Employee implements OvertimePay{
private double sales;
private double rate;
public static int value;
public SalesEmployee() {
value++;
}
public SalesEmployee(String name,int month,double sales, double rate) {
super(name,month);
this.sales = sales;
this.rate = rate;
value++;
}
public double getSalary(int month) {
return sales * rate + super.getSalary(month)+salesEmployee;
}
}
class BasePlusSalesEmployee extends SalesEmployee{
private double baseSalary;
public static int nums=0;
public BasePlusSalesEmployee() {
nums++;
}
public BasePlusSalesEmployee(String name,int month,double sales, double rate, double baseSalary) {
super(name,month,sales, rate);
this.baseSalary = baseSalary;
nums++;
}
public double getSalary(int month) {
return baseSalary + super.getSalary(month)+basePlusSalesEmployee;
}
}
public class Test17 {
public static void main(String[] args) {
Employee[] ee = new Employee[] {
new SalarieEmployee("Zh", 3, 100000),
new HourlyEmployee("Jon", 4, 170, 20),
new SalesEmployee("Mary", 8, 20000, 0.45),
new BasePlusSalesEmployee("Tom", 11, 30000, 0.45 , 5000) };
for(int i=0;i<ee.length;i++) {
System.out.println(ee[i].getSalary(4));
}
printOvaetimeSalary();
}
public static void printOvaetimeSalary(){
double sums = (double)SalesEmployee.value * 2000 +(double)BasePlusSalesEmployee.nums * 1000;
System.out.println("加班费总和为:" + sums);
}
}
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。
第一种方式可以让 MyService 实现 ServiceInterface 接口,
即: class MyService implements ServiceInterface
第二种方式可以让 MyService 继承 AbstractService 类,
即:class MyService extends AbstractService
请问:这两种方式有什么区别?AbstractService 类有什么作用?
1.实现接口的必须实现其中抽象方法
2.继承类的话不需要在实现方法,可以重写
11.验证歌德巴赫猜想
输入一个大于 6 的偶数,请输出这个偶数能被分解为哪两个质数的和。
如 10=3+7 12=5+7
要求:两个人一组合作完成。一个人负责把一个整数 n 拆分成两个整数的和,另一个人负责写一个函数,判断某一个整数 a 是否是质数 。
public class GoldbachConjecture {
public static void main(String[] args) {
/* 自己定义的类,因为方法goldBach和ifPrime 不是static修饰的,需要用对象引用调用方法 */
GoldbachConjecture Practice=new GoldbachConjecture();
Scanner scanner=new Scanner(System.in);
System.out.println("输入一个大于6的偶数:");
int n=scanner.nextInt();
Practice.goldBach(n);
}
void goldBach(int n){
//首先判断传入的数n是否是大于6的偶数
if (n%2==0&&n>6) {
//循环次数控制在n/2内
for (int i = 2; i <= n/2; i++) {
//如果质数1和质数2同时成立,则找到猜想成立的质数
if (ifPrime(i)&&ifPrime(n-i)) {
System.out.println("数为: " +i + " 和" + (n-i) );
}
}
}
else {
System.out.println("输入错误!");
}
}
//判断数是否是质数
boolean ifPrime(int num){
//从2到偶数even的前一位,只要没有因数,就是质数
for (int i = 2; i <num; i++) {
if (num%i==0) return false;
}
//质数返回真
return true;
}
}