如何超级关键词访问父类成员? [英] How super key word access parent class members?
问题描述
第一课
{
int a = 100;
int b = 200;
void get_data1 (int p,int q)
{
a = p;
b = q;
}
}
class two延伸一个
{
String x;
String y;
One O;
void get_data(String x,String y,One q)
{
this.x = x;
this.y = y;
O = q;
}
无效显示()
{< br $>
System.out.println(A =======+ x);
System.out.println( B =======+ y);
System.out.println(Oa =====+ Oa);
System.out .println(Ob =====+ Ob);
System.out.println(Super.a =+ super.a);
系统.out.println(Super.b =+ super.b);
}
}
class super_key
{
public static void main(String [] args)
{
一个obj1 = new One();
两个obj2 = new两个();
obj1.get_data1(1,2);
obj2.get_data(A,B,obj1);
obj2.display();
}
}
我尝试了什么:
它打印一个= 100& b = 200使用超级关键字虽然我已将它们初始化为a = 1& b = 2到get_data1()函数
class One
{
int a=100;
int b=200;
void get_data1(int p,int q)
{
a=p;
b=q;
}
}
class Two extends One
{
String x;
String y;
One O;
void get_data(String x,String y,One q)
{
this.x=x;
this.y=y;
O=q;
}
void display()
{
System.out.println("A======="+x);
System.out.println("B======="+y);
System.out.println("O.a====="+O.a);
System.out.println("O.b====="+O.b);
System.out.println("Super.a="+super.a);
System.out.println("Super.b="+super.b);
}
}
class super_key
{
public static void main(String[] args)
{
One obj1=new One();
Two obj2=new Two();
obj1.get_data1(1,2);
obj2.get_data("A","B",obj1);
obj2.display();
}
}
What I have tried:
It prints a=100 & b=200 using super key word though I have initialize them as a=1 & b=2 through get_data1() function
推荐答案
虽然类两个
扩展了类一个
您的对象完全独立。obj2
中类One
的属性与类One <1的属性不同/ code> in
obj1
。
Although the classTwo
extends classOne
your objects are totally independent. The properties of classOne
inobj2
are not the same as the properties of classOne
inobj1
.
这篇关于如何超级关键词访问父类成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!