java基础之--object

来源:互联网 发布:python调用谷歌浏览器 编辑:程序博客网 时间:2024/05/18 09:09

object类是所有对象的直接或间接父类,传说为上帝。包含方法 Object()
该类中定义的肯定是所有对象都具备的功能。
equals(Object obj):java认为所有对象都具有可比较性。
getClass()
Hashmap()
toString()

内部类的访问规则:
1、内部类可以直接访问外问类中的成员,包括私有。之所以可以直接访问外部类中的成员,是因为内部类中持有一个外部类的引用。格式 外类类名.this.
2、外部类要访问内部类,必须建立内部类对象。
访问格式:
1、当内部类定义在外部类的成员位置成员位置上,而且非私有,可以在外部其他类中。可以直接建立内部类对象。
外部类名.内部类名 变量名 = 外部类对象.内部类对象
Outer.Inner in = new Outer().new Inner();
2、当内部类在成员位置上,就可以被成员修饰符所修饰
例如:
private:将内部类在外部类中进行封装。
static:内部类就具备static的特性。当内部类被static修饰后,只能访问外部类中的static成员。出现了访问局限。

那么在外部类其它类中,如何直接访问内部类的非静态成员呢?
new Outer.Inner().function();
那么在外部类其它类中,如何直接访问内部类的静态成员呢?
Outer.Inner.function();
注意:当内部类中定义了静态成员,该内部类必须是static的。
当外部类中的静态方法访问内部类时,那内部类也必须是静态的。

什么时候定义内部类:
当描述事物时,事物的内部还有事物,该事物用内部类描述。因为内部事务在使用外部事物的内容。

class Outer{    private int x = 3;    class Inner    {        int x = 4;        void function()        {            int x = 5;            System.out.println("inner is"+x);            System.out.println("inner1 is"+this.x);            System.out.println("inner2 is"+Outer.this.x);        }    }    void method()    {        Inner in = new Inner();        in.function();    }}public class OuterInner {    public static void main(String[] args) {        // TODO Auto-generated method stub        Outer out = new Outer();        out.method();        Outer.Inner in = new Outer().new Inner();        in.function();    }}
0 0
原创粉丝点击