草庐IT

类和对象(中)

风起、风落 2024-02-13 原文

文章目录

类的默认成员函数

类中共有6个默认成员函数,即自己不实现,编译器会帮助我们实现出来
构造函数、析构函数、拷贝构造、赋值运算符重载、const成员、取地址及const取地址操作符重载

1. 构造函数

1. 概念

  • 在对象构造时调用的函数,这个函数完成初始化工作

2. 无参时主函数中的写法


#include<iostream>
using namespace std;
class date
{
public:
    //没有返回值
    date(int year, int month, int day)//构造函数名与类名相同
    {
        _year = year;
        _month = month;
        _day = day;
    }
    date()//构造函数可以重载
    {
        _year = 0;
        _month = 0;
        _day = 0;
    }
    void print()
    {
        cout << _year << "-" << _month << "-" << _day << endl;
    }
private:
    int _year;
    int _month;
    int _day;
};

    int main()
    {
    //构造函数无参时
        //date d();错误写法会报错
        date d;//正确写法
        d.print();
        return 0;
    }
  • 构造函数无参是一种特殊情况,不能像普通成员函数一样写括号,只写类的实例化即可
  • 编译器无法区分 date d(); 是函数的声明还是定义对象

3. 特性

特性1-3

1.没有返回值
2.函数名跟类名相同
3.对象实例化时编译器自动调用对应的构造函数

#include<iostream>
using namespace std;
class date
{
  public:
  //没有返回值
  date(int year,in tmonth,int day)//构造函数名与类名相同
  {
   _year=year;
   _month=month;
   _day=day;
   }
   private:
   int _year;
   int _month;
   int _day;
   
int main()
{
date d(2023,2,7);//类的对象实例化自动调用构造函数
return 0;
}

特性 4

4.构造函数可以重载(一个类有多个构造函数)

class date
{
  public:
  //没有返回值
  date(int year,in tmonth,int day)//构造函数名与类名相同
  {
   _year=year;
   _month=month;
   _day=day;
   }
   date ()//构造函数可以重载
   {
    ....
    }
   private:
   int _year;
   int _month;
   int _day;
   
int main()
{
date d(2023,2,7);//类的对象实例化调用构造函数
return 0;
}

当使用构造函数不传参数时,若写成date d2(); ,则会报错

特性 5

如果类中没有显式定义构造函数,则c++编译器会自动生成一个无参的默认构造函数,一旦用户显式定义编译器将不再生成

内置类型(int char double)
#include<iostream>
using namespace std;
class date
{
public:
	//编译器自动生成默认构造函数
	void print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
		int _year;
		int _month;
		int _day;
};
int main()
{
	date d;
	d.print();//-858993460--858993460--858993460
	return 0;
}

若输出结果,则会发现为随机值
对于编译器自动生成的默认构造函数,
针对内置类型的成员变量没有做处理

自定义类型
#include<iostream>
using namespace std;
class Time
{
public:
	Time()//默认构造函数
	{
		_hours = 0;
		_minute = 0;
		_seconds = 0;
	}
private:
	int _hours;
	int _minute;
	int _seconds;
};
class date
{
public:
	//没有构造函数,则编译器会自动生成一个无参的默认构造函数
	void print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
	Time _t;//调用自定义类型
};
int main()
{
	date d;//无参数
	d.print();
	return 0;
}

在date类中又定义了一个自定义类型time
自定义类型成员会调用它的默认构造函数(不用传参数的构造)

特性 6

  • 无参的构造函数和全缺省的构造函数都被称为默认构造函数,并且默认构造函数只能有一个
  • 对于无参的,两者都可以使用,就没必要共同存在

默认构造函数:(不用传参数)
1.自己实现的无参的构造函数
2.自己实现的全缺省构造函数
3.自己没写编译器自动生成的

- 既想要带参数,又想要不带参数的 如何使用一个构造函数完成?
全缺省
若参数没有传过去,则使用缺省参数
若有参数,则直接进入函数中

#include<iostream>
using namespace std;
class date
{
public:
	date(int year = 1, int month = 1, int day = 1)//全缺省
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	date d;//无参数
	d.print();//1-1-1
	date d2(2022, 12, 20);//带参数
	d2.print();//2022-12-20
	return 0;
}

2. 析构函数

1. 概念

