首页 > Web开发 > 详细

.net和JAVA面向对象,继承有趣的细节

时间:2014-03-01 11:35:33      阅读:336      评论:0      收藏:0      [点我收藏+]

原型是同事间讨论的一道面试题。估计这题秒杀了不少人,LZ也被秒了。

但这个题里隐藏了一个很有趣的细节,这个细节不说清楚,不少人会其实死的冤枉。

这是C#的代码。

bubuko.com,布布扣
 class Program
    {
        static void Main(string[] args)
        {
            Father s1 =new Son1();
            Son1 s2 =new Son1();
            s1.Say();
            s2.Say();
            Console.Read();
        }
    }

    public  class Father
    {
        public void Say()
        {
            Console.WriteLine("This is Father‘s method");
        }
    }

    public class Son1 : Father
    {
        public void Say()
        {
            Console.WriteLine("This is Son1‘s method");
        }
    }
bubuko.com,布布扣

执行结果是 This is Father‘s method

               This is Son1‘s method

不知大家有没有答对,LZ想也没想直觉就是

This is Son1‘s method

This is Son1‘s method

因为之前看过JAVA的内容。

这是原贴:http://blog.csdn.net/zhengzhb/article/details/7496949

仔细想了下.NET算是想通了。但这事就有趣了。

因为JAVA的结果和.NET完全两样。(LZ学设计模式什么的都是照着JAVA学的,之前一直感觉也相信着两者在面向对象方面是一个模子)。

这是JAVA的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Father {
    public void Say(){
        System.out.println("This is Father‘s method");
    }
}
 
class Son1 extends Father{
    public void Say(){
        System.out.println("This is Son1‘s method");
    }
}
 
public class FF {
    public static void main(String[] args){
        Father s1 = new Son1();
        Son1 s2=new Son1();
        s1.Say();
        s2.Say();
    }
}

结果是

This is Son1‘s method

This is Son1‘s method

 

C#看的相对深入点的是《clr via c#》,再底层些粗粗浏览了《windows核心编程》

但JAVA却不知道有哪些书籍,能讲到对象的堆分配,方法调用的细节。

借此文,求JAVA的答案,求书。

.net和JAVA面向对象,继承有趣的细节,布布扣,bubuko.com

.net和JAVA面向对象,继承有趣的细节

原文:http://www.cnblogs.com/zihunqingxin/p/3574377.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!