- 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); //创建对象,传入对应的构造参数
- com.printSum(); //调用方法输出
- }
- }
复制代码 |