class Sa{ int x,y; public void setXy(int x,int y) { this.x=x; this.y=y; } public int result() { return x+y; } } class Sb extends Sa{ int x; public int result(){ super.x=5; x=super.result()/x; return x; } } class Sc { public static void main(String aa[]){ Sa s=new Sa(); s.setXy(5,10); System.out.println("sa result:"+s.result()); Sb s2=new Sb(); s2.x=3; s2.y=10; System.out.println("sb result:"+s2.result()); }}
时间: 2023-07-16 17:11:36 浏览: 75
这段代码定义了三个类:Sa、Sb、Sc。
Sa类有两个成员变量x、y和两个方法setXy和result。setXy方法用于设置x、y的值,result方法返回x+y的结果。
Sb类继承了Sa类,新增了一个成员变量x和一个result方法。result方法重写了Sa类的result方法,首先通过super关键字调用父类的result方法,然后给父类的成员变量x赋值为5。接着将父类的result方法除以子类的成员变量x,最后返回计算结果。
Sc类是程序的入口,创建了一个Sa类对象和一个Sb类对象,并分别调用它们的result方法输出结果。
当执行Sc类的main方法时,输出的结果为:
sa result:15
sb result:1
Sa类的result方法返回了5+10=15,而Sb类的result方法返回了5/3=1。
相关问题
4. 修改以下程序,使其可以成功运行 #include<iostream.h> class base{ int x; public: void setx(int n){ x=n;} void showx( ){ cout<<x<<endl;} }; class derived:private base{ private: int y; public: void setxy(int n,int m) { setx(n); y=m; } void showxy( ){ cout<<x<<endl; cout<<y<<endl; } }; Int main() { derived obj; obj.setx(10); obj.showx(); obj.setxy(10,20); obj.showxy( ); return 0; }
修改后的程序如下:
```cpp
#include<iostream>
using namespace std;
class base{
int x;
public:
void setx(int n){ x=n;}
void showx( ){ cout<<x<<endl;}
};
class derived:private base{
int y;
public:
void setxy(int n,int m){ setx(n); y=m; }
void showxy( ){ cout<<base::x<<endl; cout<<y<<endl; }
};
int main() {
derived obj;
obj.setxy(10,20);
obj.showxy();
return 0;
}
```
在原程序中,由于类derived私有继承了类base,因此在main函数中不能访问base类的public成员函数setx和showx。解决方法是,将obj.setx(10);和obj.showx();改为obj.setxy(10,20);和obj.showxy();。同时,在showxy函数中,由于x是base类的private成员,不能直接访问,需要用base::x来访问。
改进代码:#include<iostream> using namespace std; class base { private: int x; public: void setx(int n) { x = n; } void showx() { cout<<x<<endl; } }; class derived:private base { private: int y; public: void setxy(int n, int m) { setx(n); y = m; } void showxy() { cout<<x<<endl; cout<<y<<endl; } } int main() { derived obj; obj.setx(10); obj.showx(); obj.setxy(20, 30); obj.showxy(); return 0; }
以下是改进后的代码:
```
#include<iostream>
using namespace std;
class base {
private:
int x;
public:
void setx(int n)
{
x = n;
}
void showx()
{
cout<<x<<endl;
}
};
class derived:private base {
private:
int y;
public:
void setxy(int n, int m)
{
setx(n);
y = m;
}
void showxy()
{
showx();
cout<<y<<endl;
}
};
int main() {
derived obj;
obj.setx(10);
obj.showx();
obj.setxy(20, 30);
obj.showxy();
return 0;
}
```
改进的内容如下:
1.在 derived 类的 showxy 函数中,使用 showx 函数来输出 x 的值,而不是直接访问 x。因为在 derived 类中,x 是私有的,不能直接访问。
2.将 showx 函数的调用从 main 函数中移动到 showxy 函数中,以避免代码重复。
3.修复了代码中的分号错误。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)