methods(Access = protected)
时间: 2023-09-24 08:03:01 浏览: 58
这是一个在MATLAB中定义类的方法的关键字。它表示该方法只能在该类及其子类中被访问,而不能从类外部直接访问。这可以帮助保护类的实现细节,使得类的使用者只能通过类公开的接口来访问类的方法。在MATLAB中,方法的访问控制关键字包括public、protected和private。public表示该方法可以被类的所有使用者访问,private表示该方法只能在该类内部被访问。
相关问题
methods (Access = protected) %% 生成文件输出 function fileOut = genFileOut(obj,channel,cmd) outPath = obj.OutputPath+'\'+string(channel.prn); fileOut.OutPath = outPath; if ~exist(fileOut.OutPath,'dir') mkdir(fileOut.OutPath); end fileOut.TrackFile = -1; if exist('cmd','var') switch cmd case 'open' permission = 'wt'; case 'recover' permission = 'at'; otherwise return; end fileOut.TrackFile = fopen(fileOut.OutPath+'\TrackFile.txt',permission); end end %% 关闭文件输出 function fileOut = clcFileOut(~,fileOut) if ~isempty(fileOut) && fileOut.TrackFile ~= -1 fclose(fileOut.TrackFile); fileOut.TrackFile = -1; end end end
这段代码是一个 MATLAB 类中的两个方法,用于生成和文件输出。方法 genFileOut 用于生成文件输出,它会根据传入的 channel 和 cmd 参数来确定输出路径和 TrackFile.txt 文件的权限。如果输出路径不存在,则会创建该路径。方法 clcFileOut 则用于关闭文件输出,它会关闭 TrackFile.txt 文件的句柄。这些方法都是被保护的,只能在该类或其子类中访问。
java protected
The protected keyword in Java is an access modifier that provides access to the member variables and methods of a class to its subclasses and classes within the same package.
When a class member is marked as protected, it can only be accessed within the same package or by a subclass of the class. Protected members are useful when you want to provide access to certain fields or methods of a class to its subclasses, but not to other classes outside the package.
For example, if we have a class called Animal, and we want to provide access to its protected member variable called age to its subclass called Dog, we can declare the variable as protected in the Animal class like this:
```
public class Animal {
protected int age;
}
```
Now, the Dog class can access the age variable like this:
```
public class Dog extends Animal {
public void printAge() {
System.out.println("The age of this dog is: " + age);
}
}
```
Protected members can also be accessed within the same class, just like private members. However, unlike private members, protected members can be accessed by subclasses as well.
It is important to note that the protected access modifier is less restrictive than the private access modifier, but more restrictive than the public access modifier.
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)