gpt4 book ai didi

java - 在eclipse中意外生成getter和setter

转载 作者:行者123 更新时间:2023-11-30 10:29:18 25 4
gpt4 key购买 nike

public class GetterAndSetter {
private int bestScore;
private int cDate;

//use eclipse generate getter and setter
public int getBestScore() {
return bestScore;
}
public void setBestScore(int bestScore) {
this.bestScore = bestScore;
}
public int getcDate() {//why not be getCDate?
return cDate;
}
public void setcDate(int cDate) {//why not be setCDate?
this.cDate = cDate;
}
}

会不会导致反射失败?为什么eclipse会这样,我也不知道为什么?

最佳答案

这不是eclipse的问题,是getter和setter的逻辑问题。

这是正确的,但为了更好的实践,不要将前两个字母大写,您可以在这里查看 Java Tip #6 - Don't capitalize first two letters of a bean property name

This is in our java standards. You should not create a java bean property name that begins with a capital letter in the 1st two places. It can lead to confusing results. We had this happen a few times & finally added it to our standards & enforce it in code reviews. One place we saw problems was in struts. The form bean properties are used in the JSP page, but the Struts framework has to use the getter() & setter() to interact with the bean. This mapping happens based on the java bean spec & in certain cases can cause a method not found error if the developer doesn't name the method just right. The java bean spec provides guidelines on how to map between property and the associated getter() & setter().

关于java - 在eclipse中意外生成getter和setter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44066818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com