#include <iostream>
using namespace std;
class User
{
private:
int m_id;
public:
User(int id=0){m_id = id;}
void setId(int id){m_id = id;}
int getId(){return m_id;}
};
void test0(User t){//按值传参
User s;
t = s;
t.setId(1002);
cout << "test1:" << t.getId() << endl;
}
void test1(User *t){//按值传参
t = new User();//指针指向了一个新对象,外面实参没变
t->setId(1002);
cout << "test1:" << t->getId() << endl;
}
void test2(User* & t){//按引用传参
t = new User();//指针指向了一个新对象,外面实参也跟着变了
t->setId(1002);
cout << "test2:" << t->getId() << endl;
}
int main(int argc, char const *argv[]) {
cout<< "npass by ref:"<<endl;
User* t = new User();
t->setId(1001);
cout << t->getId() << endl;
test2(t);
cout << t->getId() << endl;
cout<< "npass by value:"<<endl;
t = new User();
t->setId(1001);
cout << t->getId() << endl;
test1(t);
cout << t->getId() << endl;
return 0;
}
pass by ref: 1001 test2:1002 1002 pass by value: 1001 test1:1002 1001
package com.pollyduan.bean;
@Data
public class User {
private Integer id;
public static void testObject(User t){
t=new User();//指向了一个新对象,外面实参没变
t.setId(1002);
System.out.println("testObject="+t);
}
@Test
public void testObject(){
User user=new User();
user.setId(1001);
System.out.println("user="+user);
testObject(user);
System.out.println("user="+user);
}
}
user=User(id=1001) testObject=User(id=1002) user=User(id=1001)
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有