第二章:面向对象

一、对象的封装

1. 类和对象

1.1 类

image.png

1.2 对象

万物皆对象,客观存在的事物即为对象

1.3 对象的属性

对象的各种特征,每个对象的每个属性都有着特定的值

1.4 对象的行为

对象能够执行的操作

1.5 类和对象的关系

类是对象的抽象分类
对象是类的实体

1.6 类的定义

image.png

  1. //类
  2. public class Phone(){
  3. //成员变量
  4. String brand;
  5. int price;
  6. //成员方法
  7. public void call(){
  8. System.out.println{"打电话"};
  9. }
  10. public void sendMessage(){
  11. System.out.println("发短信");
  12. }
  13. }

1.9 对象的使用

image.png

  1. //注意对象和类是结合使用的
  2. public class PhoneDemo(){
  3. public static void main(String[] args){
  4. //-----------------成员变量的使用
  5. //创建对象
  6. Phone p = new Phone();
  7. //使用对象
  8. System.out.println(p.brand);
  9. System.out.println(p.price);
  10. //变量赋值
  11. p.brand = "小米";
  12. p.price = 59999;
  13. System.out.println(p.brand);
  14. System.out.println(p.price);
  15. //------------------成员方法的使用
  16. //使用成员方法
  17. p.call();
  18. p.sendMessage();
  19. }
  20. }

2.0 案例学生类的定义和使用

  1. //--------------------------------------------------学生类
  2. package com.iteima_01;
  3. public class Student {
  4. //成员变量
  5. String name;
  6. int age;
  7. //成员方法
  8. public void study(){
  9. System.out.println("好好学习,天天向上");
  10. }
  11. public void doHomework(){
  12. System.out.println("键盘敲烂,月薪过万");
  13. }
  14. }
  15. //--------------------------------------------------学生类的使用
  16. package com.iteima_01;
  17. //学生测试类
  18. public class StudentDemo {
  19. public static void main(String[] args){
  20. //创建对象
  21. Student s = new Student();
  22. //使用对象
  23. System.out.println(s.name+","+s.age);
  24. s.name = "马云";
  25. s.age = 44;
  26. System.out.println(s.name+","+s.age);
  27. //使用方法
  28. s.study();
  29. s.doHomework();
  30. }
  31. }

2.1 成员变量和局部变量的含义

image.png

2.2 成员变量和局部变量的区别

image.png

二、对象的继承

1. 继承

image.png
image.png

1. 案例

  1. //--------------------------------------这是父类
  2. package com.iteima_01;
  3. public class Fu {
  4. public void call(){
  5. System.out.println("Fu方法被调用");
  6. }
  7. }
  8. //--------------------------------------这是子类
  9. package com.iteima_01;
  10. public class Zi extends Fu{
  11. public void test(){
  12. System.out.println("这是Zi方法");
  13. }
  14. }
  15. //--------------------------------------这是main方法测试类
  16. package com.iteima_01;
  17. public class Demo {
  18. public static void main(String[] args) {
  19. Fu f = new Fu();
  20. Zi z = new Zi();
  21. f.call();
  22. z.test();
  23. z.call();
  24. }
  25. }

2. 继承的好处和弊端

image.png

3. 继承中变量的访问特点

1. 继承中变量的访问顺序

image.png

2. 继承中 this super 的使用方法

image.png

  1. //--------------------------------------这是父类
  2. package com.iteima_01;
  3. public class Fu {
  4. int a = 30;
  5. public void call(){
  6. System.out.println("Fu方法被调用");
  7. }
  8. }
  9. //--------------------------------------这是子类
  10. package com.iteima_01;
  11. int a = 20;
  12. public class Zi extends Fu{
  13. public void test(){
  14. int a = 10;
  15. System.out.println("这是访问局部变量a"+a);
  16. System.out.println("这是访问子类变量a"+this.a);
  17. System.out.println("这是访问父类变量a"+super.a);
  18. }
  19. }

3. 继承中构造方法的访问特点

image.png

4. 继承中成员方法的访问特点

image.png

5. 方法重写

image.png

  1. //--------------------------------------这是父类
  2. package com.iteima_01;
  3. public class Fu {
  4. public void call(String name){
  5. System.out.println("给"+name+"打电话");
  6. }
  7. }
  8. //--------------------------------------这是子类
  9. package com.iteima_01;
  10. public class Zi extends Fu{
  11. @Override
  12. public void call(String name){
  13. System.out.println("开启视频通话功能");
  14. super.call(name);
  15. }
  16. }
  17. //--------------------------------------这是main方法测试类
  18. package com.iteima_01;
  19. public class Demo {
  20. public static void main(String[] args) {
  21. Fu f = new Fu();
  22. f.call("马云");
  23. System.out.println("---------");
  24. Zi z = new Zi();
  25. z.call("马云");
  26. }
  27. }
  • 私有方法不能被重写(父类的私有成员子类是不能被继承的)
  • 方法重写的时候,子类的方法权限不能低于父类(public > 默认 > 私有)

    6. Java继承的注意事项

    image.png
    image.png

    7. 案例 老师和学生 super传参

    ```java //——————————————————————————人类 package com.iteima_01;

public class People { private String name; private int age;

  1. public People() {
  2. }
  3. public People(int age, String name) {
  4. this.age = age;
  5. this.name = name;
  6. }
  7. public int getAge() {
  8. return age;
  9. }
  10. public void setAge(int age) {
  11. this.age = age;
  12. }
  13. public String getName() {
  14. return name;
  15. }
  16. public void setName(String name) {
  17. this.name = name;
  18. }

} //——————————————————————————老师类 package com.iteima_01;

public class Teacher extends People { public Teacher(){}

  1. public Teacher(String name,int age) {
  2. super(age, name);
  3. }
  4. public void teach(){
  5. System.out.println("用爱成就每一位学员");
  6. }

} //——————————————————————————学生类 package com.iteima_01;

public class Student extends Teacher{ public Student(){} public Student(String name,int age){ super(name, age); } public void study(){ System.out.println(“好好学习,天天向上”); } } //——————————————————————————测试类 package com.iteima_01;

public class Demo { public static void main(String[] args) { //无参老师类 Teacher t1 = new Teacher(); t1.setName(“武当王也”); t1.setAge(18); System.out.println(t1.getName()+”,”+t1.getAge()); t1.teach();

  1. //带参老师类
  2. Teacher t2 = new Teacher("校长",20);
  3. System.out.println(t2.getName()+","+t2.getAge());
  4. t2.teach();
  5. System.out.println("-------------------我是分割线-----------------------");
  6. //无参学生类
  7. Student s1 = new Student();
  8. s1.setName("全性龚庆");
  9. s1.setAge(15);
  10. System.out.println(s1.getName()+","+s1.getAge());
  11. s1.study();
  12. //带参学生类
  13. Student s2 = new Student("学生",13);
  14. System.out.println(s2.getName()+","+s2.getAge());
  15. s2.study();
  16. }

}

  1. <a name="oLZSi"></a>
  2. #### 8. 案例 猫和狗 super传参
  3. ```java
  4. //----------------------------------------------------动物类
  5. package com.iteima_01;
  6. //这是动物类
  7. public class People {
  8. private String name;
  9. private int age;
  10. public People() {
  11. }
  12. public People(String name, int age) {
  13. this.age = age;
  14. this.name = name;
  15. }
  16. public int getAge() {
  17. return age;
  18. }
  19. public void setAge(int age) {
  20. this.age = age;
  21. }
  22. public String getName() {
  23. return name;
  24. }
  25. public void setName(String name) {
  26. this.name = name;
  27. }
  28. }
  29. //----------------------------------------------------猫类
  30. package com.iteima_01;
  31. //这是猫类
  32. public class Teacher extends People{
  33. public Teacher(){}
  34. public Teacher(String name,int age){
  35. super (name,age);
  36. }
  37. public void cat() {
  38. System.out.println("我会抓老鼠");
  39. }
  40. }
  41. //----------------------------------------------------测试类
  42. package com.iteima_01;
  43. public class Demo {
  44. public static void main(String[] args) {
  45. //无参猫类
  46. Teacher t1 = new Teacher();
  47. t1.setName("黑猫");
  48. t1.setAge(6);
  49. System.out.println(t1.getName()+","+t1.getAge());
  50. t1.cat();
  51. //带参猫类
  52. Teacher t2 = new Teacher("白猫",8);
  53. System.out.println(t2.getName()+","+t2.getAge());
  54. t2.cat();
  55. }
  56. }

4. package 包

1. 包的概述和创建

image.png

2. 导包

image.png

3. 权限修饰符 默认、private、protected、public

image.png

  1. package com.iteima_01;
  2. public class People{
  3. private void show1(){
  4. System.out.println("private");
  5. }
  6. void show2(){
  7. System.out.println("默认");
  8. }
  9. protected void show3(){
  10. System.out.println("protected");
  11. }
  12. public void show4(){
  13. System.out.println("public");
  14. }
  15. }

4. final修饰局部变量

image.png

  1. package com.iteima_01;
  2. public class Demo {
  3. public static void main(String[] args) {
  4. //final修饰局部变量
  5. final int a = 10;
  6. System.out.println("这是赋值前局部变量"+a);
  7. // a = 100;
  8. System.out.println("这是赋值后局部变量"+a);
  9. //final修饰引用变量
  10. final Teacher t = new Teacher();
  11. System.out.println("这是赋值前引用变量"+t.a);
  12. t.a = 200;
  13. System.out.println("这是赋值后引用变量"+t.a);
  14. // t = new Teacher();
  15. }
  16. }

5. static

image.png

  1. package com.iteima_01;
  2. public class Teacher {
  3. public String name;
  4. public int age;
  5. public static String from;
  6. public void show(){
  7. System.out.println(name+","+age+","+from);
  8. }
  9. }
  10. //----------------------------------------------------测试类
  11. package com.iteima_01;
  12. public class Demo {
  13. public static void main(String[] args) {
  14. Teacher.from = "China";
  15. Teacher t = new Teacher();
  16. t.name = "张三";
  17. t.age = 20;
  18. // t.from = "China";
  19. t.show();
  20. Teacher t2 = new Teacher();
  21. t2.name = "李四";
  22. t2.age = 22;
  23. // t.from = "China";
  24. t2.show();
  25. }
  26. }

6. static的访问特点

  • 静态成员方法只能访问静态成员
  • 非静态成员能访问所有的成员

    5. 多态

    1. 多态的简介案例

    image.png ```java //——————————————————————————父类 package com.iteima_01;

public class Fu { public void eat(){ System.out.println(“我是父类猫”); } } //——————————————————————————子类 package com.iteima_01;

public class Zi extends Fu{ @Override public void eat() { System.out.println(“子类的猫会吃鱼”); } } //——————————————————————————测试类 package com.iteima_01;

public class Demo { public static void main(String[] args) { Fu f = new Zi(); f.eat(); } }

  1. <a name="dBy1t"></a>
  2. #### 2. 多态的访问特点
  3. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/5358492/1618149595818-34ce2773-3a0a-4686-a8d6-8d1315eb06ae.png#align=left&display=inline&height=163&id=lmRbU&margin=%5Bobject%20Object%5D&name=image.png&originHeight=163&originWidth=382&size=57136&status=done&style=none&width=382)
  4. <a name="SwCcu"></a>
  5. #### 3. 多态的好处和弊端
  6. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/5358492/1618150103112-cb4d9383-9115-42d0-ad22-5aa3edb3e39c.png#align=left&display=inline&height=90&id=lkdGu&margin=%5Bobject%20Object%5D&name=image.png&originHeight=90&originWidth=820&size=52785&status=done&style=none&width=820)
  7. <a name="ACeXY"></a>
  8. #### 4. 多态的转型
  9. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/5358492/1618150308544-e152a7d4-db45-49a5-80d7-f32b8469cc80.png#align=left&display=inline&height=194&id=kmz8g&margin=%5Bobject%20Object%5D&name=image.png&originHeight=194&originWidth=229&size=27994&status=done&style=none&width=229)
  10. ```java
  11. //----------------------------------------------------父类
  12. package com.iteima_01;
  13. public class Fu {
  14. public void eat(){
  15. System.out.println("我是父类猫");
  16. }
  17. }
  18. //----------------------------------------------------子类
  19. package com.iteima_01;
  20. public class Zi extends Fu{
  21. @Override
  22. public void eat() {
  23. System.out.println("子类的猫会吃鱼");
  24. }
  25. public void game(){
  26. System.out.println("子类的猫会玩游戏");
  27. }
  28. }
  29. //----------------------------------------------------测试类
  30. package com.iteima_01;
  31. public class Demo {
  32. public static void main(String[] args) {
  33. //向上转型
  34. Fu f = new Zi();
  35. f.eat();
  36. //向下转型
  37. Zi z = (Zi) f;
  38. z.eat();
  39. z.game();
  40. }
  41. }

5. 案例 猫和狗 多态继承的方法

  1. //-------------------------------------------------父类
  2. package com.iteima_01;
  3. public class Fu {
  4. private String name;
  5. private int age;
  6. public Fu() {}
  7. public Fu(String name,int age){
  8. this.name = name;
  9. this.age = age;
  10. }
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. public int getAge() {
  18. return age;
  19. }
  20. public void setAge(int age) {
  21. this.age = age;
  22. }
  23. public void eat(){
  24. System.out.println("父类吃饭");
  25. }
  26. }
  27. //-------------------------------------------------子类
  28. package com.iteima_01;
  29. public class Zi extends Fu{
  30. public Zi() {
  31. }
  32. public Zi(String name, int age) {
  33. super(name, age);
  34. }
  35. @Override
  36. public void eat() {
  37. System.out.println("子类的猫会玩游戏");
  38. }
  39. }
  40. //-------------------------------------------------测试类
  41. package com.iteima_01;
  42. public class Demo {
  43. public static void main(String[] args) {
  44. Fu f = new Zi();
  45. f.setAge(5);
  46. f.setName("黑猫");
  47. System.out.println(f.getName()+","+f.getAge());
  48. f.eat();
  49. f = new Zi("白猫",4);
  50. System.out.println(f.getName()+","+f.getAge());
  51. f.eat();
  52. }
  53. }

6. 抽象类 abstract

image.png

7. 抽象类的特点

image.png

8. 抽象类成员的特点

image.png

9. 案例 猫和狗 抽象类的使用

  1. //-------------------------------------------------父类
  2. package com.iteima_01;
  3. public abstract class Fu {
  4. private String name;
  5. private int age;
  6. public Fu() {
  7. }
  8. public Fu(String name, int age) {
  9. this.name = name;
  10. this.age = age;
  11. }
  12. public String getName() {
  13. return name;
  14. }
  15. public void setName(String name) {
  16. this.name = name;
  17. }
  18. public int getAge() {
  19. return age;
  20. }
  21. public void setAge(int age) {
  22. this.age = age;
  23. }
  24. public abstract void eat();
  25. }
  26. //-------------------------------------------------子类
  27. package com.iteima_01;
  28. public class Zi extends Fu{
  29. public Zi() {
  30. }
  31. public Zi(String name, int age) {
  32. super(name, age);
  33. }
  34. @Override
  35. public void eat() {
  36. System.out.println("子类的猫会晒太阳");
  37. }
  38. }
  39. //-------------------------------------------------测试类
  40. package com.iteima_01;
  41. public class Demo {
  42. public static void main(String[] args) {
  43. Fu f = new Zi();
  44. f.eat();
  45. f.setName("黑猫");
  46. f.setAge(4);
  47. System.out.println(f.getName()+","+f.getAge());
  48. f = new Zi("白猫",5);
  49. System.out.println(f.getName()+","+f.getAge());
  50. f.eat();
  51. }
  52. }

6. 接口 interface implements

1.描述

image.png

2. 接口的特点

image.png

3. 接口成员的特点

image.png

4. 案例 猫和狗 接口的使用

  1. //------------------------------------------------------主类
  2. package com.iteima_02;
  3. public interface Jumpping{
  4. public abstract void jump();
  5. }
  6. //------------------------------------------------------动物类
  7. package com.iteima_02;
  8. public abstract class Animal {
  9. private String name;
  10. private int age;
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. public int getAge() {
  18. return age;
  19. }
  20. public void setAge(int age) {
  21. this.age = age;
  22. }
  23. public Animal(String name, int age) {
  24. this.name = name;
  25. this.age = age;
  26. }
  27. public Animal() {
  28. }
  29. public abstract void eat();
  30. }
  31. //------------------------------------------------------猫类
  32. package com.iteima_02;
  33. public class Cat extends Animal implements Jumpping{
  34. @Override
  35. public void eat() {
  36. System.out.println("猫吃鱼");
  37. }
  38. @Override
  39. public void jump() {
  40. System.out.println("猫会跳高");
  41. }
  42. public Cat(String name, int age) {
  43. super(name, age);
  44. }
  45. public Cat() {
  46. }
  47. }
  48. //------------------------------------------------------测试类
  49. package com.iteima_02;
  50. public class Demo {
  51. public static void main(String[] args) {
  52. Jumpping j = new Cat();
  53. j.jump();
  54. Animal a = new Cat();
  55. a.setName("黑猫");
  56. a.setAge(6);
  57. System.out.println(a.getName()+","+a.getAge());
  58. a.eat();
  59. a = new Cat("白猫",4);
  60. System.out.println(a.getName()+","+a.getAge());
  61. a.eat();
  62. }
  63. }

5. 类和接口的关系

image.png

6. 抽象类和接口的区别

image.png

7. 案例 运动员和教练

image.png

  1. //------------------------------------------------------------------------学英语接口
  2. package Demo;
  3. public interface LeanEnglish {
  4. public void speakEnglish();
  5. }
  6. //------------------------------------------------------------------------人总类
  7. package Demo;
  8. public abstract class Ren {
  9. private int age;
  10. private String name;
  11. public Ren(int age, String name) {
  12. this.age = age;
  13. this.name = name;
  14. }
  15. public Ren() {
  16. }
  17. public int getAge() {
  18. return age;
  19. }
  20. public void setAge(int age) {
  21. this.age = age;
  22. }
  23. public String getName() {
  24. return name;
  25. }
  26. public void setName(String name) {
  27. this.name = name;
  28. }
  29. public void each(){
  30. System.out.println("我是人类吃饭");
  31. }
  32. }
  33. //------------------------------------------------------------------------学生类
  34. package Demo;
  35. public abstract class Study extends Ren{
  36. public Study() {
  37. }
  38. public Study(int age, String name) {
  39. super(age, name);
  40. }
  41. public void xue(){
  42. System.out.println("我是学生类");
  43. }
  44. }
  45. //------------------------------------------------------------------------老师类
  46. package Demo;
  47. public abstract class Teach extends Ren{
  48. public Teach(int age, String name) {
  49. super(age, name);
  50. }
  51. public Teach() {
  52. }
  53. public void jiao(){
  54. System.out.println("我是老师类");
  55. }
  56. }
  57. //------------------------------------------------------------------------篮球老师类
  58. package Demo;
  59. public class Lteacher extends Teach{
  60. public Lteacher(int age, String name) {
  61. super(age, name);
  62. }
  63. public Lteacher() {
  64. }
  65. @Override
  66. public void jiao() {
  67. System.out.println("我是篮球教练,我教悠悠球");
  68. }
  69. @Override
  70. public void each() {
  71. System.out.println("我是篮球教练,我吃奶");
  72. }
  73. }
  74. //------------------------------------------------------------------------乒乓球老师类
  75. package Demo;
  76. public class Pteacher extends Teach implements LeanEnglish{
  77. public Pteacher(int age, String name) {
  78. super(age, name);
  79. }
  80. public Pteacher() {
  81. }
  82. @Override
  83. public void each() {
  84. System.out.println("我是乒乓球教练,我教国足");
  85. }
  86. @Override
  87. public void jiao() {
  88. System.out.println("我是乒乓球教练,我吃肉");
  89. }
  90. @Override
  91. public void speakEnglish() {
  92. System.out.println("我是乒乓球教练,我教英语");
  93. }
  94. }
  95. //------------------------------------------------------------------------篮球学生类
  96. package Demo;
  97. public class Lstudent extends Study{
  98. public Lstudent() {
  99. }
  100. public Lstudent(int age, String name) {
  101. super(age, name);
  102. }
  103. @Override
  104. public void each() {
  105. System.out.println("我是篮球运动员,我吃屎");
  106. }
  107. @Override
  108. public void xue() {
  109. System.out.println("我是篮球运动员,我学打篮球");
  110. }
  111. }
  112. //------------------------------------------------------------------------乒乓球学生类
  113. package Demo;
  114. public class Pstudent extends Study implements LeanEnglish{
  115. public Pstudent() {
  116. }
  117. public Pstudent(int age, String name) {
  118. super(age, name);
  119. }
  120. @Override
  121. public void each() {
  122. System.out.println("我是乒乓球运动员,我吃求");
  123. }
  124. @Override
  125. public void xue() {
  126. System.out.println("我是乒乓球运动员,我学打乒乓球");
  127. }
  128. @Override
  129. public void speakEnglish() {
  130. System.out.println("我是乒乓球运动员,我学习说英语");
  131. }
  132. }
  133. //------------------------------------------------------------------------测试类
  134. package Demo;
  135. //测试类
  136. public class Demo {
  137. public static void main(String[] args) {
  138. //测试乒乓球
  139. Pstudent p = new Pstudent();
  140. p.setName("李四");
  141. p.setAge(20);
  142. System.out.println(p.getName()+","+p.getAge());
  143. p.xue();
  144. p.each();
  145. p.speakEnglish();
  146. System.out.println("----------------------------");
  147. Pteacher p2 = new Pteacher();
  148. p2.setAge(30);
  149. p2.setName("曹大大");
  150. System.out.println(p2.getName()+","+p2.getAge());
  151. p2.jiao();
  152. p2.each();
  153. p2.speakEnglish();
  154. }
  155. }

7. 内部类

1. 类名作为形参和返回值

image.png

2. 抽象类名作为形参和返回值

image.png

3. 接口名作为形参和返回值

image.png

4. 内部类访问特点

  • 内部类可以直接访问外部类的成员,包括私有
  • 外部类想要访问内部类的成员,必须先创建对象

    5. 成员内部类

    image.png ```java //——————————————————————————————————————主类 package com;

public class Outer { int num = 20; / 第一种 public class Inner{ public void show () { System.out.println(num); } } /

  1. private class Inner{
  2. public void show() {
  3. System.out.println(num);
  4. }
  5. }
  6. public void method() {
  7. Inner i = new Inner();
  8. i.show();
  9. }

} //——————————————————————————————————————测试类 package com;

public class Demo { / 第一种 public static void main(String[] args) { Outer.Inner i = new Outer().new Inner(); i.show(); } / public static void main(String[] args) { Outer out = new Outer(); out.method(); } }

  1. <a name="VVN0k"></a>
  2. #### 6. 局部内部类
  3. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/5358492/1618405405099-c58cb979-6905-4ed9-8850-f87777deb3d4.png#align=left&display=inline&height=62&id=VFJv4&margin=%5Bobject%20Object%5D&name=image.png&originHeight=62&originWidth=736&size=48382&status=done&style=none&width=736)
  4. ```java
  5. //----------------------------------------------------------------------------主类
  6. package com;
  7. public class Outer {
  8. private int num = 10;
  9. public void method(){
  10. int a = 30;
  11. class Inner{
  12. public void show(){
  13. System.out.println(num);
  14. System.out.println(a);
  15. }
  16. }
  17. Inner i = new Inner();
  18. i.show();
  19. }
  20. }
  21. //----------------------------------------------------------------------------测试类
  22. package com;
  23. public class Demo {
  24. public static void main(String[] args) {
  25. Outer o = new Outer();
  26. o.method();
  27. }
  28. }

7. 匿名内部类

image.png

  1. //----------------------------------------------------------------------------主类
  2. package com;
  3. public class Outer {
  4. public void method() {
  5. inter i = new inter(){
  6. @Override
  7. public void show() {
  8. System.out.println("内部接口调用");
  9. }
  10. };
  11. for (int a=0;a<5;a++){
  12. i.show();
  13. }
  14. }
  15. }
  16. //----------------------------------------------------------------------------接口类
  17. package com;
  18. public interface inter {
  19. void show();
  20. }
  21. //----------------------------------------------------------------------------测试类
  22. package com;
  23. public class Demo {
  24. public static void main(String[] args) {
  25. Outer o = new Outer();
  26. o.method();
  27. }
  28. }

8. 匿名内部类在开发中的使用

image.png

  1. //----------------------------------------------------------------------------主类
  2. package com;
  3. public class Outer {
  4. public void Method(inter j){
  5. j.jump();
  6. }
  7. }
  8. //----------------------------------------------------------------------------接口
  9. package com;
  10. public interface inter {
  11. void jump();
  12. }
  13. //----------------------------------------------------------------------------测试类
  14. package com;
  15. import java.lang.reflect.Method;
  16. public class Demo {
  17. public static void main(String[] args) {
  18. Outer o = new Outer();
  19. o.Method(new inter(){
  20. @Override
  21. public void jump() {
  22. System.out.println("猫可以跳高了");
  23. }
  24. });
  25. }
  26. }