  • 对象在销毁时会自动调用析构函数,完成类的一些资源清理工作

2. 特性

1. 析构函数名是在类名前加上字符~ (~在c语言表示按位取反)
2. 无参数无返回值类型
3. 一个类只能由一个析构函数,若未显式定义,系统会自动生成默认的析构函数(析构函数不能重载)
4. 对象生命周期结束,C++编译系统自动调用析构函数

#include<iostream>
using namespace std;
class date
{
public:
date()
{
}
//没有返回值并且无参
~date()//析构函数,若我们自己不写,系统会自动生成一个析构函数
{
}
int main()
{
date d;
 return 0;//调试时,走到return 0这步 F11会进入析构函数
 }

3.先构造后析构

#include<iostream>
using namespace std;
class stack
{
public:
	stack(int n=10)//构造函数
	{
		_a = (int*)malloc(sizeof(int) * n);
		_size = 0;
		_capity = n;
	}
	~stack()//析构函数
	{
		free(_a);
		_a = nullptr;
		_size = _capity = 0;
	}

private:
	int* _a;
	int _size;
	int _capity;
};
int main()
{
	stack s1;
	stack s2;
	return 0;
}

若使用构造函数malloc开辟一块空间,则使用析构函数free销毁空间

先通过 构造s1,再构造s2
由于在栈中,满足先进后出,所以 先析构s2,再析构s1

4. 对于成员变量

#include<iostream>
using namespace std;
class Time
{
public:
	~Time()//析构函数
	{
		cout << "~Time()" << endl;//输出~Time()
	}
private:
	int _hours;
	int _minute;
	int _seconds;
};
class date
{
public:
	//没有构造函数,则编译器会自动生成一个无参的默认构造函数
	void print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
	Time _t;//调用自定义类型
};
int main()
{
	date d;//无参数
	d.print();
	return 0;
}
  • 默认生成析构函数,对内置类型成员不处理

  • 默认生成析构函数,对自定义类型的成员,调用它的析构函数

3. 拷贝构造函数

1.值传递

#include<iostream>
using namespace std;
class date
{
public:
	date(int year = 1, int month = 1, int day = 1)//全缺省构造
	{
		_year = year;
		_month = month;
		_day = day;
	}
	date(date d)//值传递  date d 会报错造成无限循环
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	date d1(2022,12,21);
	date d2(d1);//拷贝构造
	return 0;
}

  • 这里为什么会报错?
  • 存在递归拷贝

类的对象实例化需要先调用构造函数,所以需要先传参数,把d1传给d这个过程属于传值调用,即将d1拷贝给d,正常来说,若两者为内置类型(int ,char ,double)会直接进行拷贝,但是由于d1和d都是自定义类型date,所以会发生拷贝构造
date d(d1)又是一个拷贝构造,又会发生重复上述过程,即先调用构造函数,调用之前先传参数,d1传给d这个过程中再次发生拷贝构造,从而导致无线循环下去

2. 引用传递


由于d为d1的别名,d相当于d1本身,所以 参数d1传给 d的过程中, 不会发生拷贝构造

#include<iostream>
using namespace std;
class date
{
public:
	date(int year = 1, int month = 1, int day = 1)//全缺省构造
	{
		_year = year;
		_month = month;
		_day = day;
	}
	date(const date& d)//引用传递
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	date d1(2022,12,21);
	date d2(d1);//拷贝构造
	return 0;
}

加入const,是为了防止由于操作失误导改变d本身
如:假设 d._year =_year , _year代表d2._year ,将d2中的年赋值给d1的年,就会导致报错

3.内置类型(int char double)

class date
{
public:
	date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
	//编译器自动生成拷贝构造
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	date d1(2023,2,7);//无参数
	date d2(d1);
	d1.print();//2023-2-7
	d2.print();//2023-2-7
	return 0;
}

虽然我们没有自己写拷贝构造,但是使用编译器自动生成的拷贝构造依旧可以正常运行,
说明对于内置类型会进行处理

4. 浅拷贝问题

#include<iostream>
using namespace std;
class stack
{
public:
	stack(int n)//构造函数
	{
		 _a = (int*)malloc(sizeof(int) * n);
		 _size = 0;
		 _capity = n;
	}
	~stack()//析构函数
	{
		free(_a);
		_a = nullptr;
		_size = _capity = 0;
	}

private:
	int * _a;
	int _size;
	int _capity;
};
int main()
{
	stack s1(10);
	stack s2(s1);//拷贝构造
	return 0;//空间会被释放两次,程序崩溃
}

