看下面给出的代码, 当方法形参名与成员变量名相同 ;但我们没有手动通过this引用去调用成员变量;
看执行结果可以发现此时并没有完成对于成员变量的赋值,因为在方法中遵循局部变量优先的原则,就不会去访问成员变量,只是形参自己给自己赋值而已
public class Date { public int year; public int month; public int day; public void setDay(int year, int month, int day){ year = year; month = month; day = day; } public void printDate(){ System.out.println(year + "/" + month + "/" + day); } public static void main(String[] args) { // 构造三个日期类型的对象 d1 d2 d3 Date d1 = new Date(); Date d2 = new Date(); Date d3 = new Date(); // 对d1,d2,d3的日期设置 d1.setDay(2022,8,15); d2.setDay(2022,8,16); d3.setDay(2022,8,17); // 打印日期中的内容 d1.printDate(); d2.printDate(); d3.printDate(); }}执行结果:
要解决这里的问题只需要通过this去访问成员变量即可,此时就可以区分形参和成员变量了
public void setDay(int year, int month, int day){ this.year = year; this.month = month; this.day = day;}执行结果:
由于设计不好,或者因场景需要,子类和父类中可能会存在相同名称的成员,如果要在子类方法中访问父类同名成 员时,该如何操作?
直接访问是无法做到的,因为局部优先的的原则,会直接访问子类的成员。
Java提供了 super关键字,该关键字主要作用:在子类方法中访问父类的成员。
public class Base { int a; int b; public void methodA(){ System.out.println("Base中的methodA()"); } public void methodB(){ System.out.println("Base中的methodB()"); }}class Derived extends Base{ int a; // 与父类中成员变量同名且类型相同 char b; // 与父类中成员变量同名但类型不同 // 与父类中methodA()构成重载 public void methodA(int a) { System.out.println("Derived中的method()方法"); } // 与基类中methodB()构成重写 public void methodB(){ System.out.println("Derived中的methodB()方法"); } public void methodC(){ // 对于同名的成员变量,直接访问时,访问的都是子类的 a = 100; // 等价于: this.a = 100; b = 101; // 等价于: this.b = 101;// 注意:this是当前对象的引用// 访问父类的成员变量时,需要借助super关键字// super是获取到子类对象中从基类继承下来的部分 super.a = 200; super.b = 201;// 父类和子类中构成重载的方法,直接可以通过参数列表区分清访问父类还是子类方法 methodA(); // 没有传参,访问父类中的methodA() methodA(20); // 传递int参数,访问子类中的methodA(int)// 如果在子类中要访问重写的基类方法,则需要借助super关键字 methodB(); // 直接访问,则永远访问到的都是子类中的methodA(),基类的无法访问到 super.methodB(); //访问基类的methodB() }}根据代码块定义的位置以及关键字,又可分为以下四种:
普通代码块:定义在方法中的代码块, 其内的变量只在代码块中有效,这种用法较少见。
public class Main{ public static void main(String[] args) { { //直接使用{ }定义,普通方法块 int x = 10 ; System.out.println("x1 = " +x); } int x = 100 ; System.out.println("x2 = " +x); }}也叫: 实例代码块 。构造代码块一般用于初始化实例成员变量。
public class Student{ //实例成员变量 private String name; private String gender; private int age; //实例代码块 { this.name = "xinxin"; this.age = 21; this.gander = "nan"; System.out.println("I am instance init()!"); } public void show(){ System.out.println("name: "+name+" age: "+age+" gender: "+gender); }}public class Main { public static void main(String[] args) { Student stu = new Student(); stu.show(); }}使用static定义的代码块称为静态代码块。一般用于初始化静态成员变量。
public class Student{ private String name; private int age; private static String classRoom; //实例代码块 { this.name = "xin"; this.age = 21; System.out.println("I am instance init()!"); } // 静态代码块 static { classRoom = "rj2104"; System.out.println("I am static init()!"); } public static void main(String[] args) { Student s1 = new Student(); Student s2 = new Student(); }}记得点赞+评论以及转发!!!
| 留言与评论(共有 0 条评论) “” |