← 返回目录

this 和 super

第 17 / 58 卷日期:

4int b = 12;

5} 6}

a ݒᰁտࣁӷӻ {} ֢ አऒٖํප҅ᘒb ݒᰁጱ꧊ݝᚆࣁਙᛔ૩ጱ {} ٖํප̶

ᡱᆐਂࣁ֢አऒ҅֕ฎӧ꧋ᦜᬯ໏ٟ

1{ 2int x = 11;

3{ 4int x = 12;

5} 6}

ᬯᐿٟဩࣁC/C++ Ӿฎݢզጱ҅֕ฎࣁJava Ӿӧ꧋ᦜᬯ໏ٟ҅ࢩԅJava ᦡᦇᘏᦊԅᬯ໏ٟտ੕ᛘᑕ ଧႰԤ̶

this ޾ super

this ޾ super ᮷ฎ Java ӾጱىᲫਁ

this ᤒᐏጱ୮ڹ੒᨝҅this ݢզ᧣አොဩ̵᧣አં௔޾೰ݻ੒᨝๜᫝̶this ࣁ Java ӾጱֵአӞᛱํӣ ᐿғ೰ݻ୮ڹ੒᨝

1public class Apple { 2

3int i = 0;

4 5Apple eatApple(){

6i++;

7return this;

8} 9

10public static void main(String[] args) { 11Apple apple = new Apple();

12apple.eatApple().eatApple();

13} 14}

ᬯྦྷդᎱྲ᫾ᔜও҅ᔜওࣁޫߺ҅౯ӞӻeatApple()ොဩᒌᆐݢզ᧣አग़ེ֦҅ࣁݸᶎᬮݢզᖀᖅ᧣
አ҅ᬯ੪உᐟ॰ԧ҅ԅޫࠨҘٌਫ੪ฎthisࣁ֢ᐠԧ҅౯ࣁ eatAppleොဩӾےԧӞӻreturn
thisጱᬬࢧ꧊҅Ԟ੪ฎ᧔ߺӻ੒᨝᧣አeatAppleොဩ᮷ᚆᬬࢧ੒᨝ጱᛔ᫝̶
thisᬮݢզץ᷶ં௔๋҅ଉᥠጱ੪ฎࣁ຅᭜ොဩӾֵአthis ҅ইӥಅᐏ

4 5public Apple(int num){

6this.num = num;

7} 8

9publicstaticvoidmain(String[]args){
10newApple(10);
11}
12}
mainොဩӾփ᭓ԧӞӻint꧊ԅ10ጱ݇හ҅ਙᤒᐏጱ੪ฎ᝘ຎጱහᰁ҅ଚ಩ᬯӻහᰁᩙᕳԧnumق
ੴݒᰁ̶ಅզnumጱ꧊ሿࣁ੪ฎ10̶

this ᬮݢզ޾຅᭜ڍහӞֵ᩸አ҅꧌୮ӞӻقੴىᲫਁጱපຎ

1public class Apple { 2

3private int num;

4private String color;

5 6public Apple(int num){

7this(num,"ᕁᜋ");

8} 9

10public Apple(String color){ 11this(1,color);

12} 13

14public Apple(int num, String color) { 15this.num = num;

16this.color = color;

17} 18

19}

֦տݎሿӤᶎᬯྦྷդᎱֵአጱӧฎthis, ᘒฎ this(݇හ) ̶ਙፘ୮ԭ᧣አԧٌ՜຅᭜ොဩ҅ᆐݸփ᭓ ݇හᬰ̶݄ᬯ᯾ဳ఺Ӟᅩғthis() ஠ᶳනࣁ຅᭜ොဩጱᒫӞᤈ҅ڞވᖫᦲӧ᭗ᬦ

图 27-1
图 27-1

಩֦ইຎthis ቘᥴԅ೰ݻᛔ᫝ጱӞӻ୚አ҅ᮎԍsuper ੪ฎ೰ݻᆿᔄጱӞӻ୚አ̶super ޾Ძਁىthis አֵզݢ֦Ӟ໏҅super.੒᨝ ๶୚አᆿᔄጱ౮ާ҅ইӥ

1public class Fruit { 2

3int num;

4String color;

5 6public void eat(){

7System.out.println("eat Fruit");

8} 9}

10 11public class Apple extends Fruit{

12 13@Override

14public void eat() { 15super.num = 10;

16System.out.println("eat " + num + " Apple");

17} 18

19}

֦አֵզݢԞsuper(݇හ)๶᧣አᆿᔄጱ຅᭜ڍහ҅ᬯ᯾ӧٚԈֺৼԧ̶
ӥᶎԅ֦࿤௛ԧthis޾ᲫਁىsuperىᲫਁጱྲ᫾̶
全站分卷索引 · 58 卷
  1. 1Java 概述
  2. 2Java 的特点
  3. 3Java 开发环境
  4. 4JRE
  5. 5基础语法
  6. 6Java 执行控制流程
  7. 7循环语句
  8. 8for 循环语句
  9. 9continue 语句
  10. 10对象的创建
  11. 11构造方法
  12. 12方法重载
  13. 13方法的重写
  14. 14初始化
  15. 15初始化顺序
  16. 16对象的销毁
  17. 17this 和 super
  18. 18访问控制权限
  19. 19多态
  20. 20组合
  21. 21代理
  22. 22static
  23. 23接口
  24. 24异常
  25. 25常见的 Exception
  26. 26与 Exception 有关的 Java 关键字
  27. 27什么是 Error
  28. 28内部类
  29. 29创建内部类
  30. 30Iterable 接口
  31. 31顶层接口
  32. 32LinkedList 类
  33. 33PriorityQueue
  34. 34HashMap
  35. 35WeakHashMap 类
  36. 36集合实现类特征图
  37. 37泛型的使用
  38. 38用泛型表示接口
  39. 39Class 类
  40. 40Field 类
  41. 41Method 类
  42. 42枚举特性
  43. 43枚举和普通类一样
  44. 44枚举神秘之处
  45. 45枚举类
  46. 46I/O
  47. 47File 类
  48. 48基础 IO 类和相关方法
  49. 49OutputStream
  50. 50Writer 类
  51. 51OutputStream 及其子类
  52. 52注解
  53. 53关于 null 的几种处理方式
  54. 54null 是任何引用类型的初始值
  55. 55使用 Null-Safe 方法
  56. 56关于思维导图
  57. 57Java.math
  58. 58Java.net