Java:学习总结

  • 为什么有构造器的存在?
    一直以来,都构造器的设计初衷都感到不解,为什么设计Java时,有构造器这样的存在。一般在写c的时候,通常,我们对一个模块初始化的时候,都会时候一个函数初始化这个模块。所以Java直接引入构造器的概念来完成这个工作。

  • 枚举类型
    枚举类型的关键字是enum,实际上,和int、char等基本数据类型一样,也可以将enum创建的类看做是一种数据类型。数据的值有enum的成员决定。如:
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());
        }

  • 代理(Proxy)
    有时候,我们希望给用户的接口屏蔽底层实现;这时候可以使用代理。如:
    我们有一个飞船的控制模块Control:
/**
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 得到的。
总之,代理就是:把直接的实现变为间接的实现。


  • @Override注解
    如果想在子类中覆写一个方法而不是重载,使用@Override注解。
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());
    }
}

  • 向上转型(upcasting)
    在子类中用到父类的类型时,子类向父类转型。如:
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); 
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容