1. 多态

  • 多态polymorhic概述

    • 事物存在的多种形态。
  • 多态前提
    • 要有继承关系
    • 要有方法重写
    • 要有父类引用指向子类对象
  • 案例演示
    • 代码体现多态
      1. class Demo1_Polymorphic{
      2. public static void main(String[] args) {
      3. Cat c = new Cat();
      4. c.eat();
      5.  
      6. Animal a = new Cat(); // 父类引用指向子类对象
      7. a.eat(); // 输出“猫吃鱼”
      8. }
      9. }
      10.  
      11. class Animal {
      12. public void eat() {
      13. System.out.println("动物吃饭");
      14. }
      15. }
      16.  
      17. class Cat extends Animal {
      18. public void eat() {
      19. System.out.println("猫吃鱼");
      20. }
      21. }
  • 多态中的成员访问特点

    • 成员变量

      • 编译看左边(父类),运行看左边(父类)
      • Father f = new Son();
        1. class Demo2_Polymorphic {
        2. public static void main(String[] args) {
        3. Father f = new Son();
        4. System.out.println(f.num); //输出10
        5. }
        6. }
        7.  
        8. class Father {
        9. int num = 10;
        10.  
        11. }
        12.  
        13. class Son extends Father {
        14. int num = 20;
        15. }

        如果再上面例子的基础上,再生成一个Son类对象,则

        1. class Demo2_Polymorphic {
        2. public static void main(String[] args) {
        3. Father f = new Son();
        4. System.out.println(f.num); //输出10

        5. Son s = new Son();
        6. System.out.println(s.num); //输出20
        7. }
        8. }
        9.  
        10. class Father {
        11. int num = 10;
        12.  
        13. }
        14.  
        15. class Son extends Father {
        16. int num = 20;
        17. }

    • 成员方法
      • 编译看左边(父类),运行看右边(子类)——又称“动态绑定”
      • Father f = new Son();
      • 编译时,看父类中有没有该成员方法,有则编译成功;运行则动态绑定到子类的成员方法上,运行子类的成员方法。
        1. class Demo3_Polymorphic {
        2. public static void main(String[] args) {
        3. Father f = new Son();
        4. f.print();// 输出son
        5. }
        6. }
        7.  
        8. class Father {
        9. int num = 10;
        10.  
        11. public void print() {
        12. System.out.println("father");
        13. }
        14.  
        15. }
        16.  
        17. class Son extends Father {
        18. int num = 20;
        19.  
        20. public void print() {
        21. System.out.println("son");
        22. }
        23. }

    • 静态方法
      • 编译看左边(父类),运行看左边(父类)
      • 静态和类相关,算不上重写,所以,访问还是左边的
        1. class Demo4_Polymorphic {
        2. public static void main(String[] args) {
        3. Father f = new Son();
        4. f.method(); //输出"father static method";
        5. // 相当于是Father.method(); 调用父类的方法
        6. }
        7. }
        8.  
        9. class Father {
        10. int num = 10;
        11.  
        12. public void print() {
        13. System.out.println("father");
        14. }
        15.  
        16. public static void method(){
        17. System.out.println("father static method");
        18. }
        19.  
        20. }
        21.  
        22. class Son extends Father {
        23. int num = 20;
        24.  
        25. public void print() {
        26. System.out.println("son");
        27. }
        28.  
        29. public static void method(){
        30. System.out.println("son static method");
        31. }
        32. }
    • 总结:
      • 只有非静态的成员方法,编译看左边(父类),运行看右边(子类)
      • 其他都是编译看左边(父类),运行看左边(父类)
  • 案例:超人的故事

    • 通过该案例帮助理解多态的现象。
      1. class Demo5_Polymorphic {
      2. public static void main(String[] args) {
      3. Person p = new SuperMan(); // 父类引用指向子类对象,超人提升为人
      4.  
      5. System.out.println(p.name);// 输出“John”
      6. p.谈生意(); // 输出"谈几个亿的大单子"
      7.  
      8. //p.fly(); //编译出错
      9.  
      10. }
      11. }
      12.  
      13. class Person {
      14. String name = "John";
      15.  
      16. public void 谈生意() {
      17. System.out.println("谈生意");
      18. }
      19. }
      20.  
      21. class SuperMan extends Person {
      22. String name = "Super man";
      23.  
      24. public void 谈生意() {
      25. System.out.println("谈几个亿的大单子");
      26. }
      27.  
      28. public void fly() {
      29. System.out.println("飞出去救人");
      30. }
      31. }

超人作为人类身份的属性信息对外展示,Person类和SuperMan类中重写的方法,实际执行的是超人身份的方法;

