如何超级关键词访问父类成员? [英] How super key word access parent class members?

查看:100
本文介绍了如何超级关键词访问父类成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一课

{

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 class Two extends class One your objects are totally independent. The properties of class One in obj2 are not the same as the properties of class One in obj1.


这篇关于如何超级关键词访问父类成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