Eclipse 可以选择从各自的变量生成 getter 和 setter。是否有一个选项或设置可以让我生成 getter 和 setter 的注释?
例如如果我的变量名称是
protected boolean isActive;
Run Code Online (Sandbox Code Playgroud)
然后,当我为此生成 getter 时,我是否也可以获得自动生成的注释块,如下所示 -
/**
* Gets the value of the isActive property.
*
* @return
* possible object is
* {@link Boolean }
*
*/
public Boolean getIsActive() {
return isActive;
}
Run Code Online (Sandbox Code Playgroud)
如果这件事是可能的,那就太好了。另外评论区可以自定义吗?
我有一组脚本,它们共享一些用作状态标志的环境变量。
考虑:
./script1.sh; ./script2.sh; # I execute 2 scripts within the same shell.
现在,这些脚本中的每一个都会定期执行以下脚本,以设置(刷新)环境变量:
. ./setEnvVariables.sh #This executes it in the context of the current shell
and thus making the environment variables accessible across both scripts.
script1.sh,script2.sh,..脚本执行过程中在文件中手动更改。另一种方法是将标志保存在文件中并创建典型的获取/设置函数来读取文件并返回/设置标志值。这些标志由我设置以简化对脚本功能的控制。
有没有更好的方法来处理这个问题?这有点属于 getter-setter 设计模式......
我想使用自定义设置器订阅所有输入中属性的所有更改value:
Object.defineProperty(HTMLInputElement.prototype, 'value', {
set: function(newValue) {
// do some logic here
// WHAT PUT HERE to call "super setter"?
}
});
Run Code Online (Sandbox Code Playgroud)
如果我使用this.value = newValue;我得到的结果Maximum call stack size exceeded是相当正确的,但是......
没关系。我应该怎样称呼才能以value正确的方式改变?这里是
JSFIDDLE有更详细的解释。
爪哇 8
我有一个 POJO 类:
class User {
private String name;
private String password;
//... Getters / Setters ...
}
Run Code Online (Sandbox Code Playgroud)
我将用作实体类。
在 getter/setter for 中password,我想添加解密/加密逻辑。
public String getPassword() {
return EncryptionFactory.decryption(password);
}
public void setPassword(String password) {
this.password = EncryptionFactory.encryption(password);
}
Run Code Online (Sandbox Code Playgroud)
EncryptionFactory是一个加密/解密 a 的实用程序类String。
根据通用 Java 编码指南,如果我添加更改密码的逻辑,它是否破坏了设计或设计不良?
在使用它时,我从我的教授那里得到了糟糕的设计反馈。
我有一个带有字符串属性的类,我的 getter 必须返回这些属性的 string& 值。
\n\n我设法做到这一点而没有出现错误的唯一方法是这样的:
\n\ninline string& Class::getStringAttribute() const{\n static string dup = stringAttribute;\n return dup;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n在 C++ 中编写返回私有字符串属性的字符串引用的 getter 的正确方法是什么?
\n\n这样做:
\n\ninline string& Class::getStringAttribute() const{\n return stringAttribute;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n给我这个错误:
\n\nerror: invalid initialization of reference of type \xe2\x80\x98std::string& {aka std::basic_string<char>&}\xe2\x80\x99 from expression of type \xe2\x80\x98const string {aka const std::basic_string<char>}\xe2\x80\x99\nRun Code Online (Sandbox Code Playgroud)\n 我正在询问使用 getter 和 setter 的建议。我在两个版本中编写了相同的代码:使用 getter 和 setter 以及仅使用类方法。而且我看不出它们之间的区别。
我用私有字段评级编写了类 Book。并且构造函数 Book 可以通过 RatingSetter 或 RatingMethod 为 Book.rating 分配一些东西。RatingMethod 只设置值,但我也可以创建一个仅用于获取值的方法。
class Book
{
public string title;
public string author;
private string rating;
public Book(string title, string author, string rating)
{
this.title = title;
this.author = author;
RatingSetter = rating;
RatingMethod(rating);
}
public string RatingSetter
{
get { return this.rating; }
set
{
if (value == "PG" || value == "PG-13" || value == "R")
{
rating = value;
}
else …Run Code Online (Sandbox Code Playgroud) 这是我的基础对象:
let resources = {
TEST_FLAG: false,
FRUIT: 'banana',
ID: 11
};
Run Code Online (Sandbox Code Playgroud)
我想通过 asetter和 a访问该对象的每个属性getter。我尝试在下面这样做:
let dynamicResources = resources
for (let key in resources) {
Object.defineProperty(dynamicResources, key, {
get() {
console.log(`[debug]: GET <${key}>, VALUE <${this[key]}>`);
return `${this[key]}`;
},
set(value) {
console.log(`[debug]: SET <${key}>, VALUE <${this[key]}>`);
this[key] = value;
}
});
}
Run Code Online (Sandbox Code Playgroud)
这个想法是getter和setter可以从具有任意数量属性的基础对象生成。
当我console.log()得到结果对象时,我得到这个:
{
TEST_FLAG: [Getter/Setter],
FRUIT: [Getter/Setter],
ID: [Getter/Setter]
}
Run Code Online (Sandbox Code Playgroud)
这表明工厂循环已经起作用。但是,当我这样做时:
dynamicResources.FRUIT = 'berry';
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
set: function set(value) { …Run Code Online (Sandbox Code Playgroud) 有没有办法避免getter在setterspring-boot 实体中,有时我的数据库中有很多列?
的值n被传递给我的void Cell::setValue(int value)函数但m_value不会改变。任何人都可以解释这里有什么问题吗?
文件读入
void SudokuPuzzle::readPuzzle(const char filenameIn[]) {
// Add code to read in a puzzle from the text file and store within the SudokuPuzzle object
ifstream inStream;
inStream.open(filenameIn);
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
int n;
inStream >> n;
m_grid[i][j].setValue(n);
}
}
Run Code Online (Sandbox Code Playgroud)
在单元格中设置值
void Cell::setValue(int value)
{
m_value = value;
if (m_value = 0)
{
m_given = true; …Run Code Online (Sandbox Code Playgroud) 在 javascript 中,您可以像这样设置对象属性 getters
const obj = {
get a() {
return 1
}
}
console.log(obj.a) // 1
Run Code Online (Sandbox Code Playgroud)
是否可以定义一个全局getter?就像是:
let a = get() { return 1 }
console.log(a) // 1
Run Code Online (Sandbox Code Playgroud)
或者,在节点 REPL 中, obj 显示为:{a: [Getter]},那么我可以使用某种构造函数:let a = new Getter(() => {return 1})
另外,我可以对 setter 做同样的事情吗?