rust学习二十.7、RUST完全限定名以及静态方法
<p>rust中看不到java等一些OOP语言的中用于修饰方法的static之类的关键字眼。</p><p>注:<strong>rust有static关键字,目前是用于修饰生命周期的或者是变量的</strong></p>
<p>例如:</p>
<div style="color: rgba(0, 0, 0, 1); background-color: rgba(255, 255, 255, 1); font-family: Consolas, "Courier New", monospace; font-weight: normal; font-size: 14px; line-height: 19px; white-space: pre">
<div><span style="color: rgba(0, 0, 255, 1)">let</span><span style="color: rgba(0, 0, 0, 1)"> leaked_str: &</span><span style="color: rgba(0, 0, 255, 1)">'static</span><span style="color: rgba(0, 0, 0, 1)"> str </span><span style="color: rgba(0, 0, 0, 1)">=</span><span style="color: rgba(0, 0, 0, 1)"> Box</span><span style="color: rgba(0, 0, 0, 1)">::</span><span style="color: rgba(0, 0, 0, 1)">leak(tmp</span><span style="color: rgba(0, 0, 0, 1)">.</span><span style="color: rgba(0, 0, 0, 1)">into_boxed_str());</span></div>
<div>
<div style="color: rgba(0, 0, 0, 1); background-color: rgba(255, 255, 255, 1); font-family: Consolas, "Courier New", monospace; font-weight: normal; font-size: 14px; line-height: 19px; white-space: pre">
<div><span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> </span><span style="color: rgba(0, 0, 255, 1)">mut</span><span style="color: rgba(0, 0, 0, 1)"> SIGN: u32 </span><span style="color: rgba(0, 0, 0, 1)">=</span><span style="color: rgba(0, 0, 0, 1)"> </span><span style="color: rgba(9, 134, 88, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">;</span></div>
<div> </div>
<div><span style="color: rgba(0, 0, 0, 1)">先给出完全限定语法:</span></div>
<div><span style="color: rgba(0, 0, 0, 1)"><Type as Trait>::function(receiver_if_method, next_arg, ...);</span></div>
<div> </div>
<div><span style="color: rgba(0, 0, 0, 1)">为什么需要这个语法,直接原因是:特质和实现特定特质的类型中都存在同样的方法,必须通过完全限定的方式告诉Rust执行哪个方法。</span></div>
<div> </div>
<h1><span style="color: rgba(0, 0, 0, 1)">一、例子</span></h1>
<p><span style="color: rgba(0, 0, 0, 1)">本例对于书上的例子稍微进行了改造.</span></p>
<pre class="language-rust highlighter-hljs"><code>trait Fight{
fn fight(&self);
fn rest();
}
struct Student{
name:String
}
implStudent{
/**
* fight是关联函数,因为它带有self参数,所以不能直接调用特质中的同名方法,
* 必须通过实例调用
*/
fn fight(&self){
println!("{} 在独斗.十步杀一人,千里不留行", self.name);
}
/**
* rest是静态方法,不是关联方法,可以直接通过类型调用
* 所以不能通过实例调用,只能通过类型调用
*/
fnrest(){
println!("休息一下");
}
}
impl Fight for Student{
fn fight(&self){
println!("{} 在团战.", self.name);
}
fn rest(){
println!("大家休息一下");
}
}
fn main() {
let stu = Student{name:"小明".to_string()};
// 三种调用方式,调取不同的方法
// 1.实例后跟上方法
// 2.特质名::方法(&实例)
// 3.<Type as Trait>::function(receiver_if_method, next_arg, ...);
stu.fight();//1.实例跟上方法
Fight::fight(&stu); //2.特质名::方法(&实例) -- 实际是调用特质的方法
<Student as Fight>::fight(&stu);//3.特质名::方法(&实例) -- 实际是调用对象实例的方法
Student::rest();//4.类型的静态方法
<Student as Fight>::rest();
}</code></pre>
<p><span style="color: rgba(0, 0, 0, 1)">在本例中有一个类型Student和一个特质Fight.</span></p>
<p><span style="color: rgba(0, 0, 0, 1)">Student和Fight都有方法fight和rest,其中rest是无参数。<strong>Student.reset本质上就是一个类型Student的静态方法,即使不实例化也可以调用。</strong></span></p>
<p>看运行结果:</p>
<p><img src="https://img2024.cnblogs.com/blog/1177268/202503/1177268-20250331164419163-424785662.png"></p>
<p> </p>
<h1><span style="color: rgba(0, 0, 0, 1)">二、小结</span></h1>
<p><span style="color: rgba(0, 0, 0, 1)">1.rust的类型可以有静态方法,不需要实例化即可执行,但是rust并没有使用类似static之类的修饰符明显指出</span></p>
<p><span style="color: rgba(0, 0, 0, 1)">2.rust可以通过方法的完全限定语法来确定要执行类型所实现的特质方法--如果这个名称的方法类型自己也有一个</span></p>
<p>3.在诸如java这样的语言中,是不允许rust这样的-对象的方法名和接口的方法名称一样</p>
<p>4.rust这种措施,允许重名出现,但这样反而会导致代码难于阅读。不清楚为什么它要容许这个,是不是因为rust设计到一半的时候,不想重构了,于是顺水推舟...</p>
<p> </p>
</div>
</div>
</div>
</div>
<div id="MySignature" role="contentinfo">
<p>本文来自博客园,作者:正在战斗中,转载请注明原文链接:https://www.cnblogs.com/lzfhope/p/18802457</p><br><br>
来源:https://www.cnblogs.com/lzfhope/p/18802457
頁:
[1]