独立思想是最宝贵的

多写原创的文章
随笔 - 15, 文章 - 0, 评论 - 78, 阅读 - 49036
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

2018年2月7日

节选自:http://www.cnblogs.com/dolphin0520/p/3811445.html

1. 成员内部类是依附外部类而存在的,也就是说,如果要创建成员内部类的对象,前提是必须存在一个外部类的对象。创建成员内部类对象的一般方式如下

public class Test {
    public static void main(String[] args)  {
        //第一种方式:
        Outter outter = new Outter();
        Outter.Inner inner = outter.new Inner();  //必须通过Outter对象来创建
         
        //第二种方式:
        Outter.Inner inner1 = outter.getInnerInstance();
    }
}
 
class Outter {
    private Inner inner = null;
    public Outter() {
         
    }
     
    public Inner getInnerInstance() {
        if(inner == null)
            inner = new Inner();
        return inner;
    }
      
    class Inner {
        public Inner() {
             
        }
    }
}

2. 局部内部类和匿名内部类只能访问局部final变量

 

节选自:http://www.cnblogs.com/dolphin0520/p/3803432.html

下面这段代码的输出结果是什么?

 1 public class Test {
 2     public static void main(String[] args)  {
 3         Shape shape = new Circle();
 4         System.out.println(shape.name);
 5         shape.printType();
 6         shape.printName();
 7     }
 8 }
 9  
10 class Shape {
11     public String name = "shape";
12      
13     public Shape(){
14         System.out.println("shape constructor");
15     }
16      
17     public void printType() {
18         System.out.println("this is shape");
19     }
20      
21     public static void printName() {
22         System.out.println("shape");
23     }
24 }
25  
26 class Circle extends Shape {
27     public String name = "circle";
28      
29     public Circle() {
30         System.out.println("circle constructor");
31     }
32      
33     public void printType() {
34         System.out.println("this is circle");
35     }
36      
37     public static void printName() {
38         System.out.println("circle");
39     }
40 }
shape constructor
circle constructor
shape
this is circle
shape
View Code

覆盖只针对非静态方法(终态方法不能被继承,所以就存在覆盖一说了),而隐藏是针对成员变量和静态方法的。这2者之间的区别是:覆盖受RTTI(Runtime type  identification)约束的,而隐藏却不受该约束。也就是说只有覆盖方法才会进行动态绑定,而隐藏是不会发生动态绑定的。在Java中,除了static方法和final方法,其他所有的方法都是动态绑定。因此,就会出现上面的输出结果。

2.下面这段代码的输出结果是什么?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public class Test {
    public static void main(String[] args)  {
        Shape shape = new Circle();
        System.out.println(shape.name);
        shape.printType();
        shape.printName();
    }
}
 
class Shape {
    public String name = "shape";
     
    public Shape(){
        System.out.println("shape constructor");
    }
     
    public void printType() {
        System.out.println("this is shape");
    }
     
    public static void printName() {
        System.out.println("shape");
    }
}
 
class Circle extends Shape {
    public String name = "circle";
     
    public Circle() {
        System.out.println("circle constructor");
    }
     
    public void printType() {
        System.out.println("this is circle");
    }
     
    public static void printName() {
        System.out.println("circle");
    }
}
shape constructor
circle constructor
shape
this is circle
shape

  这道题主要考察了隐藏和覆盖的区别(当然也和多态相关,在后续博文中会继续讲到)。

  覆盖只针对非静态方法(终态方法不能被继承,所以就存在覆盖一说了),而隐藏是针对成员变量和静态方法的。这2者之间的区别是:覆盖受RTTI(Runtime type  identification)约束的,而隐藏却不受该约束。也就是说只有覆盖方法才会进行动态绑定,而隐藏是不会发生动态绑定的。在Java中,除了static方法和final方法,其他所有的方法都是动态绑定。因此,就会出现上面的输出结果。

posted @ 2018-02-07 11:39 漂流的鱼 阅读(176) 评论(0) 推荐(0) 编辑

2018年2月5日

摘要: 转载自:https://www.cnblogs.com/dolphin0520/p/3923167.html Java并发编程:Lock 在上一篇文章中我们讲到了如何使用关键字synchronized来实现同步访问。本文我们继续来探讨这个问题,从Java 5之后,在java.util.concurr 阅读全文

