在面向对象编程中,“调用(访问)超类变量”通常指在子类中访问从父类(超类)继承来的成员变量。不同语言写法不同,下面以常见语言为例说明。
super(用于区分子类和父类同名字段)class Parent {
int x = 10;
}
class Child extends Parent {
int x = 20;
void show() {
System.out.println(x); // 子类变量:20
System.out.println(super.x); // 超类变量:10
}
}
class Child extends Parent {
void show() {
System.out.println(x); // 直接访问继承的父类变量
}
}
super()(推荐)class Parent:
x = 10
class Child(Parent):
x = 20
def show(self):
print(self.x) # 20
print(super().x) # 10
print(Parent.x)
class Parent {
public:
int x = 10;
};
class Child : public Parent {
public:
int x = 20;
void show() {
cout << x << endl; // 子类
cout << Parent::x << endl; // 超类
}
};
getter/setter类名.变量名 访问super 或 父类名如果你指的是某种具体语言或开发框架(如 Kotlin、C#、Unity、Spring),可以告诉我,我可以给更精准的示例。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。