在我的一次面试中,我有一个问题:
classA{
// some instructions
ClassA a = new ClassA();
}
ClassB{
//Some instructions
ClassA a1 = new ClassA(); // Here I should throw a compilation or runtime error.
}
我能做这个吗?
我在Scala中遇到了如下代码片段:
final class Test private (fn: Int => Int) {
def square(i: Int) = i * i
}
object Test {
def apply(fn: Int => Int) = new Test(fn)
}
那意味着什么?在关于Scala的文本材料中,我找不到对此的任何引用。
编辑:在这个链接中找到了我想要的内容:
假设我正在构建一个不可变的Yahtzee记分卡类:
public final class Scorecard {
private Map<Category, Integer> scorecard = new HashMap<Category, Integer>();
public Scorecard() {
// Instantiates a new empty scorecard
}
private Scorecard(Map<Category, Integer> scorecard) {
public class A extends B {
private A(B b) {
super(b);
}
public static A parse(string s) // question 1
{
B result = D.parse(s);
return new A(result); // question 2
}
}
问题1:我不明白什么类型的构造函数(?)是这个吗。我对普通构造函数的看法类似于public A().,但这个构造函数是:
public static A parse(str
我有一个抽象类Employee,它有两个具体的子类,MinorEmployee和AdultEmployee。我知道如何在Employee中创建一个静态工厂方法来实例化一个具体子类的实例:
public abstract class Employee() {
public static Employee create(LocalTime birthdate) {
if (/* omitted */) {
return new MinorEmployee();
} else {
return new Adult
我对C#语言很陌生,在一个网站上看到了这种声明。我还没见过这种构造函数(?)以前,我真的很困惑
public class Employee {
public string Name { get; set; }
public string Title { get; set; }
public static Employee GetEmployee() { //Constructor? Method?
var emp = new Employee() {
Name = "Somebody", Title =
我有一个基类,它被很多地方使用:
class base {
base(param1, param2 ...) // constructor 1, which is the legit constructor
base(param3, param4 ...) // constructor only for unit testing!!!
base(param5, param6 ...) // constructor only for unit testing!!!
}
有没有一种优雅的方法来处理这种情况?有些构造器只用于测试,这真的很令人困惑。我简化了上面的例子,实际上我们有多个
我有一个类Foo和一个静态类FooFactory,它用于通过以下API创建Foo和Foo派生类的实例:
public static class FooFactory {
public static T Create<T>() where T : Foo, new() {
...
return new T();
}
}
new()类型参数的T专门化要求Foo具有一个public默认构造函数。换句话说,没有什么可以阻止Foo的用户通过构造函数直接初始化类。
但是,FooFactory的目的是跟踪所有Foo实例,因此我希望强制用户通过FooFactory创建所
域中的所有实体都需要有身份。通过继承DomainEntity,我能够向类提供标识。
City域实体(为便于阅读而简化):
public class City : DomainEntity, IAggregateRoot
{
public string Name { get; private set; }
public Coordinate Coordinate { get; private set; }
public City(string name, decimal latitude, decimal longitude)
{
Name
我正在尝试为我的问题找出最佳实践。我在一个类中有多个创建新对象的方法:
public void ToBeTested()
{
ClassForExmaple classForExample = new ClassForExample();
//Other logic.....
}
public void ToBeTested2()
{
ClassForExmaple2 classForExample2 = new ClassForExample2();
//Other lo
我有一个班级游戏,这是我的主课和二等卡。类卡拥有它的属性和构造函数私有,只有函数在它是公共的。函数init检查值是否合理,如果一切都好于构造函数,则获取值并创建对象。现在我想在课堂游戏中从类卡中创建一个对象。我该怎么做?
这是我的代码:
班级游戏:
import java.util.List;
import java.util.Vector;
public class Game {
public static void main(String[] args)
{
/*
CREATING NEW CARD OBJECT
*/
int value = 13;
V
也有类似的问题,但似乎都不是这样的。
我有一个包含S的包装类。这是我们最简单的形式
// A simple class with a two-argument constructor:
struct S {
int x[2];
S(int x, int y) : x{x, y} {}
};
struct WrappedSSimple {
S s;
template <typename... Args>
WrappedSSimple(Args&&... args) : s(std::forward<Args>(ar
我有一门根据服务而定的课程。我已将其登记如下:
container.Register<IMain, Main>();
container.Register<IChild, Child>();
在为该类编写测试时,我希望在IChild中模拟其中一个方法。所以我有mockIChild.Setup(...etc)。
但是您不能注册模拟类型,所以我必须在测试中手工实例化Main,对吗?
或者,在简单的喷射器或MSTest的深度中有什么神奇的东西可以做相当于:
[TestMethod]
public void foo()
{
var mockChild = new Moc
class XFactory {
private XFactory() {}
static void getX() {
if(...)
return new A(new XFactory());
else
return new B(new XFactory());
}
}
class A {
private A() {}
public A(XFactory xf) {}
}
class B {
private B() {}
public A(XFactory xf) {}
}
通过这种方式,我可以确保只有Fa
我有一个DataProvider类(DAL),它需要"year“作为参数。它是这样使用的:
using (var provider = new DataProvider(year))
{
provider.SomeRepostitory.DoSomethingUsefull();
}
DataProvider构造函数代码处理配置-因此它可以抛出异常。并且不推荐使用异常可抛出的构造函数。因此,我添加了Init方法,并将所有可抛出的代码放在那里:
var provider = new DataProvider();
provider.Init(year);
但现在有两行代码,而不是
我有一个特殊的情况,一个对象需要在每个线程的基础上是一个单例。所以我想使用Factory类的静态工厂方法来实例化这些实例。为了确保使用工厂类(因为它是按线程缓存的),构造函数需要受到保护。
假设我有一个这样的类。
public class XXXX : Model {
protected XXXX() {
}
}
我想使用像这样的工厂类。
public class Factory {
private static Dictionary<int,Model> _singletons;
public static T Instance() whe
我有遵循这种模式的代码。
Class Factory
build:
returns: Add|Remove|Stub
takes: property1, property2, property3
Class Add
execute: does something
takes: property2, propery3
Class Remove
execute: does something
takes: property2
Class Null
execute: does nothing
最近,我一直在考虑保护我的一些代码。我很好奇如何才能确保对象永远不会被直接创建,而只能通过工厂类的某个方法来创建。假设我有一些“业务对象”类,我想确保这个类的任何实例都有一个有效的内部状态。为了实现这一点,我需要在创建对象之前执行一些检查,可能是在它的构造函数中。在我决定将此检查作为业务逻辑的一部分之前,一切都是可以的。那么,我如何安排一个业务对象只能通过我的业务逻辑类中的某个方法创建,而不能直接创建呢?与C#相比,使用C++这个古老的“朋友”关键字的第一个本能愿望是不够的。所以我们需要其他选择...
让我们来看一些例子:
public MyBusinessObjectClass
{
假设我有一个这样的类:
public class MyClass {
Dao dao;
public String myMethod(Dao d) {
dao = d;
String result = dao.query();
return result;
}
}
我想用mockito来测试它。因此,我创建了一个模拟对象,并以这种方式调用该方法进行测试:
Dao mock = Mockito.mock(Dao.class);
Mockito.when(mock.myMethod()).thenReturn(&
我想做以下几件事。
class MySuperClass {
final String s;
MySuperClass(this.s);
factory MySuperClass.from(int a) {
return MySuperClass(a.toString());
}
}
class MySubClass extends MySuperClass {
final int b;
MySubClass(int a, this.b) : super.from(a); // error
}
如何调用超类的工厂构造函数?
给出的错误是:
The