在这个例子中,父类中没有fly方法,故编译失败,也无法执行。针对这个,要考虑下面的 向上转型和向下转型 的问题。

  • 多态中 向上转型和向下转型

    • 向上转型

      • Person p = new SuperMan();
      • 父类引用指向子类对象,就是向上转型
    • 向下转型
      • SuperMan sm = (SuperMan)p;
        1. class Demo5_Polymorphic {
        2. public static void main(String[] args) {
        3. Person p = new SuperMan(); // 父类引用指向子类对象,超人提升为人
        4.  
        5. System.out.println(p.name);// 输出“John”
        6. p.谈生意(); // 输出"谈几个亿的大单子"
        7.  
        8. //p.fly(); //编译出错
        9.  
        10. SuperMan sm = (SuperMan)p; // 向下转型
        11. sm.fly();
        12.  
        13. }
        14. }
        15.  
        16. class Person {
        17. String name = "John";
        18.  
        19. public void 谈生意() {
        20. System.out.println("谈生意");
        21. }
        22. }
        23.  
        24. class SuperMan extends Person {
        25. String name = "Super man";
        26.  
        27. public void 谈生意() {
        28. System.out.println("谈几个亿的大单子");
        29. }
        30.  
        31. public void fly() {
        32. System.out.println("飞出去救人");
        33. }
        34. }

  • 补充参考:基本数据类型:

    • 自动类型提升
    • 强制类型转换
      1. class Demo1_Polymorphic{
      2. public static void main(String[] args) {
      3. int i = 10;
      4. byte b = 20;
      5. i = b; // 自动类型转换
      6. b = (byte)i; // 强制类型转换
      7.  
      8. }
      9. }
  • 多态的好处和弊端

    • 多态的好处

      • 提高了代码的维护性(继承保证)
      • 提高了代码的扩展性(由多态保证)
        • 可以当做形式参数
        • 可以接收任意子类对象
    • 多态的弊端
      • 不能使用子类的特有属性和行为
    • 案例演示
      • method(Animal a)  方便Animal的所有子类的 重写的方法调用
      • method(Cat c)  这种直接调用子类,出现并行子类,调用父类方法,需要些新的method方法,传入不同的类作为实参。
        1. class Demo6_Polymorphic {
        2. public static void main(String[] args) {
        3.  
        4. method(new Cat());
        5. method(new Dog());
        6. /*
        7. Animal a = new Cat();
        8. Animal a = new Dog();
        9.  
        10. 开发的时候很少在创建对象的时候用父类引用指向子类对象,
        11. 直接创建子类对象更方便,可以使用子类中的特有属性和方法
        12.  
        13. */
        14.  
        15. }
        16.  
        17. /*
        18. public static void method(Cat c) {
        19. c.eat();
        20. }
        21.  
        22. public static void method(Dog d) {
        23. d.eat();
        24. }*/
        25.  
        26. public static void method(Animal a) { // 当做参数的时候,用多态最好,扩展性强
        27. //关键字 instanceof 判断前边的引用是否是后边的数据类型
        28. if (a instanceof Cat) {
        29. Cat c = (Cat)a;
        30. c.eat();
        31. c.catchMouse();
        32. }else if (a instanceof Dog) {
        33. Dog d = (Dog)a;
        34. d.eat();
        35. d.lookHome();
        36. }else {
        37. a.eat();
        38. }
        39.  
        40. /*
        41. // 如果把Dog强转成Cat,就会出现类型转换异常,ClassCastException
        42. Cat c = (Cat)a;
        43. c.eat();
        44. c.catchMouse();*/
        45. }
        46. }
        47.  
        48. class Animal {
        49. public void eat() {
        50. System.out.println("动物吃饭");
        51. }
        52. }
        53.  
        54. class Cat extends Animal {
        55. public void eat() {
        56. System.out.println("猫吃鱼");
        57. }
        58.  
        59. public void catchMouse() {
        60. System.out.println("抓老鼠");
        61. }
        62. }
        63.  
        64. class Dog extends Animal {
        65. public void eat() {
        66. System.out.println("狗吃肉");
        67. }
        68.  
        69. public void lookHome() {
        70. System.out.println("看家");
        71. }
        72. }

