卜若的代码笔记系列-Web系列-SpringBoot-第四章:面向接口之:理解接口-3203


#1:既然是面向接口,那么你肯定就要理解接口是什么啦,有什么作用啦。
我们肯定要对接口有一个了解,这张博客我找了很久,写得非常棒
https://blog.csdn.net/tangerr/article/details/74784298
总的来说,其实接口可以从“泛型”的角度来理解,或者使用“工厂模式”的角度来理解
我现在有一个接口:

    public interface IPlayer
    {
        
        public void bornMe();
        
    }


我现在有一个工厂

class Factory
{
    public void born(IPlayer player)
    {
        player.bornMe();
        
    }


}

ok,这样有什么好处呢?
我可以这样

public class superPlayer implements IPlayer
{
    public void bornMe()
    {
        System.out.println("我是超级玩家");
    
    }


}
public class normalPalyer implements IPlayer
{
    public void bornMe()
    {
    System.out.println("我是一般玩家");
    
    }


}

这两个是实现类,同时,记住这句话很重要:

它也是工厂里面born的参数,你可以直接用


实现类作为参数,从而优化了整个代码结构!

    

2018年9月24日15:15 2/7

猜你喜欢

转载自blog.csdn.net/qq_37080133/article/details/82829350
今日推荐