我看到了一个试图用Java解释继承的例子.Employee类是基类,它有三个实例变量和三个构造函数.它如下:
public class Employee{
private String name;
private int id;
public Employee(){
name = " No Name!";
id = 00100;
}
public Employee(String n, int i){
name = n;
id = i;
}
public Employee (Employee originalObject){
name = originalObject.name;
id = originalObject.id;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:第三个构造函数有什么意义?以及它如何接受Employee我们仍在处理的类的相同类型的参数?该计划已空的构造函数和另外一个,对于经过串name并int为id,为什么有一个额外的一个并不比前面两个构造得多?
我有一个复制结构内容的问题..以下情况......我的类Session中有一个struct arg_struct:
struct Session::arg_struct
{
const char* targetFilePath;
const char* url;
unsigned int thread_id;
Session::ThreadFinishedCallbackFunction callback;
};
Run Code Online (Sandbox Code Playgroud)
在我的一个方法中,我启动一个线程并将结构赋予将要执行的函数:
{
...
arg_struct args;
args.targetFilePath = targetFilePath;
args.url = req_url;
args.thread_id = ++mThread_id;
args.callback = callback;
curl_global_init(CURL_GLOBAL_ALL);
error = pthread_create(&thread,NULL,download,&args);
}
Run Code Online (Sandbox Code Playgroud)
现在将执行下载功能:
void* download(void* arguments)
{
Session::arg_struct ar = *(Session::arg_struct*) arguments;
Session::arg_struct args;
args.targetFilePath = new char[strlen(ar.targetFilePath)];
args.url = new char[strlen(ar.url)];
strcpy(const_cast<char*>(args.targetFilePath),ar.targetFilePath);
strcpy(const_cast<char*>(args.url),ar.url);
args.callback = ar.callback;
args.thread_id = ar.thread_id;
cout << "copied" << endl;
CURL *curl;
FILE* datafile;
datafile = fopen(args.targetFilePath, "w"); …Run Code Online (Sandbox Code Playgroud) 关于python深度复制和浅拷贝的问题.
帖子在 深拷贝和浅拷贝之间有什么区别?
无法帮助我.
为什么1的总和是6而不是10?
kvps = { '1' : 1, '2' : 2 }
theCopy = kvps.copy() # both point to the same mem location ?
kvps['1'] = 5
sum = kvps['1'] + theCopy['1']
print sum
Run Code Online (Sandbox Code Playgroud)
输出和是6
aList = [1,2]
bList = [3,4]
kvps = { '1' : aList, '2' : bList }
theCopy = kvps.copy() # both point to the same mem location ?
kvps['1'][0] = 5
sum = kvps['1'][0] + theCopy['1'][0]
print sum
Run Code Online (Sandbox Code Playgroud)
输出总和是10
public void MethodSample1(Itest variable)
{
variable.TestString = "some sample data";
Itest var1 = variable;
Console.WriteLine(variable.TestString);
MethodSample2(variable);
Console.WriteLine(variable.TestString);
Console.WriteLine(var1.TestString);
}
public void MethodSample2(Itest variable)
{
variable.TestString = "testdata";
}
public interface Itest
{
string TestString { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
预计两个控制台输出行都打印"一些样本数据",但似乎TestString是被新值覆盖了?是不是"默认情况下在C#中所有的值都按值传递?".
总之,如何保存"TestString"的值MethodSample1?
(我遇到了这个问题,因为我的所有项目都基于单个界面)
即使保留了价值,它确实反映了!奇怪!
在开始之前,我知道还有其他几个问题可以"回答"这个问题,但不是我能够理解的方式.这就是我所指的: C#中的深度复制
我做了一些研究,我希望有人可以向我解释如何将对象设置为另一个对象的值.
例如,让我们说:
myObject bob;
bob = new myObject();
myObject joe
joe = bob
Run Code Online (Sandbox Code Playgroud)
从我可以研究的,joe现在指向鲍勃的实例bob.这些物体是否仍然独立运作?
我如何创建两个对象,将一个对象的内容复制到另一个对象,并将它们作为两个单独的对象,其字段,方法和事件分别发生?
我很抱歉再次提出这个问题,但我似乎找不到其他地方对我有意义的解释.
我需要重复列表的每个元素N次,即执行这种转换:
(1 2 3) => (1 1 1 2 2 2 3 3 3) ; N = 3
Run Code Online (Sandbox Code Playgroud)
保持元素的顺序很重要,即第一个元素应该重复N次,然后是第二个,等等.
到目前为止,这是我最好的尝试:
(defun my-fnc (lst &optional (n 2))
(mapcan (lambda (x) (make-list n :initial-element x))
lst))
Run Code Online (Sandbox Code Playgroud)
看起来很有效:
CL-USER> (defparameter *foo* '("foo" "bar"))
*FOO*
CL-USER> (setf *foo* (my-fnc *foo* 3))
("foo" "foo" "foo" "bar" "bar" "bar")
Run Code Online (Sandbox Code Playgroud)
......但不完全.问题是前三个元素是对同一个对象的引用.
("foo" "foo" "foo" "bar" "bar" "bar")
;{---------------} {---------------}
; the same string the same string
Run Code Online (Sandbox Code Playgroud)
这不是我想要的.
所以我的问题是:如何以大多数惯用的方式解决问题,以便结果列表的每个元素都可以引用复制的单独对象.
我注意到调用 .map() 而不将其分配给变量会使其返回整个数组,而不仅仅是更改的属性:
const employees = [{
name: "John Doe",
age: 41,
occupation: "NYPD",
killCount: 32,
},
{
name: "Sarah Smith",
age: 26,
occupation: "LAPD",
killCount: 12,
},
{
name: "Robert Downey Jr.",
age: 48,
occupation: "Iron Man",
killCount: 653,
},
]
const workers = employees.concat();
workers.map(employee =>
employee.occupation == "Iron Man" ? employee.occupation = "Philantropist" : employee.occupation
);
console.log(employees);Run Code Online (Sandbox Code Playgroud)
但考虑到 .concat() 创建了原始数组的副本并将其分配给工人,为什么雇员也会发生变异?
vector< MyObject<MyType> > ObjectList(100, MyObject<MyType>(param1));
Run Code Online (Sandbox Code Playgroud)
MyObject在内部创建了一个名为"storage"的成员,它是一个使用堆上的MyType数组.
但是使用上面的代码行,ObjectList中的每个项目都有"存储"指向相同的内存位置(基本上共享存储).
当我使用手动分配堆栈上的列表时,不会发生此问题
MyObject<MyType> ObjectList[100] = { MyObject<MyType>(param1),
MyObject<MyType>(param1), ...};
Run Code Online (Sandbox Code Playgroud)
当我用上面的行声明MyObject时,每个存储都有自己的内存位置.
在我的代码中有 operator+ 重载。在这个范围内,我定义了 object ans,我想构建并返回它,但似乎析构函数ans在我可以返回它之前就进行了破坏,所以这个方法返回了一些未定义的值。
我不明白我错在哪里?它是析构函数、构建器还是在我的 operator+ 重载中?
这是我的代码:
class vector1{
int * arr;
int size;
public:
//constructors
vector1(){}
vector1(int n){
size=n;
arr= new int[size];
}
//functions
int get_size(){return size;}
void init(){ //initialize all array cells to 0
for(int i=0;i<size;i++)
arr[i]=0;
}
int get_value_in_index(int index){
return arr[index];
}
void set_value_in_index(int i, int num){
arr[i]=num;
}
int & operator[](int i){
int default_val=0;
if (i<0 || i>size){
cout<<"index not valid"<<endl;
return default_val;
}
return arr[i];
}
vector1 operator+(vector1 & …Run Code Online (Sandbox Code Playgroud) 我实现了这里描述的复制构造函数.但问题仍然是当我更新时route_copy,则应用相同的更新route.所以,我不明白我的代码有什么问题?
public class Route implements Comparable<Route> {
private List<Site> sites;
public Route()
{
sites = new ArrayList<Site>();
}
public Route(List<Site> sites)
{
this.sites = sites;
}
/**
* Copy constructor
*/
public Route(Route r) {
this(r.sites);
}
public void deleteSite(Site s) {
this.sites.remove(s);
}
}
public processData(Route route)
{
Route route_copy = new Route(route);
Site s = selectSite(route_copy);
route_copy.deleteSite(s); // !!! now 'route' does not contain an element 's'
}
Run Code Online (Sandbox Code Playgroud) c++ ×3
.net ×2
c# ×2
constructor ×2
java ×2
shallow-copy ×2
.net-4.0 ×1
arrays ×1
class ×1
clone ×1
common-lisp ×1
copy ×1
deep-copy ×1
ecmascript-6 ×1
function ×1
inheritance ×1
javascript ×1
linux ×1
lisp ×1
list ×1
object ×1
python ×1
reference ×1
repeat ×1
vector ×1