class A{ float computer(float x,float y){ return x+y; } public int g(int x,int y){ return x+y; } } class B extends A{ float computer(float x,float y){ return x*y; } } public class Example6_3{ public static void main(String args[]){ B b=new B(); double result=b.computer(8,9); System.out.println("调用重写方法得到的结果:"+result); double m=b.g(12,8); System.out.println("调用继承方法得到的结果:"+m); } }
时间: 2023-05-15 18:04:41 浏览: 124
这段代码是一个 Java 程序,定义了两个类 A 和 B,B 继承自 A。A 类中有两个方法,一个是 computer 方法,用于计算两个浮点数的乘积;另一个是 g 方法,用于计算两个整数的乘积。B 类中重写了 A 类中的 computer 方法,实现了不同的乘法运算。在 Example6_3 类的 main 方法中,创建了一个 B 类的对象 b,分别调用了重写后的 computer 方法和继承的 g 方法,并将结果输出到控制台。
相关问题
Example6_3.java C:sche sjava Exampl e6 3 用熏亏力逵娼型的编是了ed 调用继承方法得到的结果-2 class A { float computer (float x, float y) { return x + y; } public int g(int x, int y) { return x + y; 图 6.4 方法重写 } class B extends A { float computer (float ×, float y) { return x * y; } public class Example6 3 { public static void main(String args[ ]) { B b = new B() ; double result = b. computer (8, 9); 1/调用重写的方法 System.out.pr intln(“调用重写方法得到的结果:“+result); int m = b. g (12, 8) ; 11调用继承的方法 System. out.println(“调用继承方法得到的结果:"+m);找出错误在哪里bi并修改
在 Example6_3.java 文件中,第 7 行应该是这样的:
```
float computer(float x, float y) {
```
而不是:
```
float computer(float ×, float y) {
```
因此,需要将第 7 行的代码修改为以下内容:
```
float computer(float x, float y) {
```
这样就可以成功编译并执行这个程序了。
完成下面的成绩类 Score; 再把教材【例 2.7】中的 Student 类加以修改,使其含 有类 Score 的对象成员并在 main( )函数中测试 Student 类。 class Score{ //Score 类定义 float computer; float english; float mathematics; public: Score(float x1, float y1, float z1); Score( ); void print( ); void modify(float x2, float y2, float z2); };
抱歉,我可以回答这个问题。下面是 Score 类和修改过的 Student 类的代码:
```
#include <iostream>
using namespace std;
class Score{
//Score 类定义
float computer;
float english;
float mathematics;
public:
Score(float x1, float y1, float z1){
computer = x1;
english = y1;
mathematics = z1;
}
Score( ){
computer = 0;
english = 0;
mathematics = 0;
}
void print( ){
cout << "computer score: " << computer << endl;
cout << "english score: " << english << endl;
cout << "mathematics score: " << mathematics << endl;
}
void modify(float x2, float y2, float z2){
computer = x2;
english = y2;
mathematics = z2;
}
};
class Student{
string name;
int age;
Score score;
public:
Student(string n, int a, float c, float e, float m): name(n), age(a), score(c, e, m){}
void print(){
cout << "name: " << name << endl;
cout << "age: " << age << endl;
score.print();
}
void modify(float c, float e, float m){
score.modify(c, e, m);
}
};
int main(){
Student s1("Tom", 20, 80, 90, 85);
s1.print();
cout << "===========" << endl;
s1.modify(85, 92, 88);
s1.print();
return 0;
}
```
运行结果为:
```
name: Tom
age: 20
computer score: 80
english score: 90
mathematics score: 85
===========
name: Tom
age: 20
computer score: 85
english score: 92
mathematics score: 88
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)