作为参数的类的对象。 [英] Object of a class as a parameter.
本文介绍了作为参数的类的对象。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
import java.io.*;
class Complex
{
int a, b;
BufferedReader br;
void accept()
{
br = new BufferedReader (new InputStreamReader(System.in));
try
{
System.out.println("Enter two numbers");
int a= Integer.parseInt(br.readLine());
int b= Integer.parseInt(br.readLine());
}
catch (IOException ioe)
{
}
}
void display()
{
System.out.println("Number 1 = " + a);
System.out.println("Number 2 = " + b);
}
void add(Complex c1 , Complex c2)
{
a=c1.a+c2.a;
b=c1.a+c2.a;
}
public static void main(String [] args)
{
Complex c1 = new Complex();
Complex res = new Complex();
Complex c2 = new Complex();
c1.accept();
c2.accept();
res.add(c1,c2);
res.display();
}
}
此代码正在编译并正在运行。但是输出是不可取的。
添加2个数字不会被打印。
当我调用res.display时,打印的内容是
小时= 0
分钟= 0
秒= 0
有人可以告诉我原因吗?
我尝试过:
请帮我解决这个问题。
谢谢
This code is getting compiled and its running. But the output is not desired .
The addition of the 2 numbers is not getting printed.
when i call res.display the contents which is getting printed is
hour=0
minutes=0
seconds=0
Can anyone tell me why?
What I have tried:
Please help me resolve this.
Thanks
推荐答案
是的,因为你从未在对象中为a和b设置任何值,你只需在accept方法中设置本地值。
并且您不会通过在此处针对您所犯的每个基本错误发布问题来学习Java。我建议你去 Java™教程 [ ^ ]并以结构合理的方式学习。
Yes, because you never set any values for a and b in your objects, you just set local values in the accept method.
And you are not going to learn Java by posting questions here for every basic mistake you make. I suggest you go to The Java™ Tutorials[^] and learn it in a properly structured way.
这篇关于作为参数的类的对象。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文