java里operate什么意思 Java如何实现const
大家好,今天来为大家解答java里operate什么意思这个问题的一些问题点,包括Java如何实现const也一样很多人还不知道,因此呢,今天就来为大家分析分析,现在让我们一起来看看吧!如果解决了您的问题,还望您关注下本站哦,谢谢~
java的Struts配置<Action attribute>是什么意思
1)应用前提,attribute只有在设置了name后才有意义。
2)attribute可以实现对象的重用,即如果设置了attribute属性,在创建actionform是,会先去查找相应的scope中是否有此对象,如果有,则重用,否则创建新的对象。
3)当你将创建的acitonForm保存到相应的scope中时,你想用一个更有意义的名字来访问它时,它就有意义了。
可是,看到"一个更有意义的名字的时候",我好像有点理解了
<action
attribute="newLoginForm"
name="loginForm"
type="loginAction"
scope="request"
path="/login">
在struts实例化actionform的时候,struts是根据attribute的值来查找并创建actionform,有两种情况:如果已经存在,那么从内存中取回;如果第一次实例化,那么创建,并放入内存。
org.apache.struts.util.RequestUtils中的源代码
public static Actionform createActionform(
HttpServletRequest request,
ActionMapping mapping,
ModuleConfig moduleConfig,
ActionServlet servlet){
............
............
String attribute= mapping.getAttribute();
......
Actionform instance= null;
HttpSession session= null;
if("request".equals(mapping.getScope())){
instance=(Actionform) request.getAttribute(attribute);
} else{
session= request.getSession();
instance=(Actionform) session.getAttribute(attribute);
}
................
................
}
如果没有配置attribute属性的话, struts才会从name属性里读出要创建的formbean的名字,并创建一下实例,看下边的源代码就知道了,呵呵.
org.apache.struts.config.ActionConfig
protected String attribute= null;public String getAttribute(){
//就是这里了.
if(this.attribute== null){
return(this.name);
} else{
return(this.attribute);
}
}public void setAttribute(String attribute){
if(configured){
throw new IllegalStateException("Configuration is frozen");
}
this.attribute= attribute;
}
Java如何实现const
const是constant的缩写,“恒定不变”的意思。被const修饰的东西都受到强制保护,可以预防意外的变动,能提高程序的健壮性和高效性。
1用const修饰函数的参数
如果参数作输出用,不论它是什么数据类型,也不论它采用“指针传递”还是“引用传递”,都不能加const修饰,否则该参数将失去输出功能。
const只能修饰输入参数:
u如果输入参数采用“指针传递”,那么加const修饰可以防止意外地改动该指针,起到保护作用。
例如StringCopy函数:
void StringCopy(char*strDestination, const char*strSource);
其中strSource是输入参数,strDestination是输出参数。给strSource加上const修饰后,如果函数体内的语句试图改动strSource的内容,编译器将指出错误。
u如果输入参数采用“值传递”,由于函数将自动产生临时变量用于复制该参数,该输入参数本来就无需保护,所以不要加const修饰。
例如不要将函数void Func1(int x)写成void Func1(const int x)。同理不要将函数void Func2(A a)写成void Func2(const A a)。其中A为用户自定义的数据类型。
u对于非内部数据类型的参数而言,象void Func(A a)这样声明的函数注定效率比较底。因为函数体内将产生A类型的临时对象用于复制参数a,而临时对象的构造、复制、析构过程都将消耗时间。
为了提高效率,可以将函数声明改为void Func(A& a),因为“引用传递”仅借用一下参数的别名而已,不需要产生临时对象。但是函数void Func(A& a)存在一个缺点:“引用传递”有可能改变参数a,这是我们不期望的。解决这个问题很容易,加const修饰即可,因此函数最终成为void Func(const A& a)。
以此类推,是否应将void Func(int x)改写为void Func(const int& x),以便提高效率?完全没有必要,因为内部数据类型的参数不存在构造、析构的过程,而复制也非常快,“值传递”和“引用传递”的效率几乎相当。
对于非内部数据类型的输入参数,应该将“值传递”的方式改为“const引用传递”,目的是提高效率。例如将void Func(A a)改为void Func(const A& a)。
对于内部数据类型的输入参数,不要将“值传递”的方式改为“const引用传递”。否则既达不到提高效率的目的,又降低了函数的可理解性。例如void Func(int x)不应该改为void Func(const int& x)。
2用const修饰函数的返回值
u如果给以“指针传递”方式的函数返回值加const修饰,那么函数返回值(即指针)的内容不能被修改,该返回值只能被赋给加const修饰的同类型指针。
例如函数
const char* GetString(void);
如下语句将出现编译错误:
char*str= GetString();
正确的用法是
const char*str= GetString();
u如果函数返回值采用“值传递方式”,由于函数会把返回值复制到外部临时的存储单元中,加const修饰没有任何价值。
例如不要把函数int GetInt(void)写成const int GetInt(void)。
同理不要把函数A GetA(void)写成const A GetA(void),其中A为用户自定义的数据类型。
如果返回值不是内部数据类型,将函数A GetA(void)改写为const A& GetA(void)的确能提高效率。但此时千万千万要小心,一定要搞清楚函数究竟是想返回一个对象的“拷贝”还是仅返回“别名”就可以了,否则程序会出错。例如:
[cpp] view plaincopyprint?
class String
{
//…
//赋值函数
String& operate=(const String&other);
//相加函数,如果没有friend修饰则只许有一个右侧参数
friend String operate+(const String&s1, const String&s2);
private:
char*m_data;
}<span style="font-family:宋体; font-size: 14pt; background-color: rgb(255, 255, 255);"></span>
class String
{
//…
//赋值函数
String& operate=(const String&other);
//相加函数,如果没有friend修饰则只许有一个右侧参数
friend String operate+(const String&s1, const String&s2);
private:
char*m_data;
}
String的赋值函数operate=的实现如下:
[cpp] view plaincopyprint?
String& String::operate=(const String&other)
{
if(this==&other)
return*this;
delete m_data;
m_data= new char[strlen(other.data)+1];
strcpy(m_data, other.data);
return*this;//返回的是*this的引用,无需拷贝过程
}<span style="font-family:宋体; font-size: 14pt; background-color: rgb(255, 255, 255);"></span>
String& String::operate=(const String&other)
{
if(this==&other)
return*this;
delete m_data;
m_data= new char[strlen(other.data)+1];
strcpy(m_data, other.data);
return*this;//返回的是*this的引用,无需拷贝过程
}
对于赋值函数,应当用“引用传递”的方式返回String对象。如果用“值传递”的方式,虽然功能仍然正确,但由于return语句要把*this拷贝到保存返回值的外部存储单元之中,增加了不必要的开销,降低了赋值函数的效率。例如:
String a,b,c;
…
a= b;//如果用“值传递”,将产生一次*this拷贝
a= b= c;//如果用“值传递”,将产生两次*this拷贝
String的相加函数operate+的实现如下:
[cpp] view plaincopyprint?
String String::operate+(const String&s1, const String&s2)
{
String temp;
delete temp.data;// temp.data是仅含‘\0’的字符串
temp.data= new char[strlen(s1.data)+ strlen(s2.data)+ 1];
strcpy(temp.data, s1.data);
strcat(temp.data, s2.data);
return temp;
}
String String::operate+(const String&s1, const String&s2)
{
String temp;
delete temp.data;// temp.data是仅含‘\0’的字符串
temp.data= new char[strlen(s1.data)+ strlen(s2.data)+ 1];
strcpy(temp.data, s1.data);
strcat(temp.data, s2.data);
return temp;
}
对于相加函数,应当用“值传递”的方式返回String对象。如果改用“引用传递”,那么函数返回值是一个指向局部对象temp的“引用”。由于temp在函数结束时被自动销毁,将导致返回的“引用”无效。例如:
c= a+ b;
此时 a+ b并不返回期望值,c什么也得不到,留下了隐患。
u函数返回值采用“引用传递”的场合并不多,这种方式一般只出现在类的赋值函数中,目的是为了实现链式表达。
例如
[cpp] view plaincopyprint?
class A
{//…
A& operate=(const A&other);//赋值函数
};
class A
{//…
A& operate=(const A&other);//赋值函数
};
A a, b, c;// a, b, c为A的对象
…
a= b= c;//正常的链式赋值
(a= b)= c;//不正常的链式赋值,但合法
如果将赋值函数的返回值加const修饰,那么该返回值的内容不允许被改动。上例中,语句 a= b= c仍然正确,但是语句(a= b)= c则是非法的。
3用const修饰成员函数
const成员函数是指在类的成员函数后面加 const的函数,如int GetNum() const; const修饰成员函数的作用是:只能读取数据成员,不能改变数据成员。const成员函数只能调用const函数,不能调用其它非const成员函数;const对象只能调用const成员函数,不能调用非const成员函数。
任何不会修改数据成员的函数都应该声明为const类型。如果在编写const成员函数时,不慎修改了数据成员,或者调用了其它非const成员函数,编译器将指出错误,这无疑会提高程序的健壮性。
Java的const怎么用
const是constant的缩写,“恒定不变”的意思。被const修饰的东西都受到强制保护,可以预防意外的变动,能提高程序的健壮性和高效性。
1用const修饰函数的参数
如果参数作输出用,不论它是什么数据类型,也不论它采用“指针传递”还是“引用传递”,都不能加const修饰,否则该参数将失去输出功能。
const只能修饰输入参数:
u如果输入参数采用“指针传递”,那么加const修饰可以防止意外地改动该指针,起到保护作用。
例如StringCopy函数:
void StringCopy(char*strDestination, const char*strSource);
其中strSource是输入参数,strDestination是输出参数。给strSource加上const修饰后,如果函数体内的语句试图改动strSource的内容,编译器将指出错误。
u如果输入参数采用“值传递”,由于函数将自动产生临时变量用于复制该参数,该输入参数本来就无需保护,所以不要加const修饰。
例如不要将函数void Func1(int x)写成void Func1(const int x)。同理不要将函数void Func2(A a)写成void Func2(const A a)。其中A为用户自定义的数据类型。
u对于非内部数据类型的参数而言,象void Func(A a)这样声明的函数注定效率比较底。因为函数体内将产生A类型的临时对象用于复制参数a,而临时对象的构造、复制、析构过程都将消耗时间。
为了提高效率,可以将函数声明改为void Func(A& a),因为“引用传递”仅借用一下参数的别名而已,不需要产生临时对象。但是函数void Func(A& a)存在一个缺点:“引用传递”有可能改变参数a,这是我们不期望的。解决这个问题很容易,加const修饰即可,因此函数最终成为void Func(const A& a)。
以此类推,是否应将void Func(int x)改写为void Func(const int& x),以便提高效率?完全没有必要,因为内部数据类型的参数不存在构造、析构的过程,而复制也非常快,“值传递”和“引用传递”的效率几乎相当。
对于非内部数据类型的输入参数,应该将“值传递”的方式改为“const引用传递”,目的是提高效率。例如将void Func(A a)改为void Func(const A& a)。
对于内部数据类型的输入参数,不要将“值传递”的方式改为“const引用传递”。否则既达不到提高效率的目的,又降低了函数的可理解性。例如void Func(int x)不应该改为void Func(const int& x)。
2用const修饰函数的返回值
u如果给以“指针传递”方式的函数返回值加const修饰,那么函数返回值(即指针)的内容不能被修改,该返回值只能被赋给加const修饰的同类型指针。
例如函数
const char* GetString(void);
如下语句将出现编译错误:
char*str= GetString();
正确的用法是
const char*str= GetString();
u如果函数返回值采用“值传递方式”,由于函数会把返回值复制到外部临时的存储单元中,加const修饰没有任何价值。
例如不要把函数int GetInt(void)写成const int GetInt(void)。
同理不要把函数A GetA(void)写成const A GetA(void),其中A为用户自定义的数据类型。
如果返回值不是内部数据类型,将函数A GetA(void)改写为const A& GetA(void)的确能提高效率。但此时千万千万要小心,一定要搞清楚函数究竟是想返回一个对象的“拷贝”还是仅返回“别名”就可以了,否则程序会出错。例如:
[cpp] view plaincopyprint?
class String
{
//…
//赋值函数
String& operate=(const String&other);
//相加函数,如果没有friend修饰则只许有一个右侧参数
friend String operate+(const String&s1, const String&s2);
private:
char*m_data;
}
class String
{
//…
//赋值函数
String& operate=(const String&other);
//相加函数,如果没有friend修饰则只许有一个右侧参数
friend String operate+(const String&s1, const String&s2);
private:
char*m_data;
}
String的赋值函数operate=的实现如下:
[cpp] view plaincopyprint?
String& String::operate=(const String&other)
{
if(this==&other)
return*this;
delete m_data;
m_data= new char[strlen(other.data)+1];
strcpy(m_data, other.data);
return*this;//返回的是*this的引用,无需拷贝过程
}
String& String::operate=(const String&other)
{
if(this==&other)
return*this;
delete m_data;
m_data= new char[strlen(other.data)+1];
strcpy(m_data, other.data);
return*this;//返回的是*this的引用,无需拷贝过程
}
对于赋值函数,应当用“引用传递”的方式返回String对象。如果用“值传递”的方式,虽然功能仍然正确,但由于return语句要把*this拷贝到保存返回值的外部存储单元之中,增加了不必要的开销,降低了赋值函数的效率。例如:
String a,b,c;
…
a= b;//如果用“值传递”,将产生一次*this拷贝
a= b= c;//如果用“值传递”,将产生两次*this拷贝
String的相加函数operate+的实现如下:
[cpp] view plaincopyprint?
String String::operate+(const String&s1, const String&s2)
{
String temp;
delete temp.data;// temp.data是仅含‘\0’的字符串
temp.data= new char[strlen(s1.data)+ strlen(s2.data)+ 1];
strcpy(temp.data, s1.data);
strcat(temp.data, s2.data);
return temp;
}
String String::operate+(const String&s1, const String&s2)
{
String temp;
delete temp.data;// temp.data是仅含‘\0’的字符串
temp.data= new char[strlen(s1.data)+ strlen(s2.data)+ 1];
strcpy(temp.data, s1.data);
strcat(temp.data, s2.data);
return temp;
}
对于相加函数,应当用“值传递”的方式返回String对象。如果改用“引用传递”,那么函数返回值是一个指向局部对象temp的“引用”。由于temp在函数结束时被自动销毁,将导致返回的“引用”无效。例如:
c= a+ b;
此时 a+ b并不返回期望值,c什么也得不到,留下了隐患。
u函数返回值采用“引用传递”的场合并不多,这种方式一般只出现在类的赋值函数中,目的是为了实现链式表达。
例如
[cpp] view plaincopyprint?
class A
{//…
A& operate=(const A&other);//赋值函数
};
class A
{//…
A& operate=(const A&other);//赋值函数
};
A a, b, c;// a, b, c为A的对象
…
a= b= c;//正常的链式赋值
(a= b)= c;//不正常的链式赋值,但合法
如果将赋值函数的返回值加const修饰,那么该返回值的内容不允许被改动。上例中,语句 a= b= c仍然正确,但是语句(a= b)= c则是非法的。
3用const修饰成员函数
const成员函数是指在类的成员函数后面加 const的函数,如int GetNum() const; const修饰成员函数的作用是:只能读取数据成员,不能改变数据成员。const成员函数只能调用const函数,不能调用其它非const成员函数;const对象只能调用const成员函数,不能调用非const成员函数。
任何不会修改数据成员的函数都应该声明为const类型。如果在编写const成员函数时,不慎修改了数据成员,或者调用了其它非const成员函数,编译器将指出错误,这无疑会提高程序的健壮性。
好了,本文到此结束,如果可以帮助到大家,还望关注本站哦!