Java:学习总结

2017-10-22  本文已影响0人  安公子_

enum Chars{
    A , B , C
}

这个枚举类型Chars表明;Chars只有3个可取数据。就相当于int型的数据只有整数一样。
使用数据的时候;

Chars a = Chars.A;
Chars b = Chars.B;
Chars c = Chars.C;

基本使用方法

    public static void foo6(){
        Chars a= Chars.A;
        // default case ; toString() method to use for all enum type
        // for example the following
        System.out.println(a);
        //others method; the usage of vaules() and ordinal() as follow 
        for (Chars i : Chars.values()) {
            System.out.println(i+" ordinal is " + i.ordinal());
        }

/**
how to use Proxy
*/

// this is a spaceship control module
class Controls{
    public void up(int velocity){}
    public void down(int velocity){}
    public void right(int velocity){}
    public void left(int velocity){}
    public void forward(int velocity){}
    public void back(int velocity){}
}

我们可以采用继承的方式实现一个飞船。

// a way to construct a spaceship is interheit

class SpaceShip{
    private String name;

    SpaceShip(String name){
        this.name = name;
    }

    public static void main(String[] args) {
        // this way can construct a spaceship through new keywords to create a object
        // but it will expose the implementation details of based control module
        SpaceShip spaceship = new SpaceShip("Racketer");
    }
}

但是这种实现方式有一个问题,其中的spaceship 对象将会暴露控制模块的实现,也就是会得到Controls的所有成员变量和成员方法。这是不希望的。用户模块SpaceShip可以得到Controls的任何的动作。
为了解决这个问题,使用下面的代理模式:

// otherwise way is apply Proxy to spaceship,
// it can hide the implementation of based control module

class SpaceshipProxy{
    private String name;
    // create a control module
    private Controls control = new Controls();

    SpaceshipProxy(String name){
        this.name = name;
    }

    public void up(int velocity){
        control.up(velocity);
    }
    public void down(int velocity){
        control.down(velocity);
    }
    public void right(int velocity){
        control.right(velocity);
    }
    public void left(int velocity){
        control.left(velocity);
    }
    public void forward(int velocity){
        control.forward(velocity);
    }
    public void back(int velocity){
        control.back(velocity);
    }

    public static void main(String[] args) {
        SpaceshipProxy protector = new SpaceshipProxy("protector");
        protector.back(100);
    }
}

可以看出,我可以得到Controls的所有动作,但是至于底层实现,却被屏蔽了,用户protector对象不能直接得到Controls的动作,而是通过SpaceshipProxy 得到的。
总之,代理就是:把直接的实现变为间接的实现。


class Homer{
    public char doh(char c){
        System.out.println("Homer----doh(char)");
        return 'd';
    } 
    public float doh(float f){
        System.out.println("Homer----doh(float)");
        return 1.0f;
    }
}

class M{}

class Sar extends Homer{

    public void doh(M m){
        System.out.println("Bar----doh");
    }
}

class Bar extends Sar{
    // when you want to over ride a method but not overload ; using annotation @Override
    @Override
    public void doh(M m){
        System.out.println("Sar----doh");
    }
}

public class Hide{
    public static void main(String[] args) {
        Bar bar  = new Bar();
        bar.doh(1.0f);
        bar.doh('a');
        bar.doh(new M());
    }
}

class Instrument{
    public void play(){}
    public static void turn(Instrument i){
        System.out.println("Instrument---based class");
        i.play();
        System.out.println("Instrument---i.play()");
    }
}

// 当参数需要父类的时候,子类要转换成父类
public class Wind extends Instrument{
    public static void main(String[] args) {
        Wind w = new Wind();
        // upcasting
        Instrument.turn(w); 
    }
}
上一篇 下一篇

猜你喜欢

热点阅读