  • 以上代码为什么一运行就会报错?
    -
    s1._a 指针指向 开辟的10个字节的空间,由于是拷贝构造,所以将s1._a指针的值 赋值给了 s2._a指针, 使s2._a指针同样指向与s1._a相同的位置,
    又由于是先构造的后析构,所以理应先析构 s2 ,s2free这块空间后,由于s1._a与s2._a指向同一个位置,s1还会对这块空间再次free
    同一块空间释放两次,会导致崩溃

4. 赋值运算符重载

1.运算符重载

1. 自定义类型为什么要使用运算符重载

#include<iostream>
using namespace std;
class date
{
public:
	date(int year = 1, int month = 1, int day = 1)//构造
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	date d1(2022,12,21);
	date d2(2022,12,22);
	return 0;
}

  • date 实例化出的对象 d1 与d2 的大小可以直接比较嘛?
  • 不可以,内置类型是编译器自己定义的类型,它知道要怎么比
  • 自定义类型是自己定义的,怎么比较大小由自己规定
  • C++为了增强代码的可读性引入运算符重载,运算符重载是具有特殊函数名的函数 ,运算符重载就是为了给自定义类型进行比较等提供的函数
  • 函数名字为:关键字operator后面接需要重载的运算符符号
    函数原型:返回值类型 operator操作符(参数列表)

2. 操作符 ==

1. 错误写法
#include<iostream>
using namespace std;
class date
{
public:
	date(int year = 1, int month = 1, int day = 1)//构造
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
//private://需要将_year等改为public
	int _year;
	int _month;
	int _day;
};
bool operator ==(const date& d1, const date& d2)//由几个参数,就接收几个
{
//判断年月日是否都相等
	return d1._year == d2._year && d1._month == d2._month && d1._day == d2._day;
}
int main()
{
	date d1(2022,12,21);
	date d2(2022,12,22);
	d1 == d2;
	cout << (d1 == d2) << endl;// 0 代表假
	return 0;
}

第一个参数作为左操作数,第二个参数作为右操作数
这样写看起来是对的,但是这种方法会 把date类中私有的成员变量变成共有的,破坏类的封装,不符合我们本意

2. 正确写法
  • 写入类中作为成员函数
#include<iostream>
using namespace std;
class date
{
public:
	date(int year = 1, int month = 1, int day = 1)//构造
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
	bool operator ==( const date& d2)// 由于隐藏的this指针的存在,&d1传过来由this指针接收
	{
		return _year == d2._year && _month == d2._month && _day == d2._day;
	}
private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	date d1(2022,12,21);
	date d2(2022,12,22);
	d1 == d2;//  d1.operator==(d2) 等价于 d1.operator==(&d1,d2)
	cout << (d1 == d2) << endl;
	return 0;
}

传入类中,由于隐藏的this指针的存在,取第一个参数d1的地址传过去被this指针接收,_year等价于d1._year
但是由于this指针是隐藏的,所以&d1也不需要表现出来直接传入d2即可

3. cout << d1 == d2<< endl; 报错问题
  • 运算符优先级问题,<<代表流插入,<<优先级比==高,所以会先执行cout<<d1 ,剩余的 = =d2无法执行,就会报错
  • 所以要改成 (d1= =d2)来保证d1与d2先执行

3. 注意事项

1. 不能通过连接其他符号来创建新的操作符 (如 operator@)
2.重载操作符必须有一个类类型参数

3.用于内置类型的操作符,其含义不能改变(如 int 加法 不能改变)

  • 这里理解很简单,因为运算符重载是针对自定义类型的,对于内置类型,编译器都已经设置好了

4.作为类成员的重载函数时,其形参看起来比操作数数目少1成员函数的操作符有一个默认的形参this,限定为第一个形参

