public int setNum1(int num1) {
this.num1 = num1;
return num1;
}
public int setNum2 (int num2) {
this.num2 = num2;
return num2;
}
}
class Day06Test1 {
public static void main(String[] args) {
Computer c = new Computer();
int num1 = c.setNum1(34);
int num2 = c.setNum2(33);
c.printSum(num1,num2);
}
}
作者: 殇_心。 时间: 2013-4-26 21:00
测试代码如下:
class Computer
{
private int num1;
private int num2;
Computer( int num1, int num2 )
{
this.num1 = num1;
this.num2 = num2;
}
public void printSum()
{
int sum = num1 + num2;
System.out.println("sum="+sum);
}
}
class Test
{
public static void main(String[] args)
{
Computer com = new Computer(3,4);//创建对象
com.printSum();//调用函数,输出和。
}
}作者: lipingan0520 时间: 2013-4-26 21:02
class Computer
{
private int num1,num2; //定义2个变量
Computer(int num1,int num2) //初始化变量
{
this.num1 = num1;
this.num2 = num2;
}
public void printSum() //输出
{
System.out.println("Sum="+(num1+num2));
}
}
class Demo110
{
public static void main(String[] args)
{
Computer com = new Computer(2,3); //创建对象,传入对应的构造参数