• JAVA中的类


    节选自: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方法,其他所有的方法都是动态绑定。因此,就会出现上面的输出结果。

  • 相关阅读:
    UVa-272-TEX Quotes
    UVa-10881-蚂蚁
    UVa-1339-古老的密码
    POJ-1328-放置雷达
    POJ-3190-分配畜栏
    Openjudge-2787-算24
    WHYZOJ-#47. 滑行的窗口(单调队列)
    2017年9月16日18:03:54
    WHYZOJ-#93. 暗黑破坏神(垃圾01背包)
    WHYZOJ-#95 大逃亡(二分+BFS)(好题!!!)
  • 原文地址:https://www.cnblogs.com/liuliqiang/p/8425875.html
Copyright © 2020-2023  润新知