Да, мы можем передать объект как параметр в Программе Java.
Первый путь
class demo
{private int length=1;
private int breadth=1;
private int area;
void input(int length,int breadth)
{
this.length=length;
this.breadth=breadth;
}
void add(demo d1,demo d2)
{
length=d1.length+d2.length;
breadth=d1.breadth+d2.breadth;
}
void output()
{
System.out.println("\nLength="+length+"\nBreadth="+breadth);
}
public static void main(String args[])
{
demo d1=new demo();
demo d2=new demo();
d1.input(1, 1);
d1.output();
d2.input(2, 2);
d2.output();
demo d3=new demo();
d3.add(d1, d2);
d3.output();
}
}
Второй путь
</p>
<code> class demo
{private int length;
private int breadth;
void input(int length,int breadth)
{
this.length=length;
this.breadth=breadth;
}
demo add(demo d2)
{ demo obj=new demo();//Neccesary as we want to return complete
object irrespective of its number of data fields
obj.length=length+d2.length;//Storing the length of two objects
in the obj
obj.breadth=breadth+d2.breadth;
return obj;
}
void output()
{
System.out.println("\nLength="+length+"\nBreadth="+breadth);
}
public static void main(String args[])
{
demo d1=new demo();
demo d2=new demo();
d1.input(1, 1);
d2.input(2, 2);
demo d3=d1.add(d2);//Here we created new object d3 called
add() by d1 and passed d2 object through it
d3.output();
}
}
</code>