6. this代表当前对象 如果使用本类的变量和方法,在其前面隐含 着this class X{ int x; void show1... void method(){ x=3; /相当于this.x=3; showO; /相当于this.show0;
6-16 Programming in Java • this代表当前对象 ¾ 如果使用本类的变量和方法,在其前面隐含 着this “this”(1) class X { int x; void show() {…} void method() { x = 3; //相当于this.x = 3; show(); //相当于this.show();
this是一个对象的若干个引用之一,利用this可以调 用当前对象的方法或使用当前对象的域 Example class Moose{ String hairdresser Moose(String hairdresser){ this.hairdresserhairdresser; field parameter
6-17 Programming in Java “this”(2) •this是一个对象的若干个引用之一,利用this可以调 用当前对象的方法或使用当前对象的域 • Example class Moose { String hairdresser ; Moose(String hairdresser) { this.hairdresser = hairdresser; } } field parameter
supper >表示当前对象的直接父类对象,是当前对象 的直接父类对象的引用 使用 >用来访问父类被隐藏的成员变量 super.variable >用来调用父类中被重写的方法 super.method ([paramlist]); >用来调用父类的构造方法 super ([paramlist]);
6-18 Programming in Java • supper ¾ 表示当前对象的直接父类对象,是当前对象 的直接父类对象的引用 “supper” • 使用 ¾ 用来访问父类被隐藏的成员变量 super.variable ¾ 用来调用父类中被重写的方法 super.method([paramlist]); ¾ 用来调用父类的构造方法 super ([paramlist]);
Example 6 class D200 Card extends Number PhoneCard double balance; double additoryFee; boolean performDialO) if (balance >(0.5+additoryFee)) { balance -=(0.5+additoryFee); return true; else reurn false; double getBalanceO{ return super.balance;
6-19 Programming in Java Example class D200_Card extends Number_PhoneCard { double balance; double additoryFee; boolean performDial() { if (balance >(0.5+additoryFee)) { balance -= (0.5 + additoryFee); return true; } else reurn false; } } double getBalance() { return super.balance;} }
同一个类中存在着多个具有不同参数列表的构造函数 D200Card0{}/没有形参的构造函数 D200_Card(long cn){∥一个参数的构造函数 thisO); cardNumber cn; D200_Card(long cn,int pw){/两个参数的构造函数 this(cn); password pw; D200_Card(long cn,int pw,double b,String c){/四个参数 this(cn,pw,b); connectNumber c;
6-20 Programming in Java • 同一个类中存在着 多个具有不同参数列表的构造函数 构造函数的重载 D200_Card() { } //没有形参的构造函数 D200_Card(long cn) { //一个参数的构造函数 cardNumber = cn; } D200_Card(long cn, int pw) { //两个参数的构造函数 cardNumber = cn; password = pw; } D200_Card(long cn, int pw, double b, String c) { //四个参数 cardNumber = cn; password = pw; balance = b; connectNumber = c; } this(); this(cn); this(cn, pw, b);