Java中面向对象的知识点总结
一、对象和类的概念 类:对具有相同属性和方法的一类事物的抽象。 对象:具体的某一事物,代表自身的一些属性和方法。 二、类(对象)之间的关系 关联(组合、聚合),继承,依赖、实现 三、面向对象设计思想 面向对象---》考虑哪些类,对象---》类和对象有属性、方法-----》类和类之间的关系 四、重载、重写和隐藏 1). 重载(overload): 方法重载就是多个方法名称相同但是参数类型或者参数个数不同的方法,与返回值类型和修饰符无关 class Test { public int test(int a) { return a; } public String test(String a) { return a; } public float test(int a,String b) { return 0f; } public float test(String a,int b) { return 1.0f; } String test(float a) { return ""; } String test(int a) {//该方法不是重载 return ""; } } 前面五个互为重载,第一个和第六个虽然返回值类型不同,但参数相同,所以第一个和第六个参数不是重载 2). 重写(override): 子类继承父类时,子类的方法名称、参数类型、参数个数与父类完全相同,则认为子类重写了父类的方法。 方法重写规则:
3). 隐藏: 隐藏是针对于父类的成员变量和静态方法而言的。子类中声明了和父类相同的变量名或静态方法(方法名相同、参数列表相同、返回类型相同)则实现了对父类成员变量和静态方法的隐藏,下面举个例子有助理解: class A { static int a = 1; static int b = 2; int c = 33; public static void printA() { System.out.print(a); } public static void printB() { System.out.print(b); } } class B extends A { static int a = 3; static int b = 4; int c = 44; public static void printB() { System.out.print(b); } } public class Test { public static void main(String[] args) { B.printA(); B.printB(); System.out.print(B.a); A a = new B(); B b = new B(); a.printB(); b.printB(); System.out.print(a.c); System.out.print(b.c); } } 输出结果: 1 4 3 2 4 33 44 如果子类中有相同名称的静态方法或变量父类的会被隐藏,如果子类中存在同名的静态方法或变量,则会隐藏父类中得静态方法或变量,此时子类调用的就是子类中自己的静态方法或变量;如果子类中不存在同名的静态方法或变量,则会调用父类中的静态方法或变量;父类调用的始终是其本身的静态方法和变量。 五、封装: 封装是把对象的属性和操作结合为一个独立的整体,隐藏对象内部操作的实现,用户只需要通过其对外提供的方法来访问该对象,无需知道其内部实现细节。 优点:
六、继承: 继承是一个对象获取另一个对象属性的过程,关键字为extends和implements。 1). IS-A关系(一个对象所属于另一个对象): 方式一. 用extends来实现继承: public class Animal { public void eat() { System.out.println("Animal eating..."); } } public class Mammal extends Animal { public void eat() { System.out.println("Mammal eating..."); } } public class Dog extends Mammal { public void eat() { System.out.println("Dog eating..."); } } 方式二. 用implements来实现继承: public interface Animal { void eat(); } public class Mammal extends Animal { public void eat() { System.out.println("Mammal eating..."); } } public class Dog extends Mammal { public void eat() { System.out.println("Dog eating..."); } } 无论方式一还是方式二,我们都可以用instanceof关键字检查得出:Mammal是一个Animal(哺乳动物也是动物);Dog既是一个Mammal,也是一个Animal(狗既是哺乳动物也是动物)。 public class Test { /** * instanceof关键字检查代码 */ public static void main(String[] args) { Mammal m = new Mammal(); Dog d = new Dog(); System.out.print(m instanceof Animal); System.out.print(d instanceof Mammal); System.out.print(d instanceof Animal); } } 输出结果: true true true 2). HAS-A关系(一个对象含有另一个对象的一些属性): public class Car{} public class Speed{} public class Benz extends Car{ private Speed sp; } Benz含有Spend属性,但Benz不是Spend 七、多态: 实现多态的三个必要条件:继承、重写、父类引用指向子类对象。 1). 向上转型: 我们实例化一个Dog对象可以用 2). 举例说明: class A { public String show(D obj) { return ("A and D"); } public String show(A obj) { return ("A and A"); } } class B extends A { public String show(B obj) { return ("B and B"); } public String show(A obj) { return ("B and A"); } } class C extends B {} class D extends B {} class Test { public static void main(String[] args) { A a1 = new A(); A a2 = new B(); B b = new B(); C c = new C(); D d = new D(); System.out.println(a1.show(b)); // ① System.out.println(a1.show(c)); // ② System.out.println(a1.show(d)); // ③ System.out.println(a2.show(b)); // ④ System.out.println(a2.show(c)); // ⑤ System.out.println(a2.show(d)); // ⑥ System.out.println(b.show(b)); // ⑦ System.out.println(b.show(c)); // ⑧ System.out.println(b.show(d)); // ⑨ } } 输出结果: A and A // ① 前三个比较简单不容易出错,看看下面几个:
总结 以上就是关于Java面向对象知识点整理的全部内容了,希望本文的内容对大家学习或者使用java能带来一定的帮助,如果有疑问大家可以留言交流。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |