java面向编程(三)关键词this、super、static、final和default

java面向对象关键词

一、关键词this的使用

1.this:就是指当前对象
  • 当前对象就是当前调用它的对象
  1. class Car {
  2. private String name;
  3. public Car(String name) {
  4. this.name = name;
  5. }
  6. public String getName() {
  7. return this.name;
  8. }
  9. }
  10. public class KeyWords {
  11. public static void main(String[] args) {
  12. Car c1 = new Car("奥迪A8");
  13. Car c2 = new Car("领克03");
  14. Car c3 = new Car("哈弗H6");
  15. System.out.println(c1.getName()); //c1调用getName方法时,此时this表示c1
  16. System.out.println(c2.getName()); //c2调用getName方法时,此时this表示c2
  17. System.out.println(c3.getName()); //c3调用getName方法时,此时this表示c3
  18. }
  19. }
  • c1调用getName方法时,this指向c1这个对象
  • c2调用getName方法时,this指向c2这个对象
2.this的作用
  • 直接引用,类似于当前对象的指针
  • 形参与成员属性相同时,用于区分成员变量和形参
  • 引用构造函数
  1. /**
  2. * 响应类
  3. */
  4. class Response {
  5. /**
  6. * 响应状态码
  7. */
  8. private int code;
  9. /**
  10. * 响应提示信息
  11. */
  12. private String message;
  13. /**
  14. * 响应数据
  15. */
  16. private Object data;
  17. public Response() {
  18. }
  19. public Response(int code) {
  20. /**
  21. * 1.此时的this指代当前对象
  22. * 2.因为传入的参数code与当前的成员变量相同,因此使用this.code来代表当前的对象的成员变量,code表示传入的参数
  23. */
  24. this.code = code;
  25. }
  26. public Response(int code, String message) {
  27. /*this.code = code;
  28. this.message = message;*/
  29. /**
  30. * 3.使用this(code)调用构造方法Response(int code)
  31. */
  32. this(code);
  33. this.message = message;
  34. }
  35. public Response(int code, String message, Object o) {
  36. /*this.code = code;
  37. this.message = message;
  38. this.data = o;*/
  39. /**
  40. * 3.使用this(code, message)调用构造方法Response(int code, String message)
  41. */
  42. this(code, message);
  43. this.data = o;
  44. }
  45. public void output() {
  46. System.out.println("父类输出:code = "+this.code +"; message = "+this.message);
  47. }
  48. }
3.this使用的注意点
  • 在构造方法中调用this构造方法必须把this构造方法放在第一行
  • 构造方法中调用this构造方法,不要陷入死循环中,即在A构造方法中调用this调用构造方法B,又在构造方法B中使用this调用构造方法A

二、关键词super的使用

1.super:指代当前的父类(超类)对象
  1. class JsonResponse extends Response {
  2. public JsonResponse(int code, String message) {
  3. super(code, message);
  4. }
  5. public void output() {
  6. super.output();
  7. System.out.println("子类输出");
  8. }
  9. }
  10. public class KeyWords {
  11. public static void main(String[] args) {
  12. JsonResponse r = new JsonResponse(100, "hello");
  13. r.output(); //输出:
  14. /**
  15. 父类输出:code = 100; message = hello
  16. 子类输出
  17. */
  18. }
  19. }
  • 通过super可以调用父类方法(前提是有权限)
  • 通过super可以调用父类成员(前提是有权限)
2.super的作用

super的作用与this非常相似,只不过super是指父类(超类方法)

  • 直接引用,调用父类变量(protected及以上权限的变量)
  • 引用父类方法
  • 引用父类构造函数

三、关键词static的使用

1.static:表示静态,一般是不可变的变量或方法
2.static的用法
(1)静态变量:类变量,通过类来调用
  • 静态方法和成员方法都可以调用
  1. protected static int SUCCESS_CODE = 200;
(2)静态方法
  • 静态方法只能访问静态变量,不能访问成员变量和成员方法
  1. public static Result success() {
  2. return new Result(SUCCESS_CODE, SUCCESS_MSG);
  3. }
(3)静态代码块
  • 在类被实例化后就会被执行(或给静态的List添加值)
  1. private static List<Integer> errorCode = new ArrayList<Integer>(5);
  2. static {
  3. errorCode.add(500);
  4. errorCode.add(502);
  5. errorCode.add(503);
  6. }
(4)静态导入
  • 导入包时,通过静态导入,可以直接使用静态方法,不需要加包名
  1. import static java.util.Collections.max;

在使用时直接使用max方法即可

  1. /*Collections.max(errorCode);*/
  2. System.out.println(max(errorCode));
(5)静态类
  • 静态类一般在内部类中使用,它只使用访问外部类的静态成员
  1. static class Demo {
  2. }
  1. import java.util.ArrayList;
  2. /**
  3. * 4.静态导入
  4. */
  5. import static java.util.Collections.max;
  6. import java.util.List;
  7. class Result {
  8. private static List<Integer> errorCode = new ArrayList<Integer>(5);
  9. /**
  10. * 3.静态代码块
  11. */
  12. static {
  13. errorCode.add(500);
  14. errorCode.add(502);
  15. errorCode.add(503);
  16. }
  17. /**
  18. * 成员变量
  19. */
  20. private int code;
  21. private String message;
  22. protected String tip;
  23. /**
  24. * 1.静态变量
  25. */
  26. protected static int SUCCESS_CODE = 200;
  27. protected static int ERROR_CODE = 500;
  28. protected static String SUCCESS_MSG = "success";
  29. protected static String ERROR_MSG = "failed";
  30. public Result(int code) {
  31. this.code = code;
  32. }
  33. public Result(int code, String message) {
  34. this(code);
  35. this.message = message;
  36. }
  37. /**
  38. * 2.静态方法
  39. * @return
  40. */
  41. public static Result success() {
  42. return new Result(SUCCESS_CODE, SUCCESS_MSG);
  43. }
  44. public static Result success(String message) {
  45. return new Result(SUCCESS_CODE, message);
  46. }
  47. public void maxErrorCode() {
  48. /*Collections.max(errorCode);*/
  49. /**
  50. * 4.静态导入使用
  51. */
  52. System.out.println(max(errorCode));
  53. }
  54. /**
  55. * 静态类
  56. */
  57. static class Error {
  58. public static int getCode() {
  59. return errorCode.get(0);
  60. }
  61. //无法访问非静态变量
  62. /*public String getMessage() {
  63. return message;
  64. }*/
  65. }
  66. }

四、关键词final的使用

1.final:表示最终的,不可变的
2.作用
  • 修饰类class:表示这个类是一个最终类,无法再继承它,它就是一个终结者
  • 修饰变量:表示它是一个常量,常常与static一起修饰一个类常量
  • 修改方法:表示它是一个最终的方法,不会被覆盖
  1. /**
  2. * 1.最终类:类终结者,无法被继承
  3. */
  4. final class FinalDemo {
  5. /**
  6. * 2.最终变量:不能修改其值
  7. */
  8. public final int CODE = 100;
  9. /**
  10. * 与static结合,表示一个类常量
  11. */
  12. public final static String MESSAGE = "SUCCESS";
  13. /**
  14. * 3.最终方法,不能被覆盖
  15. */
  16. public final int getCode() {
  17. return CODE;
  18. }
  19. }

五、关键词default的使用

1.default:默认方法,官方叫虚拟扩展方法(jdk1.8添加的,用于扩展接口)
2.default的作用:
  • 扩展接口,以前接口只能有抽象方法,使用default后接口也可以拥有带有方法体的方法了(default修饰的方法必须带方法体)
  1. interface Writer {
  2. public int write(String content);
  3. /**
  4. * 默认接品不能有方法体
  5. */
  6. /*public byte[] getBytes(String content) {
  7. return content.getBytes();
  8. }*/
  9. public default byte[] getBytes(String content) {
  10. return content.getBytes();
  11. }
  12. }