2. 抽象类

  • 抽象类概述

    • 抽象就是看不懂的
  • 抽象类的特点
    • 抽象类和抽象方法必须用abstract关键字修饰

      • abstract class 类名 ()
      • public abstract void eat();
    • 抽象类不一定有抽象方法,有抽象方法的类一定是抽象类或者接口
    • 抽象类不能实例化,那么,抽象类如何实例化呢?
      • 按照多态的方式,由具体的子类实例化。其实这也是多态的一种,抽象类多态。
    • 抽象类的子类
      • 要么是抽象类
      • 要么重写抽象类中的所有抽象方法
    • 案例演示
        1. class Demo1_Abstract {
        2. public static void main(String[] args) {
        3.  
        4. Animal a = new Cat();
        5. a.eat();
        6. print(a);
        7. }
        8.  
        9. public static void print(Animal a) {
        10. a.eat();
        11. }
        12. }
        13.  
        14. abstract class Animal { // 抽象类
        15. public abstract void eat(); // 抽象方法
        16. }
        17.  
        18. class Cat extends Animal {
        19.  
        20. public void eat() {
        21. System.out.println("猫吃鱼");
        22. }
        23.  
        24. }
        1. class Demo1_Abstract {
        2. public static void main(String[] args) {
        3.  
        4. }
        5.  
        6. public static void print(Animal a) {
        7. a.eat();
        8. }
        9. }
        10.  
        11. abstract class Animal { // 抽象类
        12. public abstract void eat(); // 抽象方法
        13. }
        14.  
        15. class Cat extends Animal {
        16.  
        17. //错误: Cat不是抽象的, 并且未覆盖Animal中的抽象方法eat()
        18.  
        19. }
    • 抽象类联系(猫狗,抽象类为动物)
      • 具体事物:猫,狗
        共性:姓名,年龄,吃饭
        猫的特性:抓老鼠
        狗的特性:看家

        1. class Test1_Animal {
        2. public static void main(String[] args) {
        3. Cat c = new Cat("加菲",8);
        4. System.out.println(c.getName() + "..." + c.getAge());
        5. c.eat();
        6. c.catchMouse();
        7.  
        8. Dog d = new Dog("八公",30);
        9. System.out.println(d.getName() + "..." + d.getAge());
        10. d.eat();
        11. d.lookHome();
        12.  
        13. }
        14. }
        15.  
        16. /*
        17. 案例演示:
        18. 具体事物:猫,狗
        19. 共性:姓名,年龄,吃饭
        20. 猫的特性:抓老鼠
        21. 狗的特性:看家
        22. */
        23.  
        24. abstract class Animal {
        25. private String name;
        26. private int age;
        27.  
        28. public Animal(){}
        29.  
        30. public Animal(String name,int age) {
        31. this.name = name;
        32. this.age = age;
        33. }
        34.  
        35. public void setName(String name) { // 设置姓名
        36. this.name = name;
        37. }
        38.  
        39. public String getName() { // 获取姓名
        40. return name;
        41. }
        42.  
        43. public void setAge(int age) { // 设置年龄
        44. this.age = age;
        45. }
        46.  
        47. public int getAge() { // 获取年龄
        48. return age;
        49. }
        50.  
        51. public abstract void eat(); // 抽象方法:吃饭
        52. }
        53.  
        54. class Cat extends Animal {
        55. public Cat(){}
        56.  
        57. public Cat(String name,int age) {
        58. super(name,age);
        59. }
        60.  
        61. public void eat() {
        62. System.out.println("猫吃鱼");
        63. }
        64.  
        65. public void catchMouse() {
        66. System.out.println("抓老鼠");
        67. }
        68. }
        69.  
        70. class Dog extends Animal {
        71. public Dog(){}
        72.  
        73. public Dog(String name,int age) {
        74. super(name,age);
        75. }
        76.  
        77. public void eat() {
        78. System.out.println("狗吃肉");
        79. }
        80.  
        81. public void lookHome() {
        82. System.out.println("抓老鼠");
        83. }
        84. }
        1. class Test2_Teacher {
        2. public static void main(String[] args) {
        3. Teacher t = new BaseTeacher();
        4. print(t);
        5.  
        6. print(new EmpTeacher());
        7.  
        8. }
        9.  
        10. public static void print(Teacher t) {
        11. t.teach();
        12. }
        13. }
        14.  
        15. /*
        16. 具体事物:基础班老师,就业班老师
        17. 共性:姓名、年龄、讲课
        18. */
        19.  
        20. abstract class Teacher {
        21. private String name;
        22. private int age;
        23.  
        24. public abstract void teach();
        25.  
        26. public Teacher() {}
        27.  
        28. public Teacher(String name, int age) {
        29. this.name = name;
        30. this.age = age;
        31. }
        32.  
        33. public void setName(String name) {
        34. this.name = name;
        35. }
        36.  
        37. public String getName() {
        38. return name;
        39. }
        40.  
        41. public void setAge(int age) {
        42. this.age = age;
        43. }
        44.  
        45. public int getAge() {
        46. return age;
        47. }
        48.  
        49. }
        50.  
        51. class BaseTeacher extends Teacher {
        52. public BaseTeacher() {}
        53.  
        54. public BaseTeacher(String name, int age) {
        55. super(name, age);
        56. }
        57.  
        58. public void teach() {
        59. System.out.println("基础班老师 讲课");
        60. }
        61.  
        62. }
        63.  
        64. class EmpTeacher extends Teacher {
        65. public EmpTeacher() {}
        66.  
        67. public EmpTeacher(String name, int age) {
        68. super(name, age);
        69. }
        70.  
        71. public void teach() {
        72. System.out.println("就业班老师 讲课");
        73. }
        74.  
        75. }

        老师抽象类

        1. class Test3_Student {
        2. public static void main(String[] args) {
        3. state(new BaseStudent());
        4. state(new EmpStudent());
        5. }
        6.  
        7. public static void state(Student s) {
        8. s.study();
        9. }
        10. }
        11.  
        12. /*
        13. 具体事物:基础班学生,就业班学生
        14. 共性:姓名,年龄,学习
        15. */
        16.  
        17. abstract class Student {
        18. private String name;
        19. private int age;
        20.  
        21. public abstract void study();
        22.  
        23. public Student() {}
        24.  
        25. public Student(String name, int age) {
        26. this.name = name;
        27. this.age = age;
        28. }
        29.  
        30. public void setName(String name) {
        31. this.name = name;
        32. }
        33.  
        34. public String getNamge() {
        35. return name;
        36. }
        37.  
        38. public void setAge(int age) {
        39. this.age = age;
        40. }
        41.  
        42. public int getAge() {
        43. return age;
        44. }
        45.  
        46. }
        47.  
        48. class BaseStudent extends Student {
        49. public BaseStudent() {}
        50.  
        51. public BaseStudent(String name, int age) {
        52. super(name,age);
        53. }
        54.  
        55. public void study() {
        56. System.out.println("基础班学生在学习");
        57. }
        58. }
        59.  
        60. class EmpStudent extends Student {
        61. public EmpStudent() {}
        62.  
        63. public EmpStudent(String name, int age) {
        64. super(name,age);
        65. }
        66.  
        67. public void study() {
        68. System.out.println("就业班学生在学习");
        69. }
        70. }

        学生抽象类

        1. class Test4_Employee {
        2. public static void main(String[] args) {
        3. Coder c = new Coder("John", "001", 10000.00);
        4. c.work();
        5.  
        6. Manager m = new Manager("Ann", "033", 40000.00, 10000);
        7. m.work();
        8.  
        9. }
        10. }
        11.  
        12. /*
        13. 假如我们在开发一个系统时,需要对程序员类进行设计。
        14. 程序员包含3个属性:
        15. 姓名
        16. 工号
        17. 工资
        18.  
        19. 经理:出了含有程序员的属性外,还有一个奖金属性
        20.  
        21. 用继承的思想设计出程序员类和经理类。
        22. 要求类中提供必要的方法进行属性访问。
        23.  
        24. */
        25.  
        26. abstract class Employee {
        27. private String name;
        28. private String code;
        29. private double salary;
        30.  
        31. public Employee() {}
        32.  
        33. public Employee(String name, String code, double salary) {
        34. this.name = name;
        35. this.code = code;
        36. this.salary = salary;
        37. }
        38.  
        39. public void setName(String name) {
        40. this.name = name;
        41. }
        42.  
        43. public String getName(){
        44. return name;
        45. }
        46.  
        47. public void setCode(String code) {
        48. this.code = code;
        49. }
        50.  
        51. public String getCode(){
        52. return code;
        53. }
        54.  
        55. public void setSalary(double salary) {
        56. this.salary = salary;
        57. }
        58.  
        59. public double getSalary(){
        60. return salary;
        61. }
        62.  
        63. public abstract void work();
        64.  
        65. }
        66.  
        67. class Manager extends Employee {
        68. private int bonus;
        69.  
        70. public Manager() {}
        71.  
        72. public Manager(String name, String code, double salary, int bonus) {
        73. super(name,code,salary);
        74. this.bonus = bonus;
        75. }
        76.  
        77. public void setBonus(int bonus) {
        78. this.bonus = bonus;
        79. }
        80.  
        81. public int getBonus(){
        82. return bonus;
        83. }
        84.  
        85. public void work() {
        86. System.out.println("项目经理" + this.getName() + "在工作");
        87. System.out.println("奖金是" + this.getBonus());
        88. }
        89.  
        90. }
        91.  
        92. class Coder extends Employee {
        93. public Coder() {}
        94.  
        95. public Coder(String name, String code, double salary) {
        96. super(name,code,salary);
        97. }
        98.  
        99. public void work() {
        100. System.out.println("程序员" + this.getName() + "在工作");
        101. }
        102.  
        103. }

        雇员:程序员,项目经理

  • 抽象类的注意问题
    • 一个抽象类如果没有抽象方法,可不可以定义为抽象类?如果可以,有什么意义?

      • 可以。
        目的只有一个,就是不让其他类创建本类对象,交给子类完成
    • abstract不能和哪些关键字共存?

      • abstract 和 static
        被abstract修饰的方法没有方法体;
        被static修饰的可以用 类名.调用,但是类名.调用 抽象方法是没有意义的。

      • abstract 和 final 
        被abstract修饰的方法强制子类重写;
        被final修饰的方法,不让子类重写。这两个修饰词是矛盾的。

      • abstract 和 private
        被abstract修饰的是为了让子类看到并强制重写
        被private修饰的不让子类访问,所以他两是矛盾的。

        1. class Demo4_Abstract {
        2. public static void main(String[] args) {
        3. System.out.println("Hello World!");
        4. }
        5. }
        6.  
        7. /*
        8. 面试题1:
        9. 一个抽象类如果没有抽象方法,可不可以定义为抽象类?如果可以,有什么意义?
        10. 答:
        11. 可以。
        12. 目的只有一个,就是不让其他类创建本类对象,交给子类完成
        13.  
        14. 面试题2:
        15. abstract不能和哪些关键字共存?
        16. 答:
        17. abstract 和 static
        18. 被abstract修饰的方法没有方法体;
        19. 被static修饰的可以用 类名.调用,但是类名.调用 抽象方法是没有意义的。
        20.  
        21. abstract 和 final
        22. 被abstract修饰的方法强制子类重写;
        23. 被final修饰的方法,不让子类重写。这两个修饰词是矛盾的。
        24.  
        25. abstract 和 private
        26. 被abstract修饰的是为了让子类看到并强制重写
        27. 被private修饰的不让子类访问,所以他两是矛盾的。
        28.  
        29. */
        30.  
        31. abstract class Demo {
        32. // public static abstract void print();
        33. // Demo4_Abstract.java:21: 错误: 非法的修饰符组合: abstract和static
        34.  
        35. // public final abstract void print();
        36. // Demo4_Abstract.java:29: 错误: 非法的修饰符组合: abstract和final
        37.  
        38. // private abstract void print();\
        39. // Demo4_Abstract.java:36: 错误: 非法的修饰符组合: abstract和private
        40. }

3. 接口

  • 接口概述

    • 从狭义的角度讲,就是指java中的interface
    • 从广义的角度讲,就是对外提供规则的都是接口
  • 接口特点
    • 接口关键字interface表示

      • interface 接口名 {}
    • 类实现接口用 implements表示
      • class 类名 implements 接口名 {}
    • 接口不能实例化
      • 那么,接口如何实例化呢?
      • 按照多态的方式来实例化
    • 接口的子类
      • 可以是抽象类,但是意义不大。
      • 可以使具体类,要重写接口中的所有抽象方法(推荐方案)
  • 案例演示:接口特点
      1. class Demo1_Interface {
      2. public static void main(String[] args) {
      3. //Inter i = new Inter(); // 接口不能被实例化,因为调用抽象方法没有意义
      4.  
      5. Inter i = new Demo();
      6. i.print();
      7.  
      8. }
      9. }
      10.  
      11. interface Inter {
      12. public abstract void print(); // 接口中的方法都是抽象的
      13. }
      14.  
      15. class Demo implements Inter {
      16. public void print() {
      17. System.out.println("print");
      18. }
      19. }

      接口类-子类是具体类,重写接口的 抽象方法(推荐)

      1. class Demo1_Interface {
      2. public static void main(String[] args) {
      3.  
      4. }
      5. }
      6.  
      7. interface Inter {
      8. public abstract void print(); // 接口中的方法都是抽象的
      9. }
      10.  
      11. abstract class Demo implements Inter {
      12.  
      13. }

      接口-子类是抽象类(不推荐)

  • 接口的成员特点:

    • 成员变量

      • 只能是常量,并且是静态的,并公共的。
      • 默认修饰符:public static final(即使不手写,也会默认添加)
      • 建议:自己手动给出
    • 构造方法

      • 接口没有构造方法
    • 成员方法

      • 只能是抽象方法

        • 默认修饰符:public abstract
        • 建议:自己手动给出
    • 案例演示
        1. class Demo1_Interface {
        2. public static void main(String[] args) {
        3.  
        4. Demo d = new Demo();
        5. d.print();
        6.  
        7. System.out.println(Inter.num);
        8.  
        9. }
        10. }
        11.  
        12. interface Inter {
        13. public static final int num = 10; // 系统会默认添加public static final,建议手动写出
        14. // 这三个关键词没有顺序区别
        15.  
        16. // public Inter(){} 报错,接口中没有构造方法
        17.  
        18. // public void print() {} // 接口中不能定义非抽象方法
        19.  
        20. void print(); // 前面默认添加了public abstract修饰词
        21. public abstract void print2();
        22. }
        23.  
        24. class Demo /* extends Object */ implements Inter { // 一个类不写继承任何一个类,默认继承Object类
        25. public void print() {
        26.  
        27. System.out.println(num);
        28. }
        29.  
        30. public void print2() {}
        31. }
        1. class Test1_Aniaml {
        2. public static void main(String[] args) {
        3. Cat c = new Cat("加菲",8);
        4. c.eat();
        5. c.sleep();
        6.  
        7. JumpCat jc = new JumpCat("跳高猫",3);
        8. jc.eat();
        9. jc.sleep();
        10. jc.jump();
        11. }
        12. }
        13.  
        14. /*
        15. 动物类:姓名,年龄,吃饭,睡觉
        16.  
        17. 猫和狗
        18.  
        19. 动物培训接口:跳高
        20. */
        21.  
        22. abstract class Animal {
        23. private String name;
        24. private int age;
        25.  
        26. public Animal(){}
        27. public Animal(String name,int age){
        28. this.name = name;
        29. this.age = age;
        30. }
        31.  
        32. public void setName(String name){
        33. this.name = name;
        34. }
        35.  
        36. public String getName(){
        37. return name;
        38. }
        39.  
        40. public void setAge(int age){
        41. this.age = age;
        42. }
        43.  
        44. public int getAge(){
        45. return age;
        46. }
        47.  
        48. public abstract void eat();
        49.  
        50. public abstract void sleep();
        51.  
        52. }
        53.  
        54. interface Jumping {
        55. public void jump();
        56. }
        57.  
        58. class Cat extends Animal {
        59. public Cat(){}
        60. public Cat(String name,int age){
        61. super(name,age);
        62. }
        63.  
        64. public void eat() {
        65. System.out.println("猫吃鱼");
        66. }
        67.  
        68. public void sleep() {
        69. System.out.println("侧着睡");
        70. }
        71.  
        72. }
        73.  
        74. class JumpCat extends Cat implements Jumping {
        75. public JumpCat(){}
        76. public JumpCat(String name,int age){
        77. super(name,age);
        78. }
        79.  
        80. public void jump() {
        81. System.out.println("猫跳高");
        82. }
        83.  
        84. }

        动物:猫类,跳高的接口

3. 各种类,接口之间的关系

3.1 类与类,类与接口,接口与接口的关系

  • 类与类

    • 继承关系

      • 只能单继承,可以多层继承
  • 类与接口
    • 实现关系,可以单实现,也可以多实现
    • 并且还可以在继承一个类的同时实现多个接口
      1. class Demo1_Interface {
      2. public static void main(String[] args) {
      3.  
      4. }
      5. }
      6.  
      7. interface InterA {
      8. public abstract void printA();
      9. }
      10.  
      11. interface InterB {
      12. public abstract void printB();
      13. }
      14.  
      15. class Demo implements InterA,InterB {
      16. public void printA() {
      17. System.out.println("printA");
      18. }
      19.  
      20. public void printB() {
      21. System.out.println("printB");
      22. }
      23. }
  • 接口与接口
    • 继承关系

      • 可以单继承,也可以多继承。
        1. class Demo1_Interface {
        2. public static void main(String[] args) {
        3.  
        4. }
        5. }
        6.  
        7. interface InterA {
        8. public abstract void printA();
        9. }
        10.  
        11. interface InterB {
        12. public abstract void printB();
        13. }
        14.  
        15. interface InterC extends InterA,InterB {
        16. }

3.2 抽象类和接口的区别

  • 成员区别

    • 抽象类

      • 成员变量:可以变量,也可以常量
      • 构造方法:有
      • 成员方法:可以抽象,也可以非抽象
    • 接口
      • 成员变量:只可以常量
      • 成员方法:只可以抽象
  • 关系区别
    • 类与类

      • 继承,单继承
    • 类与接口
      • 实现,单实现,多实现
    • 接口与接口
      • 继承,单继承,多继承
  • 设计理念区别
  • 抽象类
    • 被继承体现的是“is a”的关系。
    • 抽象类中定义的是该继承体系的共性功能
  • 接口
    • 被实现体现的是“like a”的关系。
    • 接口中定义的是该继承体系的扩展功能

4. 包package

  • 为什么要有包

    • 将字节码(.class)进行分类存放
    • 包其实就是文件夹
  • 包的概述
    • 举例:

      • 需求:

        • 学生:增加、删除、修改、查询
        • 老师:增加、删除、修改、查询
      • 方案1:按照功能分
        • com.heima.add

          • AddStudent
          • AddTeacher
        • com.heima.delete
          • DeleteStudent
          • DeleteTeacher
        • com.heima.update
          • UpdateStudent
          • UpdateTeacher
        • com.heima.find
          • FindStudent
          • FindTeacher
      • 方案2:按照模块分
        • com.heima.teacher

          • AddTeacher
          • DeleteTeacher
          • UpdateTeacher
          • FindTeacher
        • com.heima.student
          • AddStudent
          • DeleteStudent
          • UpdateStudent
          • FindStudent
  • 包的定义
    • 格式

      • package 包名;
      • 多级包用“.” 分开即可
    • 注意事项
      • package语句必须是程序的第一条可执行的代码
      • package语句在一个java文件中只能有一个
      • 如果没有package,默认表示无包名
    • 案例演示
        1. package com.heima;
        2. // package语句必须是程序的第一条可执行的代码
        3. // 一个文件内只能有一个package语句
        4.  
        5. class Demo1_Package {
        6. public static void main(String[] args) {
        7.  
        8. }
        9. }
  • 如何编译运行带包的类

    • 案例演示

      • javac编译时加上-d即可

        • javac -d . HelloWorld.java
      • 通过java命令执行
        • java 包名.HelloWorld
        • java com.heima.Demo1_Package
  • 不同包下类之间的访问
    • 案例演示
      1. package com.heima;
      2. // package语句必须是程序的第一条可执行的代码
      3. // 一个文件内只能有一个package语句
      4.  
      5. import com.baidu.Person;
      6. import com.xxx.Student;
      7. //import java.util.Scanner; //在开发中我们用的都是导入具体的类
      8. import java.util.*; //*代表通配符,他会到该包下挨个匹配,匹配上就导入
      9. class Demo1_Package {
      10. public static void main(String[] args) {
      11. Person p = new Person("张三",23);
      12. System.out.println(p.getName() + "..." + p.getAge());
      13. //p.print(); //在不同包下的无关类,不允许访问,因为是protected修饰的
      14.  
      15. /*Scanner sc = new Scanner(System.in);
      16. int x = sc.nextInt();
      17. System.out.println(x);*/
      18.  
      19. Student s = new Student("李四",24);
      20. System.out.println(s.getName() + "..." + s.getAge());
      21. s.method();
      22. }
      23. }
  • import关键字的概述和使用

    • 为什么要有import

      • 其实就是让有包的类对调用者可见,不用写全类名了

    • 导包格式

      • import 包名;

      • 注意:

        • 这种方式导入是到类的名称。

        • 虽然可以最后写*,但是不建议。

    • package,import,class有没有顺序关系
      • 有。
      • package 第一行,只能一个文件一句
      • import 中间,可以有多句
      • class 放在这两个下面

5. 修饰符

  • 四种权限修饰符

    • private 本类;
    • 默认     本类;同一个包下(子类和无关类)
    • protected  本类;同一个包下(子类和无关类);不同包下(子类)
    • public   本类;同一个包下(子类和无关类);不同包下(子类);不同包下(无关类)
  • 修饰符
    • 权限修饰符:private,默认的,protected,public
    • 状态修饰符:static,final
    • 抽象修饰符:abstract
    • 权限修饰符:默认修饰符,public
    • 状态修饰符:final
    • 抽象修饰符:abstract
    • 用的最多的就是:public
  • 成员变量:

    • 权限修饰符:private,默认的,protected,public

    • 状态修饰符:static,final

    • 用的最多的就是:private

  • 构造方法:

    • 权限修饰符:private,默认的,protected,public

    • 用的最多的就是:public

  • 成员方法:

    • 权限修饰符:private,默认的,protected,public

    • 状态修饰符:static,final

    • 抽象修饰符:abstract

    • 用的最多的就是:public

  • 除此以外的组合规则:

    • 成员变量:public static final

    • 成员方法:

      • public static

      • public abstract

      • public final

6. 内部类

  • 内部类概述

    • 在类中定义类,叫做内部类
  • 内部类访问特点

    • 内部类可以直接访问外部类的成员,包括私有。

    • 外部类要访问内部类的成员,必须创建对象。

    • 外部类名.内部类名 对象名 = 外部类对象.内部类对象;

  • 案例演示

      1. class Demo1_InnerClass {
      2. public static void main(String[] args) {
      3. //Inner i = new Inner();
      4. //i.method();
      5. //外部类名.内部类名 = 外部类对象.内部类对象
      6. Outer.Inner oi = new Outer().new Inner(); //创建内部类对象
      7. oi.method();
      8.  
      9. }
      10. }
      11.  
      12. class Outer { // 外部类
      13. private int num = 10;
      14.  
      15. class Inner { // 内部类
      16. public void method() {
      17. System.out.println(num);
      18. }
      19. }
      20. }
  • 内部类分类
    • 成员内部类

      • 成员内部类私有使用

          1. class Demo2_InnerClass {
          2. public static void main(String[] args) {
          3. //Outer.Inner oi = new Outer().new Inner(); //内部类私有,无法调用
          4. //oi.method();
          5.  
          6. Outer o = new Outer();
          7. o.print();
          8. }
          9. }
          10.  
          11. class Outer {
          12. private int num = 10;
          13.  
          14. private class Inner { //内部类私有
          15. public void method() {
          16. System.out.println(num);
          17. }
          18. }
          19.  
          20. public void print() {
          21. Inner i = new Inner(); // 外部类,提供内部类实例化方法
          22. i.method();
          23. }
          24. }

          内部类私有

          1. class Test1_InnerClass {
          2. public static void main(String[] args) {
          3. Outer.Inner oi = new Outer().new Inner();
          4. oi.show();
          5. }
          6. }
          7. //要求:使用已知的变量,在控制台输出30,20,10。
          8. //内部类之所以能获取到外部类的成员,是因为他能获取到外部类的引用外部类名.this
          9. class Outer {
          10. public int num = 10;
          11.  
          12. class Inner {
          13. public int num = 20;
          14. public void show() {
          15. int num = 30;
          16. System.out.println(num);
          17. System.out.println(this.num);
          18. System.out.println(Outer.this.num);
          19. }
          20. }
          21. }

          成员内部类面试题:获取成员变量

    • 静态成员内部类
        1. class Demo1_InnerClass {
        2. public static void main(String[] args) {
        3. //外部类名.内部类名 对象名 = 外部类名.内部类对象;
        4. Outer.Inner oi = new Outer.Inner();
        5. oi.method();
        6.  
        7. Outer.Inner2.print();
        8. }
        9. }
        10.  
        11. class Outer {
        12.  
        13. static class Inner { // 静态成员内部类
        14. public void method() {
        15. System.out.println("method");
        16. }
        17. }
        18.  
        19. static class Inner2 {
        20. public static void print() {
        21. System.out.println("print");
        22. }
        23. }
        24. }

        静态内部类

    • 局部内部类
      • 局部内部类,只能在其所在的方法中访问
      • 局部内部类访问局部变量的问题
        • 局部内部类访问局部变量必须用final修饰
        • 局部内部类在访问他所在方法中的局部变量必须用final修饰,为什么?

          • 因为当调用这个方法时,局部变量如果没有用final修饰,他的生命周期和方法的生命周期是一样的,当方法弹栈,这个局部变量也会消失。

          • 那么如果局部内部类对象还没有马上消失想用这个局部变量,就没有了。

          • 如果用final修饰会在类加载的时候进入常量池,即使方法弹栈,常量池的常量还在,也可以继续使用。

          • 但是jdk1.8取消了这个事情,所以我认为这是个bug

          1. class Demo1_InnerClass {
          2. public static void main(String[] args) {
          3. Outer o = new Outer();
          4. o.method();
          5. }
          6. }
          7. //局部内部类
          8. class Outer {
          9.  
          10. public void method() {
          11. final int num = 10;
          12. //int num = 10; 错误:从内部类中访问本地变量num;需要被声明为最终类型。final
          13.  
          14. class Inner { // 局部内部类
          15. public void print() {
          16. System.out.println(num);
          17. }
          18. }
          19.  
          20. Inner i = new Inner(); //只能在局部所在的方法中访问
          21. i.print();
          22. }
          23.  
          24. /*public void run() {
          25. Inner i = new Inner(); //局部内部类,只能在其所在的方法中访问
          26. i.print();
          27. }*/
          28. }

          局部内部类示例,final修饰符修饰变量

    • 匿名内部类
      • 就是内部类的简化写法。
      • 前提:
        • 存在一个类或者接口
        • 这里的类可以是具体类也可以是抽象类。
      • 格式
        • new 类名或者接口名() {
              重写方法;
          }

      • 本质是什么呢?
        • 是一个继承了该类或者实现了该接口的子类匿名对象。
      • 案例演示
          1. class Demo1_NoNameInnerClass {
          2. public static void main(String[] args) {
          3. Outer o = new Outer();
          4. o.method();
          5. }
          6. }
          7.  
          8. interface Inter {
          9. public void print();
          10. }
          11.  
          12. class Outer {
          13.  
          14. /*class Inner implements Inter { // 这是有名字的内部类,继承Inter接口
          15. public void print() {
          16. System.out.println("print");
          17. }
          18. }*/
          19.  
          20. public void method(){
          21. //Inner i = new Inner();
          22. //i.print();
          23. //new Inner().print();
          24. //Inter i = new Inner(); //父类引用指向子类对象
          25.  
          26. new Inter() { //实现Inter接口
          27. public void print() { //重写抽象方法
          28. System.out.println("print");
          29. }
          30. }.print(); //
          31. }
          32. }

          匿名内部类:单个方法调用

          1. class Demo2_NoNameInnerClass {
          2. public static void main(String[] args) {
          3. Outer o = new Outer();
          4. o.method();
          5. }
          6. }
          7.  
          8. interface Inter {
          9. public void show1();
          10. public void show2();
          11. }
          12. //匿名内部类只针对重写一个方法时候使用
          13. class Outer {
          14. public void method() {
          15. /*new Inter(){
          16. public void show1() {
          17. System.out.println("show1");
          18. }
          19.  
          20. public void show2() {
          21. System.out.println("show2");
          22. }
          23. }.show1();
          24.  
          25. new Inter(){
          26. public void show1() {
          27. System.out.println("show1");
          28. }
          29.  
          30. public void show2() {
          31. System.out.println("show2");
          32. }
          33. }.show2();*/
          34.  
          35. Inter i = new Inter(){
          36. public void show1() {
          37. System.out.println("show1");
          38. }
          39.  
          40. public void show2() {
          41. System.out.println("show2");
          42. }
          43.  
          44. /*public void show3() {
          45. System.out.println("show3");
          46. }*/
          47. };
          48.  
          49. i.show1();
          50. i.show2();
          51. //i.show3(); //匿名内部类是不能向下转型的,因为没有子类类名
          52. }
          53. }

          匿名内部类只针对重写一个方法时候使用

      • 匿名内部类在开发中的应用
          1. class Test1_NoNameInnerClass {
          2. public static void main(String[] args) {
          3. //如何调用PersonDemo中的method方法呢?
          4. PersonDemo pd = new PersonDemo ();
          5. //pd.method(new Student());
          6. pd.method(new Person() {
          7. public void show() {
          8. System.out.println("show");
          9. }
          10. });
          11. }
          12. }
          13. //这里写抽象类,接口都行
          14. abstract class Person {
          15. public abstract void show();
          16. }
          17.  
          18. class PersonDemo {
          19.  
          20. //public void method(Person p) { //Person p = new Student(); //父类引用指向子类对象
          21. /*
          22. Person p = new Person(){
          23. public void show() {
          24. System.out.println("show");
          25. }
          26. };
          27. */
          28. public void method(Person p) {
          29. p.show();
          30. }
          31. }
          32.  
          33. class Student extends Person {
          34. public void show() {
          35. System.out.println("show");
          36. }
          37. }

          匿名内部类在开发中,当做参数传递(把匿名内部类当做一个对象)

          1. class Test2_NoNameInnerClass {
          2. public static void main(String[] args) {
          3. //Outer.method().show(); //链式编程,每次调用方法后还能继续调用方法,证明调用方法返回的是对象
          4. Inter i = Outer.method();
          5. i.show();
          6. }
          7. }
          8. //按照要求,补齐代码
          9. interface Inter {
          10. void show();
          11. }
          12.  
          13. class Outer {
          14. //补齐代码
          15. public static Inter method() {
          16. return new Inter() {
          17. public void show() {
          18. System.out.println("HelloWorld");
          19. }
          20. };
          21. }
          22. }
          23.  
          24. //要求在控制台输出”HelloWorld”

          根据主方法的调用方法,补齐代码