posted @ 2018-02-05 19:43 漂流的鱼 阅读(140) 评论(0) 推荐(0) 编辑

2018年1月10日

摘要: 字符集和编码的区别 ascII、unicode、gb2312等都是字符集,用于定义编号指代的字符。utf 8,utf 16则是unicode的编码格式。 ascII ascII只有128个,能表示英文、数字、常用符号。 gb2312 gb2312是中文特有的字符集,有2万多个字符,前128个和asc 阅读全文

posted @ 2018-01-10 15:14 漂流的鱼 阅读(4080) 评论(0) 推荐(2) 编辑

2017年12月21日

摘要: 下面是一位牛人写得关于数据库索引的精品之作,因为很好,不敢修饰,转载至此与博友共享。 原文链接:MySQL索引背后的数据结构及算法原理 本文以MySQL数据库为研究对象,讨论与数据库索引相关的一些话题。特别需要说明的是,MySQL支持诸多存储引擎,而各种存储引擎对索引的支持也各不相同,因此MySQL 阅读全文

posted @ 2017-12-21 17:02 漂流的鱼 阅读(166) 评论(0) 推荐(0) 编辑

2017年12月13日

摘要: 转载自:http://blog.csdn.net/quitepig/article/details/8041308 B树 即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和Right); 2.所有结点存储一个关键字; 3.非叶子结点的左指针指向小于其关键字的子树,右指针指向大于其关键字的 阅读全文

posted @ 2017-12-13 14:54 漂流的鱼 阅读(178) 评论(0) 推荐(0) 编辑

2014年6月24日

摘要: 一、安装nginx 1、在nginx官方网站下载一个包,下载地址是:http://nginx.org/en/download.html 2、WinSCP(ftp上传工具).exe FTP 上传工具上传达到CentOS中 3、我是先建一个目录,把要用的到的安装包放到一起. #mkdir /home/ ... 阅读全文

posted @ 2014-06-24 15:52 漂流的鱼 阅读(172) 评论(0) 推荐(0) 编辑

2014年6月20日

摘要: Dapper是近2年异军突起的新ORM工具,它有ado.net般的高性能又有反射映射实体的灵活性,非常适合喜欢原生sql的程序员使用,而且它源码很小,十分轻便。我写本博客的目的不是为了介绍Dapper,而是要将我使用Dapper迁移数据库过程中遇到的bug和一些优化介绍给大家,Dapper在多... 阅读全文

posted @ 2014-06-20 14:33 漂流的鱼 阅读(3822) 评论(10) 推荐(7) 编辑

2014年4月1日

摘要: Dapper的使用方法,记录下易忘的方法。 阅读全文

posted @ 2014-04-01 17:34 漂流的鱼 阅读(14328) 评论(2) 推荐(3) 编辑

2012年7月23日

摘要: 仿AspNetPager写的JS分页控件,适合ajax调用,较多的配置元素。 阅读全文

posted @ 2012-07-23 14:21 漂流的鱼 阅读(1362) 评论(1) 推荐(0) 编辑

2011年12月16日

摘要: 有一道买电影票的问题网上发布的答案都是错误的,误人子弟,却没有人指正出来,余甚感痛心。在此写出我的解法。 题目:有2n个人排队进电影院,票价是50美分。在这2n个人当中,其中n个人只有50美分,另外n个人有1美元(纸票子)。愚蠢的电影院开始卖票时1分钱也没有。问: 有多少种排队方法 使得 每当一个拥有1美元买票时,电影院都有50美分找钱注: 1美元=100美分拥有1美元的人,拥有的是纸币,没法破成2个50美分 网上的答案一般是:本题可用递归算法,但时间复杂度为2的n次方,也可以用动态规划法,时间复杂度为n的平方,实现起来相对要简单得多,但最方便的就是直接运用公式:排队的种数=(2n)!/[n. 阅读全文

posted @ 2011-12-16 11:35 漂流的鱼 阅读(2320) 评论(2) 推荐(1) 编辑

点击右上角即可分享
微信分享提示