- Object 类
- 内部类及匿名内部类()
- 所有的类都必须继承自己Object类
- 1、 复写equals()方法
- 2、 复写toString()方法所有类都必须继承自Object类
- 类之源------是最大的类,是所有类的子类,则一切对象都可以向Oebject类进行转换
- ToString(): 在对象打印时默认调用,但是在默认情况下调用的是Object类的中的toString ().v如果一个类希望按自己的方式打印对象,则可以通过覆写的方式,将toString()方法覆写掉。
- //Object类是所有类的父类,如果一个类没有明确声明继承自那个类,则肯定会继承Ojbect类
- class Person
- {
- public String toString()
- {
- return "Hello World!!";
- }
- }
- public class javaoo06
- {
- public static void main(String []args)
- {
- Person p = new Person();
- } System.out.println(p); //默认情况下打印对象地址,实际上就相当于调用对象中的toString方法
- System.out.println(p.toString());
- }
- Person@c17164
- Person@c17164
- class Person
- {
- private String name;
- private int age;
- public Person(String name,int age)
- {
- this.name = name;
- this.age = age;
- }
- public String toString()
- {
- return "姓名:" + this.name + ", 年龄:" + this.age;
- }
- }
- public class javaoo06
- {
- public static void main(String []args)
- {
- Person p = new Person("HYJ",22);
- System.out.println(p);
- }
- }
- 姓名:HYJ, 年龄:22
- Equals方法是一个对象比较,在String 类中强调用的equals方法肯定是已经被String 类所覆写了。
- class Person
- {
- private String name;
- private int age;
- public Person(String name,int age)
- {
- this.name = name;
- this.age = age;
- }
- public String toString()
- {
- return "姓名:" + this.name + ", 年龄:" + this.age;
- }
- }
- public class javaoo06
- {
- public static void main(String []args)
- {
- Person p1 = new Person("HYJ",22);
- Person p2 = new Person("HYJ",22);
- System.out.println(p1.equals(p2)); //false
- }
- }
- class Person
- {
- private String name;
- private int age;
- public Person(String name,int age)
- {
- this.name = name;
- this.age = age;
- }
- public boolean equals(Object obj) // 传的是对象
- {
- if (!(obj instanceof Person)) //System.out.println(p1.equals("abc"));
- {
- return false;
- }
- Person per1 = this;
- Person per2 = (Person)obj;
- boolean flag = false;
- if (per1 == per2)
- {
- flag = true;
- }
- else
- {
- if (per1.name.equals(per2.name)&&per1.age == per2.age)
- {
- flag = true;
- }
- }
- return flag;
- }
- public String toString()
- {
- return "姓名:" + this.name + ", 年龄:" + this.age;
- }
- }
- public class javaoo06
- {
- public static void main(String []agrs)
- {
- Person p1 = new Person("HYJ",22);
- // Person p2 = new Person("HYJ",22);
- Person p2 = p1;
- //System.out.println(p1.equals(p2));
- System.out.println(p1.equals("abc"));
- }
- }
- 内部类:
- 1、 内部类的定义:同部类的使用
- 2、 内部在如何被外部调用?
- //class 外部类
- //{
- // class 内部类
- // {
- // }
- //}
- class Outer
- {
- private String info = "LXH -- MLDN";
- //内部类
- class Inner //定义
- {
- public void print()
- {
- System.out.println("INFO = " + info);
- }
- }
- public void fun() //方法
- {
- new Inner().print();
- }
- }
- public class javaoo06
- {
- public static void main(String []args)
- {
- Outer p = new Outer();
- p.fun();
- }
- }
- class Outer
- {
- private String info = "HYJ--MLDN";
- public String getInfo()
- {
- return this.info;
- }
- public void setInfo(String info)
- {
- this.info = info;
- }
- //内部类
- public void fun()
- {
- new Inner(this).print();
- }
- }
- class Inner
- {
- private Outer out = null;
- public Inner(Outer out)
- {
- this.out = out;
- }
- public void print()
- {
- System.out.println("INFO = " + out.getInfo());
- }
- }
- public class javaoo06
- {
- public static void main(String []args)
- {
- Outer p = new Outer();
- p.fun();
- }
- }
- 内部类确实可以节省代码,但是造成类的结构混乱。内部内本身也会产生*.class文件
- class Outer
- {
- private String info = "LXH---MLDN";
- //内部类
- class Inner
- {
- public void print()
- {
- System.out.println("INFO = " + info);
- }
- }
- }
- public class javaoo06
- {
- public static void main(String []args)
- {
- Outer p = new Outer();
- Outer.Inner in = p.new Inner();
- in.print();
- }
- }
- 实际上,内部类可以在任何位置定义,例如方法中
- 在方法中定义的内部类如果要访问方法中的变量,则变量前必须加final,但是此final并不代表说是一个常量。只是一个简单的标记而已。
- class Outer
- {
- private String info = "LXH---MLDN";
- public void fun(final int len)
- {
- class Inner
- {
- public void print()
- {
- System.out.println("len = " + len);
- System.out.println("INFO = " + info);
- }
- }
- new Inner().print();
- }
- }
- public class javaoo06
- {
- public static void main(String []args)
- {
- new Outer().fun(10);
- }
- }
- 匿名内部类,这个内部类只使用一次,一次之后就不用了,匿名内部类是在抽象类和接口的基础上发展起来的。
- interface A
- {
- public void fun();
- }
- class B implements A
- {
- public void fun()
- {
- System.out.println("MLDN ---LXH");
- }
- }
- class C
- {
- public void fun2()
- {
- this.print(new B());
- }
- public void print(A a)
- {
- a.fun();
- }
- }
- public class javaoo06
- {
- public static void main(String []args)
- {
- new C().fun2();
- }
- }
- ###################################
- interface A
- {
- public void fun();
- }
- class C
- {
- public void fun2()
- {
- this.print(new A()
- {
- public void fun()
- {
- System.out.println("MLDN---LXH");
- }
- }
- );
- }
- public void print(A a)
- {
- a.fun();
- }
- }
- public class javaoo06
- {
- public static void main(String []args)
- {
- new C().fun2();
- }
- }