【JAVA基础】08 面向对象3的更多相关文章

  1. Java基础-初识面向对象编程(Object-Oriented-Programming)

    Java基础-初识面向对象编程(Object-Oriented-Programming) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. Java是一门面向对象的程序设计语言.那么什 ...

  2. 黑马程序员——【Java基础】——面向对象(二)异常机制、包(Package)

    ---------- android培训.java培训.期待与您交流! ---------- 一.异常机制 (一)异常概述 1.异常:就是程序在运行时出现不正常情况. 2.异常类:程序在运行时,出现的 ...

  3. Java基础之面向对象以及其他概念

    一.基础知识:1.JVM.JRE和JDK的区别: JVM(Java Virtual Machine):java虚拟机,用于保证java的跨平台的特性. java语言是跨平台,jvm不是跨平台的. JR ...

  4. Java基础08 继承

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 继承(inheritance)是面向对象的重要概念.继承是除组合(composit ...

  5. Java基础08 继承(转载)

    继承(inheritance)是面向对象的重要概念.继承是除组合(composition)之外,提高代码重复可用性(reusibility)的另一种重要方式.组合是重复调用对象的功能接口.继承可以重复 ...

  6. 【Java基础】面向对象下

    面向对象下 这一章主要涉及其他关键字,包括 this.super.static.final.abstract.interface.package.import 等. static 在 Java 类中, ...

  7. 【java基础】面向对象的三大基本特征之-------继承

    面向对象的三大特征:封装,继承,多态 java通过extends关键字来实现继承,而且是单继承,一个子类只可以有一个直接父类,但是父类还可以有父类... java.long.Object是所有类的父类 ...

  8. 黑马程序员——【Java基础】——面向对象(一)概述、类与对象、继承、抽象类、接口、多态、内部类

    ---------- android培训.java培训.期待与您交流! ---------- 一.面向对象概述 1.面向对象:是一个很抽象的概念,它相对面向过程而言,是一种程序设计的思想. 2.面向对 ...

  9. Python基础08 面向对象的基本概念

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 谢谢逆水寒龙,topmad和Liqing纠错 Python使用类(class)和对 ...

  10. 再探java基础——对面向对象的理解(1)

    对象 对象是人们要进行研究的任何事物,从最简单的整数到复杂的飞机等均可看作对象,它不仅能表示具体的事物,还能表示抽象的规则.计划或事件.对象具有属性和行为,在程序设计中对象实现了数据和操作的结合,使数 ...

随机推荐

  1. 为什么scanf(" %c",&c)中%c前要空格?

    空格确实不是必须的,但有了空格就可以忽略你输入的空格. ****例如:scanf(" %c" ,&c),你输入了' a'(a前面有个空格),a就能被c接受. 但控制符前如果 ...

  2. 浅谈头文件(.h)和源文件(.cpp)的区别

    浅谈头文件(.h)和源文件(.cpp)的区别 本人原来在大一写C的时候,都是所有代码写在一个文件里一锅乱煮.经过自己开始写程序之后,发现一个工程只有一定是由多个不同功能.分门别类展开的文件构成的.一锅 ...

  3. 这些基本的 HTML5 标签你不能不知道

    HTML5元素 HTML5是HTML最新的修订版本,2014年10月由万维网联盟(W3C)完成标准制定. HTML5是用来写网页的一门标记语言. 使用的时候需要在首行声明HTML,如:<!DOC ...

  4. django-rest-framework权限验证

    django-rest-framework权限验证 在项目根目录下新建utils的文件 新建permissions.py from rest_framework.permissions import ...

  5. Java第二十天,Map集合(接口)

    Map接口 一.定义 Map集合是双列集合,即一个元素包含两个值(一个key,一个value),Collection集合是单列集合. 定义格式: public interface Map<K,V ...

  6. Java入门第一阶段总结

    前言 写了三周的模拟题,对原本就厌恶的模拟更加深恶痛绝.但是不得不说模拟题是对一门语言入门掌握其语法成效最快的一类题,轻松地从C入门到了Java.一直坚信各门语言都是想通的,一力破万法. 作业过程总结 ...

  7. shell http请求&处理返回值获取其中某个字段的值

    并且第一个接口的返回值中部分参数得作为第二个接口的入参,所以做了如下shell验证 第一个接口返回的response body串: { "bizCode": "1&quo ...

  8. python3(三十六)StringIO BytesIO

    """ StringIO和BytesIO """ __author__on__ = 'shaozhiqi 2019/9/23' # !/us ...

  9. hive常用函数四

    字符串函数 1. 字符串长度函数:length 语法: length(string A) 返回值: int 说明:返回字符串A的长度 举例: hive> select length('abced ...

  10. AJ学IOS(54)多线程网络之NSOperation重要知识

    AJ分享,必须精品 一:队列的类型与队列添加任务 1: 主队列 [NSOperationQueue mainQueue] 添加到”主队列”中的操作,都会放到主线程中执行. 2:非主队列 [[NSOpe ...