  • this指针是隐藏的,所以也不用传过来,this指针默认为第一个参数,并且代表d1的地址

5. ( . * ) (:: ) (sizeof ) (? : 三目运算符) ( . ) 以上5个运算符不能重载

4. 操作符 >

#include<iostream>
using namespace std;
class date
{
public:
	date(int year = 1, int month = 1, int day = 1)//构造
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
	bool operator >( const date& d2)// 由于隐藏的this指针的存在,&d1传过来由this指针接收
	{
		if (_year > d2._year)//年大为大
		{
			return true;
		}
		else if (_year == d2._year && _month > d2._month) //年相等,月大为大
		{
			return true;
		}
		else if (_year == d2._year && _month == d2._month && _day > d2._day)//年 月相等,天大为大
		{
			return true;
		}
		else
		{
			return false;
		}
	}
private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	date d1(2022,12,21);
	date d2(2022,12,22);
	d1 > d2;//等价于 operator>(&d1,d2)
	cout << (d1 > d2) << endl;
	return 0;
}

判断大于时 ,同样存在一个隐藏的this指针,我们只需要判断 年大的就为大 ,年相等 ,月大的就为大,年月相等,天大的就为大,其他情况都为假

5. 操作符 !=

#include<iostream>
using namespace std;
class date
{
public:
	date(int year = 1, int month = 1, int day = 1)//构造
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
	bool operator==(const date& d2)//d1==d2
	{
		return _year == d2._year && _month == d2._month && _day == d2._day;
	}
	bool operator!=(const date& d2)
	{
		return !(*this == d2);//借助上面的d1==d2的相反即 d1!=d2
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	date d1(2022,12,21);
	date d2(2022,12,22);
	d1 != d2;
	cout << (d1 != d2) << endl;
	return 0;
}

借助上面已经写好的d1==d2 ,取其相反 即为 d1!=d2

6. 操作符 <

#include<iostream>
using namespace std;
class date
{
public:
	date(int year = 1, int month = 1, int day = 1)//构造
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
	bool operator >(const date& d2)// d1>d2
	{
		if (_year > d2._year)//年大为大
		{
			return true;
		}
		else if (_year == d2._year && _month > d2._month) //年相,月大为大
		{
			return true;
		}
		else if (_year == d2._year && _month == d2._month && _day > d2._day)//年 月相等,天大为大
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	bool operator<(const date& d2)// d1 < d2
	{
		return !(*this > d2);
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	date d1(2022,12,21);
	date d2(2022,12,22);
	d1 < d2;
	cout << (d1 < d2) << endl;
	return 0;
}

  • 若直接写d1<d2 的条件太复杂,直接采用 d1 >d2 的相反,*this 代表 d1

2 .赋值操作符(编译器默认实现)

1. 正常使用

#include<iostream>
using namespace std;
class date
{
public:
	date(int year = 1, int month = 1, int day = 1)//构造
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
	void operator=(const date& d2)
	{
		//为了防止自己给自己赋值的事情发生,如:d1=d1
		if ( this != &d2)
		{
			_year = d2._year;
			_month = d2._month;
			_day = d2._day;
		}
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	date d1(2022,12,21);
	date d2(2022,12,22);
	d1 =d2;
	return 0;
}

  • 如果是单个赋值这样就符合了,但是还是有连续赋值的情况存在

2. 连续赋值情况的考虑

#include<iostream>
using namespace std;
class date
{
public:
	date(int year = 1, int month = 1, int day = 1)//构造
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
	date operator=(const date& d)//传值返回 类型为date
	{
		//为了防止自己给自己赋值的事情发生,如:d1=d1
		if (this != &d)
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;
	}
	date(const date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	date d1(2022, 12, 21);
	date d2(d1);
	date d3;
	d2 = d3 = d1;
	int i = 0;
	int j = 0;
	int z = 0;
	i = j = z;
	return 0;
}
  • 如果为内置类型,如int 则可以进行连续赋值
  • 对于int 来说,根据结合性,j=z ,返回值为j ,i=j,生成最终结果i
  • 对于 d2=d3=d1自定义类型来说,将 d1赋值给d3,将其返回值作为d2= 的右操作数存在
  • 所以operator= 要有一个返回值
传值返回


由于*this为date 类型,属于传值返回,即返回一个临时变量,所以需进行拷贝构造
同时也会多开辟一块空间存储
就会导致当return ( * this) 返回时,传入拷贝构造中创建临时变量 ,再次从中返回时,才能返回到 主函数中

传引用返回

  • 传 * this的引用作为变量的别名,相当于 * this本身,不会消耗一块空间,自然在return*this 返回时,不会进入拷贝构造中,而是直接返回
  • 所以传引用返回更优

5. const成员

1.对象调用const成员函数

#include<iostream>
using namespace std;
class date
{
public:
	void print()
	{
		cout << _a << endl;
	}
private:
	int _a;
};
int main()
{
	const date a;
	a.print();//权限放大,会报错
	return 0;
}
  • 将&a传过去由this指针接收, a指针类型为 const date* ,this指针类型为 date* ,将const date * 传给date * 属于权限的放大
  • 把this指针类型改成 const date* 就可以了,但是由于this指针是隐藏的,要怎么解决呢?
#include<iostream>
using namespace std;
class date
{
public:
	void print()const //this指针类型变为const date*
	{
		cout << _a << endl;
	}
private:
	int _a;
};
int main()
{
	const date a;
	a.print();//正确 权限保持
	return 0;
}
  • 在括号外面加上const,使this指针类型改成 const date*

const 在*左右对于指针的影响

const date* p1 : 修饰的是* p1, const在 * 左面 指针所指向的内容不变
date const * p2 : 修饰的是* p2,const在* 左面 指针所指向的内容不变
date * const p3 : 修饰的是 p3,const在 * 右面,指针本身不变

注意事项

#include<iostream>
using namespace std;
class date
{
public:
	void print()const //this指针类型变为const date*
	{
	   _a=10;//报错
		cout << _a << endl;
	}
private:
	int _a;
};
int main()
{
	const date a;
	a.print();//正确 权限保持
	return 0;
}

若在print函数内部修改成员变量_a的值就会报错,_a代表this->_a,但是由于 this指针当前类型为 const date *,使this指向的内容不变

2.成员函数调用const成员函数

#include<iostream>
using namespace std;
class date
{
public:
	void f1()
	{
		f2();
	}
	void f2()const
	{
	}
private:
	int _a;
};
int main()
{
	date a;
	a.f1();
	return 0;
}

在f1中时,this指针的类型为 date*
进入 f2后, this指针 转换为 const date *
属于权限缩小 ,可以实现

#include<iostream>
using namespace std;
class date
{
public:
	void f3()
	{
		
	}
	void f4()const
	{
		f3();
	}
private:
	int _a;
};
int main()
{
	date a;
	a.f4();
	return 0;
}

f4中 this指针类型为 const date*
f3中 this指针类型为 date*
权限放大了,不可以实现

6.取地址及const取地址操作符重载

#include<iostream>
using namespace std;
class date
{
public:
	date* operator&()
	{
		return this;
	}
	const date* operator&()const
	{
		return this;
	}
};
int main()
{
	date d;
	cout << &d << endl;
	return 0;
}

有关类和对象(中)的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. ruby-on-rails - 按天对 Mongoid 对象进行分组 - 2

    在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev

  3. ruby-on-rails - 如何验证非模型(甚至非对象)字段 - 2

    我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss

  4. Ruby 写入和读取对象到文件 - 2

    好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信

  5. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  6. ruby-on-rails - 未在 Ruby 中初始化的对象 - 2

    我在Rails工作并有以下类(class):classPlayer当我运行时bundleexecrailsconsole然后尝试:a=Player.new("me",5.0,"UCLA")我回来了:=>#我不知道为什么Player对象不会在这里初始化。关于可能导致此问题的操作/解释的任何建议?谢谢,马里奥格 最佳答案 havenoideawhythePlayerobjectwouldn'tbeinitializedhere它没有初始化很简单,因为你还没有初始化它!您已经覆盖了ActiveRecord::Base初始化方法,但您没有调

  7. ruby - 如何在 Rails 4 中使用表单对象之前的验证回调? - 2

    我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser

  8. Ruby——嵌套类和子类是一回事吗? - 2

    下面例子中的Nested和Child有什么区别?是否只是同一事物的不同语法?classParentclassNested...endendclassChild 最佳答案 不,它们是不同的。嵌套:Computer之外的“Processor”类只能作为Computer::Processor访问。嵌套为内部类(namespace)提供上下文。对于ruby​​解释器Computer和Computer::Processor只是两个独立的类。classComputerclassProcessor#Tocreateanobjectforthisc

  9. ruby - 一个 YAML 对象可以引用另一个吗? - 2

    我想让一个yaml对象引用另一个,如下所示:intro:"Hello,dearuser."registration:$introThanksforregistering!new_message:$introYouhaveanewmessage!上面的语法只是它如何工作的一个例子(这也是它在thiscpanmodule中的工作方式。)我正在使用标准的ruby​​yaml解析器。这可能吗? 最佳答案 一些yaml对象确实引用了其他对象:irb>require'yaml'#=>trueirb>str="hello"#=>"hello"ir

  10. ruby - 更改 ActiveRecord 中对象的类 - 2

    假设我有一个FireNinja我的数据库中的对象,使用单表继承存储。后来才知道他真的是WaterNinja.将他更改为不同的子类的最干净的方法是什么?更好的是,我很想创建一个新的WaterNinja对象并替换旧的FireNinja在数据库中,保留ID。编辑我知道如何创建新的WaterNinja来self现有FireNinja的对象,我也知道我可以删除旧的并保存新的。我想做的是改变现有项目的类别。我是通过创建一个新对象并执行一些ActiveRecord魔法来替换行,还是通过对对象本身做一些疯狂的事情,或者甚至通过删除它并使用相同的ID重新插入来做到这一点,这是问题的一部分。

随机推荐