千峯逆戰班,Day18

在千鋒逆戰班學習的第18天。

沒有不進步的人生,只有不進取的人 !

今天我學習了JAVA課程的  部分關於接口的知識。

中國加油!!!武漢加油!!!千鋒加油!!!我自己加油!!!

 

 

總結:

接口:

                接口是一種標準

                將多個常用於表示狀態和固定值的變量,以形態常量的形式定義在接口中統一管理,提高代碼的可讀性。

                模塊與模塊之間的關聯程度越鬆散,耦合越低,越好。

 

 

習題:

C8.3  有如下代碼:

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是抽象的,則需要實現哪些方法?

                                ma,mb,mc,md

                II.   把下面的代碼補充完整:

public class TestClassE{
    public static void main(String[] args){
        IC ic=new ClassE();
        //調用 ma 方法
        _____________  
        //調用 mb 方法
        _____________  
        //調用 mc 方法
        _____________  
        //調用 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

 

C8.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

 

C8.5  關於接口和抽象類,下列說法正確的是:ACDE

               A. 抽象類可以有構造方法,接口沒有構造方法

               B. 抽象類可以有屬性,接口沒有屬性

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

               D. 抽象類和接口都不能創建對象

               E. 一個類最多可以繼承一個抽象類,但是可以實現多個接口

 

C8.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 TestLemp{
    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<ls.length:i++){
            lamp.setLight(Is[i]);
            lamp.on();
        }
    }
}

               Red Light shine in Red

               Yellow Light shine in Yellow

               Green Light shine in Green

 

C8.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

       本題的重點在於利用接口,把多態用在返回值類型上。getTeacher方法返回的對象是JavaTeacher接口類型,在getTeacher方法內部創建JavaTeacher實現類的對象,並作爲返回值返回。

 

C8.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 方法
        for(int i=0;i<as.length;i++){
            as[i].eat();
        }

        //調用 as 數組中所有動物的 play 方法
        for(int i=0;i<as.length-1;i++){
            if(as[i] instanceof Pet){
                Pet p=(Pet)as[i];
                p.play;
            }
        }
    }
}

 

 

C8.9  在原有的C6.17基礎之上修改代碼:

package tset17;

public class TestEmployee {

	public static void main(String[] args) {

		Employee[] e=new Employee[4];
		
		e[0]=new SalariedEmployee("普通員工",1,4000);
		e[1]=new HourlyEmployee("小時工",2,100,161);
		e[2]=new SalesEmployee("提成銷售",3,40000,0.12);
		e[3]=new BasePlusSalesEmployee("底薪銷售",4,1000,0.09,2000);
		
		System.out.println(e[3].getBaseSalary());
		
		for(int i=0;i<e.length;i++){
			System.out.println(e[i].getName()+"工資爲:"+e[i].getSalary(1));
		}
		//========================
		double sum=0;
		for(int i=0;i<e.length;i++){
			if(e[i] instanceof Overtime){
				Overtime o=(Overtime) e[i];
				sum+=o.getOverTimeSalary();
			}
		}
		System.out.println("總加班費爲:"+sum);
	}
}

package tset17;

public interface Overtime {

	double getOverTimeSalary();
}
package tset17;

class SalariedEmployee extends Employee implements Overtime{
	private double salary;
	
	public SalariedEmployee(String name,int birthday,double salary){
		super(name,birthday);
		this.salary=salary;
	}
	
	public double getSalary(int month){
		double money=salary+super.getSalary(month);
		return money;
	}

	public double getOverTimeSalary(){
		return 2000;
	}
}
package tset17;

class BasePlusSalesEmployee extends SalesEmployee implements Overtime{
	private double baseSalary;
	
	public BasePlusSalesEmployee(String name, int birthday, double monthlySales, double royalth,double baseSalary) {
		super(name, birthday, monthlySales, royalth);
		this.baseSalary=baseSalary;
	}

	
	public double getSalay(int month){
		double money=0;
		money=super.getSalary(month)+this.baseSalary;
		return money;
	}
	
	public double getOverTimeSalary(){
		return 2000;
	}
	
	public double getBaseSalary(){
		return baseSalary;
	}
}

 

C8.10  有下列代碼:

interface ServiceInterface{
    void doService1();
    void doService2();
    void doService3();
}

abstract class AbstractService implements ServiceInterface{
    public void duService1(){}
    public void duService2(){}
    public void duService3(){}
}

               需要一個實現 ServiceInterface 接口的類 MyService

               I.     第一種方式可以讓 MyService 實現 ServiceInterface 接口,即:

                     class MyService implements ServiceInterface

               II.    第二種方式可以讓 MyService 繼承 AbstractService 類,即:

                     class MyService extends AbstractService

               這兩種方式的區別是:II是從父類繼承的接口,AbstractService起到承接的作用

 

C8.11  驗證哥德巴赫猜想:

               輸入一個大於6的偶數,輸出這個偶數能被分解爲哪兩個質數的和。

package TestGoldbach;

interface MathTool {

	public boolean isPrime(int n);
}
package TestGoldbach;
//接口的實現
public class Impl implements MathTool{

	public boolean isPrime(int n){
		for(int i=2;i<n;i++){
			if(n%i==0){
				return false;
			}
		}
		return true;
	}
}
package TestGoldbach;
//接口的使用
import java.util.Scanner;

public class TestSplit {
	public static void main(String[] args) {

		Scanner input=new Scanner(System.in);
		System.out.println("請輸入大於6的偶數n:");
		int n=input.nextInt();
		
		MathTool t=new Impl();
		
		if(n>6 && n%2==0){
			for(int i=2;i<n;i++){
				if( t.isPrime(i)==true && t.isPrime(n-i)==true ){
					System.out.println(n+"="+i+"+"+(n-i));
				}
			}
		}else{
			System.out.println("輸入有誤");
		}
	}
}

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章