我试图用XmlSerializer做一个非常简单的序列化:
public struct XmlPerson
{
[XmlAttribute] public string Id { get; set; }
[XmlAttribute] public string Name { get; set; }
}
public class GroupOfPeople
{
private Dictionary<string, string> _namesById = new Dictionary<string, string>();
//pseudo property for serialising dictionary to/from XML
public List<XmlPerson> _XmlPeople
{
get
{
var people = new List<XmlPerson>();
foreach (KeyValuePair<string, string> pair in _namesById )
people.Add(new XmlPerson() { Id = pair.Key, Name = pair.Value });
return people;
}
set
{
_namesById.Clear();
foreach (var person in value) …Run Code Online (Sandbox Code Playgroud) 我一直在网上搜索这个,但我似乎找不到足够清楚的东西让我理解.我在Java中看到过类似的"类似"问题.
class animal{
private $name;
// traditional setters and getters
public function setName($name){
$this->name = $name;
}
public function getName(){
return $this->name;
}
// animal constructors
function __construct(){
// some code here
}
// vs
function __construct($name){
$this->name = $name;
echo $this->name;
}
}
$dog = new animal();
$dog->setName("spot");
echo $dog->getName();
// vs
$dog = new animal("spot");
Run Code Online (Sandbox Code Playgroud)
请注意......这是我第一次使用OOP进行Web开发和PHP,并且我试图通过编写一些代码让我的手"脏"来学习,以便我理解OOP中的某些事情.请保持简单.
我有一个类通过构造函数注入成员,其他通过setter注入.我似乎无法让Mockito注入二传手.注入的构造函数被模拟得很好,但是setter的返回为null.当我将setter-ed成员翻转到构造函数注入时,一切都很顺利.这是原始的生产代码:
@Autowired
private BetRepository betRepository;
public void setBetRepository(BetRepository betRepository) {
this.betRepository = betRepository;
}
public TournamentScoringCache(TournamentScoringCacheInitializer cacheInitializer,
ScoringEngineInitializer scoringEngineInitializer) {
tournamentUserStates = cacheInitializer.initCache();
scoringEngines = scoringEngineInitializer.initEngines();
}
public <T extends SideScore> void updateGameScore(Long tournamentId, Long gameId, MatchScore<T> score) {
Map<Long, UserTournamentState> userStates = tournamentUserStates.get(tournamentId);
ScoringEngine<?> scoringEngine = scoringEngines.get(tournamentId);
List<Bet> bets = betRepository.getBetsByGameId(gameId); //HERE IS WHERE I GET THE NPE
....
}
Run Code Online (Sandbox Code Playgroud)
测试代码:
@Mock
BetRepository betRepository;
@Mock
TournamentScoringCacheInitializer cacheInitializer;
@Mock
ScoringEngineInitializer engineInitializer;
@InjectMocks
private TournamentScoringCacheAndDB tournamentScoringCache;
@Test
public void testUpdateGameScore() { …Run Code Online (Sandbox Code Playgroud) 我似乎无法弄清楚如何使用Mockito模拟一个简单的setter方法.我有以下课程:
class MyClass {
private SomeObject someObject;
public void setSomeObject(SomeObject someObject) {
this.someObject = someObject;
}
public someObject getSomeObject() {
return someObject;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我只想在调用"setSomeObject"时设置一个新的"SomeObject"实例.此外,应忽略setter中的参数.
我需要这样的东西:
MyClass mockedClass = mock(MyClass.class);
when(mockedClass.setSomeObject([ignoreWhatsInHere]))
.then(mockedClass.setSomeObject(new SomeObject();
Run Code Online (Sandbox Code Playgroud)
但是,我似乎无法让语法为此工作.我只能使用getters()来运行模拟,因为那时我可以返回一些东西.但我无法弄清楚如何为setters()做同样的事情.
所有帮助赞赏.
考虑以下愚蠢的例子:
class MyClass
{
public:
template <class Function>
inline double f(double x, Function&& function)
{
return function(x);
}
};
Run Code Online (Sandbox Code Playgroud)
有了这个类,我可以MyClass::f(x, function)使用lambda函数调用它来执行它x,我希望没有开销.我的问题是:function作为一个可设置成员的等价物是什么MyClass?
class MyClass
{
public:
inline double f(double x)
{
return _function(x);
}
// What are the setter and the type of the protected member _function ?
};
Run Code Online (Sandbox Code Playgroud) 最近我一直在写一些涉及使用getter和setter的 JavaScript程序.我阅读了两种方法的MDN文档,但是我很困惑尝试使用它们.
简而言之,我只想创建一系列类似的属性,它们具有相同的getter和setter,但我不想为每个属性重写每个setter和getter.
说明以上内容,我尝试执行以下代码:
var defaultProperty = {
set: function(val) {
this.value = val - 1; // Just an example
},
get: function() {
return this.value + 1; // Just an example
}
};
var myObj = {};
Object.defineProperties(myObj, {
foo: defaultProperty,
bar: defaultProperty
});
Run Code Online (Sandbox Code Playgroud)
然后我为我foo和bar属性分配了新值,如下所示:
myObj.foo = 3;
myObj.bar = 7;
Run Code Online (Sandbox Code Playgroud)
最后,我期待这样的事情:
console.log(myObj.foo, myObj.bar);
> 3 7
Run Code Online (Sandbox Code Playgroud)
但我意外地得到了这个:
> 7 …Run Code Online (Sandbox Code Playgroud) 我知道如何将存储属性的"setter"设置为private(例如public private(set) var name: String = "John"),但是如何将计算属性的"setter"设置为private?在这种情况下,变量' age' 的'setter '.当我试图将关键字私有放在前面时set(newAge){},XCode会显示错误.那么可以将计算属性的"setter"设置为private吗?
public class Person {
public private(set) var name: String = "John"
var age: Int{
get {
return 10
}
set(newAge){ // how to set this setter to private so to restrict modification
}
}
}
Run Code Online (Sandbox Code Playgroud) 仍然习惯了Entity框架,但我已经看到了类似下面的代码,他们在Entity中拥有id的私有setter.
public int Id { get; private set; }
public string FirstName { get; set; }
public string LastName { get; set; }
Run Code Online (Sandbox Code Playgroud)
为什么有些人会有私人制定者.这个Id字段无论如何都是在数据库中自动生成的,这是因为它设置为私有?
另外,为什么我们需要私有构造函数和公共构造函数,如下所示?
private Emp() { }
public Emp(string name, string lastname)
{
FirstName = firstname;
LastName = lastname;
}
Run Code Online (Sandbox Code Playgroud) SonarQube 仪表板中是否有允许忽略 getter 和 setter 的设置?这听起来是比在代码库中的每个方法上编码 //nopmd 更好的选择。
我的代码库中有很多,它们大大降低了我在 Sonarqube 仪表板中报告的单元测试覆盖率
我是一名主要用 Java 开发的后端开发人员,所以我被教导使用 setter/getter 而不是直接访问类的属性。
现在我进入前端世界,现在进入 js/ts。我见过很多人直接访问对象变量,而不像在 Java 中那样使用 setter 和 getter,例如this.person.name.
为什么是这样?如果您不需要添加额外的代码而只是为了获取值或设置它,那么在 ts 和 js 上使用 getter/setter 有什么优势吗?
谢谢。
setter ×10
getter ×5
constructor ×3
c# ×2
javascript ×2
mockito ×2
architecture ×1
c++ ×1
c++11 ×1
collections ×1
java ×1
lambda ×1
overwrite ×1
php ×1
sonarqube ×1
spring ×1
swift ×1
typescript ×1