Java基础知识记录(三)

  1. String
    1. 字符串具有不可变的特性. 字符串被创建后会保存在常量池中. 每次新定义一个新的字符串或者修改一个字符串时, 并不是修改常量池中的值,而是在常量池中新创建一个字符串,将地址值给引用的变量.
    2. StringBufferd支持并发操作,线性安全的,适 合多线程中使用。StringBuilder不支持并发操作,线性不安全的,不适合多线程中使用。
  2. 无意识的递归
    public class Demo {

    @Override
    public String toString() {
        // 重写了父类的toString()方法. 在调用该类的toString时, 编译到+ this时,
        // 由于this是一个对象,所以程序会自动调用this的toString方法. 由此,形成了一个无限循环调用
        // 形成了堆栈溢出. 因此,如果想在重写的子类中获得该类的地址值.可以调用super.toString方法
        return "abc :" + this;
    }
}
  1. String的格式化输出
    1. format方法. 可以打印,可以String.format对字符串进行格式化然后形成一个需要的字符串数据
    2. 参考链接: http://blog.csdn.net/lonely_fireworks/article/details/7962171/
public class FormatString {
    public static void main(String[] args) {
        int i = 10;
        float y = 3.234f;
        System.out.println("Row 1:["+i+" "+y+"]");
        // 格式化后
        System.out.format("Row 1:[%d %f]", i,y);
        // String.format(format, args)
    }
}
  1. 关于正则匹配器的使用,如下
    public static void main(String[] args) {
        /**
         * 正则匹配器,用于数据中检索目标数据
         */
        String argus = "abcdbcabcegreabc";
        String reg = "(abc)+";
        Pattern compile = Pattern.compile(reg);
        Matcher matcher = compile.matcher(argus);
        while (matcher.find()) {
            System.out.println("内容是:" +matcher.group());
            System.out.println("开始index:"+matcher.start());
            System.out.println("结束index:"+(matcher.end()-1));
        }
    }
参考链接: http://blog.csdn.net/cclovett/article/details/12448843/
  1. 代理模式
/**
 * 代理模式
 * 是我记性不好嘛... 感觉跟适配器一个套路
 */
interface Interface {
    void doSomeThing();
    void doSomeThingElse();
}
class RealObject implements Interface{
    public void doSomeThing() {
        System.out.println("doSomeThing");
    }
    public void doSomeThingElse() {
        System.out.println("doSomeThingElse");
    }
}
class SimpleProxy implements Interface {
    private Interface obj;
    public SimpleProxy(Interface obj) {
        this.obj = obj;
    }
    @Override
    public void doSomeThing() {
        System.out.println("新加的需求");
        obj.doSomeThing();
    }
    @Override
    public void doSomeThingElse() {
        System.out.println("新加的需求");
        obj.doSomeThingElse();
    }
}
public class SimpleProxyDemo {
    // 消费者
    public static void consumer() {
        Interface obj = new RealObject();
        obj.doSomeThing();
        obj.doSomeThingElse();
        System.out.println("代理对象");
        Interface proObj = new SimpleProxy(obj);
        proObj.doSomeThing();
        proObj.doSomeThingElse();
    }
    public static void main(String[] args) {
        consumer();
    }
}
  1. 动态代理
/**
 * 动态代理处理程序
 */
public class SimpleDynamicProxy implements InvocationHandler{
    private Object Object;
    public SimpleDynamicProxy(java.lang.Object object) {
        Object = object;
    }
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        // proxy 既是代理对象
        // method 调用的方法
        // args 传递的方法参数
        System.out.println("动态代理执行--start:"+proxy.getClass().getSimpleName()+" 方法:"+method.getName()
        +" 参数:"+args);
        return method.invoke(Object, args); //调用底层方法进行动态代理
    }
}
/**
 * 动态代理使用类
 */
public class SimpleDynamicUseClass {
    public static void consumer(Interface obj) {
        obj.doSomeThing();
        obj.doSomeThingElse();
    }
    public static void main(String[] args) {
        consumer(new RealObject());
        System.out.println("----------------------");
        // 使用代理对象创建一个代理实例
        // 第一个参数. 该类实现的接口的类的加载器
        // 第二个参数. 该被代理类的所有接口实现
        // 第三个参数. 该代理类的代理类实现
        Interface proxy = (Interface) Proxy.newProxyInstance(Interface.class.getClassLoader(),
                RealObject.class.getInterfaces(), new SimpleDynamicProxy(new RealObject()));
        consumer(proxy);
    }
}
  1. 运行时类型信息相关 , 可参考链接 : http://blog.csdn.net/u013905744/article/details/52496611
  2. 空对象模式 , 可参考链接 : http://blog.csdn.net/xiaokang123456kao/article/details/69676798
  3. 泛型擦除 参考链接 : http://blog.csdn.net/lonelyroamer/article/details/7868820
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值