this 和 super
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
| 9 | publicstaticvoidmain(String[]args){ |
|---|---|
| 10 | newApple(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() ᶳනࣁ᭜ොဩጱᒫӞᤈ҅ڞވᖫᦲӧ᭗ᬦ

֦ইຎ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 卷
- 1Java 概述
- 2Java 的特点
- 3Java 开发环境
- 4JRE
- 5基础语法
- 6Java 执行控制流程
- 7循环语句
- 8for 循环语句
- 9continue 语句
- 10对象的创建
- 11构造方法
- 12方法重载
- 13方法的重写
- 14初始化
- 15初始化顺序
- 16对象的销毁
- 17this 和 super
- 18访问控制权限
- 19多态
- 20组合
- 21代理
- 22static
- 23接口
- 24异常
- 25常见的 Exception
- 26与 Exception 有关的 Java 关键字
- 27什么是 Error
- 28内部类
- 29创建内部类
- 30Iterable 接口
- 31顶层接口
- 32LinkedList 类
- 33PriorityQueue
- 34HashMap
- 35WeakHashMap 类
- 36集合实现类特征图
- 37泛型的使用
- 38用泛型表示接口
- 39Class 类
- 40Field 类
- 41Method 类
- 42枚举特性
- 43枚举和普通类一样
- 44枚举神秘之处
- 45枚举类
- 46I/O
- 47File 类
- 48基础 IO 类和相关方法
- 49OutputStream
- 50Writer 类
- 51OutputStream 及其子类
- 52注解
- 53关于 null 的几种处理方式
- 54null 是任何引用类型的初始值
- 55使用 Null-Safe 方法
- 56关于思维导图
- 57Java.math
- 58Java.net