我希望能够使用这种代码实例化我的应用程序中的任何对象:
SmartForm smartForm = SmartForm.Create("id = 23");
Customer customer = Customer.Create("id = 222");
Run Code Online (Sandbox Code Playgroud)
我现在正在讨论如果该对象不存在,Create()应该返回什么.
如果Create()返回一个空对象,那么这就是一种"空模式",我仍然可以在我的应用程序周围传递该对象并在其上调用方法,这使得使用此模型进行编程变得方便和容易
如果Create()返回null,那么我需要在每次实例化后检查对象是否等于null,这使得编程更乏味但更明确.这样做的一个问题是,如果你忘记检查null,你的应用程序可能会工作很长时间而你不知道你没有检查null,然后突然中断
如果创建()抛出一个异常,这是基本相同,返回空,但使编程更加的让你创建一个尝试,未来,finally块为每个实例乏味,但你可以扔不同类型的异常(你可以" (使用null解决方案)可能会冒泡,以便您可以更明确地处理UI上的深层错误,所以我认为这是最强大的解决方案,尽管会产生try/catch代码膨胀
所以它似乎是一种轻盈/稳健的权衡.有没有人有任何经验可以根据这个决定沿着这些方向做出决定?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestFactory234.Models
{
public class SmartForm : Item
{
public string IdCode { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public int LabelWidth { get; set; }
public SmartForm() { }
private SmartForm(string …Run Code Online (Sandbox Code Playgroud) 我正在做一些WebDriver + PageObject的东西.
(如果您不熟悉PageObjects,这是一种模式,您有一个类代表您网站上的每个页面,它使用域语言公开页面的所有功能,从测试中隐藏HTML内容.)
我想要变得懒惰,并且在我的抽象Page类中有一个'submit'方法,我的所有其他Pages都扩展了.我也希望这个方法能够新建下一个Page子类并返回它.
这是我在Page类中的内容:
def submitExpecting[P <: Page[P]](implicit m: Manifest[_]): P = {
driver.findElement(By.xpath("//input[@type='submit']")).click
m.erasure.getConstructor(classOf[WebDriver]).newInstance(driver).asInstanceOf[P]
}
Run Code Online (Sandbox Code Playgroud)
这就是我如何称呼它:
val userHomePage = userSignupPage
.login("graham")
.acceptTermsAndConditions
.submitExpecting[UserHomePage]
Run Code Online (Sandbox Code Playgroud)
编译这个,我得到:
error: could not find implicit value for parameter m: Manifest[_]
.submitExpecting[UserHomePage]
Run Code Online (Sandbox Code Playgroud)
我以为自己很聪明,但显然我不是.;) 我究竟做错了什么?
例:
Foo make_foo(int a1, int a2){
Foo f(a1,a2);
return f;
}
Run Code Online (Sandbox Code Playgroud)
多次看过这样的功能,只是编码风格/偏好的问题,还是有更多的东西而不是眼睛?具体来说,这个答案让我思考make_unique实施和声称它是异常安全的 - 是否与创建和返回的分裂有关?还是我读了太多这个?为什么不简单写
Foo make_foo(int a1, int a2){
return Foo(a1,a2);
}
Run Code Online (Sandbox Code Playgroud) 今天要了解一个新事物,我们可以使用new运算符创建整数,如下所示
int num = new int();
Run Code Online (Sandbox Code Playgroud)
现在我想知道我是否以这种方式创建一个整数,然后生成的整数将是值类型或引用类型?我想这将是一种价值类型.我尝试了下面的代码
int num1 = 10;
int num2 = new int();
int num3;
num1 = num2;
num2 = num3;
Run Code Online (Sandbox Code Playgroud)
我得到了以下构建错误:
使用未分配的局部变量'num3'
我知道为什么会出现这个构建错误.但我想知道何时以及如何使用new int()以及这究竟是如何工作的?有人可以对此有所了解吗?
感谢和问候 :)
我只是在一条简单的线上浪费了数小时,导致数据丢失 我有AnotherClass持有MyClass实例的向量.这个AnotherClass通过以下方式实例化MyClass的对象:
AnotherClass::AnotherClass(){
MyClass myObject(...);
myVector.push_back(&myObject);
}
Run Code Online (Sandbox Code Playgroud)
之后,myObject的地址被推送到一个向量(与MyClass的其他实例一起),就像在代码中编写的一样.当我开始使用AnotherClass的实例时,我注意到MyClass的值丢失了(完全随机).当我将代码更改为:
AnotherClass::AnotherClass(){
MyClass* myObject = new MyClass(...);
myVector.push_back(myObject);
}
Run Code Online (Sandbox Code Playgroud)
我没有数据丢失.
有人可以这么好解释为什么第二种创建对象的方法不会导致数据丢失吗?(没有引用我的1.000页的书籍)
我正在更改我们的Java类,我注意到以下代码行:
OurClass<OurInterface1> ourClass = new OurClass<OurInterface1>() {};
Run Code Online (Sandbox Code Playgroud)
我对这一行感到奇怪的是,这OurClass是一个抽象类 - 这里的定义是OurClass:
public abstract class OurClass<T extends OurInterface1> implements OurInterface2<T>
Run Code Online (Sandbox Code Playgroud)
当我删除{}行尾时,Eclipse告诉我Cannot instantiate the type OurClass<OurInterface1>,但是当我放{}回去时,一切都很好.
如何{}允许您实例化一个抽象类?
我想实例化一个包并将函数传递给它重载运算符,以便创建一个通用二进制搜索树.这是规格.
bstgen.ads(片段)
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Unchecked_Deallocation;
GENERIC
type Akey is private;
type TreeRecord is private;
with function "<"(K: in Akey; R: in TreeRecord) return Boolean;
with function ">"(K: in Akey; R: in TreeRecord) return Boolean;
with function "="(K: in Akey; R: in TreeRecord) return Boolean;
PACKAGE BSTGen IS
TYPE St10 IS NEW String(1..10);
TYPE TreePt IS PRIVATE;
PACKAGE EnumIO IS NEW Ada.Text_IO.Enumeration_IO(Akey); USE EnumIO;
Run Code Online (Sandbox Code Playgroud)
driver.adb(片段)
with Ada.Text_IO; use Ada.Text_IO;
WITH BSTGen;
PROCEDURE Driver IS
IP: Integer := 1; …Run Code Online (Sandbox Code Playgroud) 编译器知道这AbstractDemo是一个抽象类,而抽象类无法实例化.
但是当我调用newInstance()方法时,为什么它没有给出编译时错误?
import java.lang.reflect.Constructor;
public abstract class AbstractDemo{
public AbstractDemo(){
System.out.println("Default constructor");
}
public static void main(String args[]){
try{
/* No compilation error for this statement */
AbstractDemo demo = AbstractDemo.class.newInstance();
Constructor[] ctors = AbstractDemo.class.getDeclaredConstructors();
for ( int i=0; i < ctors.length; i++){
System.out.println(ctors[i]);
/* No compilation error for this statement too */
AbstractDemo demo1 = (AbstractDemo) ctors[i].newInstance();
}
/* Compilation error here */
// AbstractDemo demo2 = new AbstractDemo();
}catch(Exception err){
err.printStackTrace();
}
}
} …Run Code Online (Sandbox Code Playgroud) 我想在python2.x中构建简单的分数计算器
from fractions import Fraction
class Thefraction:
def __init__(self,a,b):
self.a = a
self.b =b
def add(self):
return a+b
def subtract(self,a,b):
return a-b
def divide(self,a,b):
return a/b
def multiply(self,a,b):
return a/b
if __name__=='__main__':
try:
a = Fraction(input('Please type first fraction '))
b = Fraction(input('Please type second fraction '))
choice = int(input('Please select one of these 1. add 2. subtract 3. divide 4. multiply '))
if choice ==1:
print(Thefraction.add(a,b))
elif choice==2:
print(Thefraction.subtract(a,b))
elif choice==3:
print(Thefraction.divide(a,b))
elif choice==4:
print(Thefraction.multiply(a,b))
except ValueError:
print('Value error!!!!!') …Run Code Online (Sandbox Code Playgroud) 我做了一个简单的消息框,应向用户显示一条消息。它是一个预制件,可以做几件事,大部分是实例化时的动画。为了在实例化时运行代码,我使用了该Start()函数。当我已经知道要传达什么信息时,它就起作用了,但是我需要一个类似的东西constructor,该东西在之前运行Start(),但instantiation可以运行并且可以接受参数。现在,我完全意识到我可以实例化,设置消息并运行所有内容-因此在实例化它的地方使用3行代码,但是我很好奇是否还有另一个更合适的解决方案?我在网上发现的只是实例化,然后做点什么。
编辑:
我要求显示一个消息框:
var timeBox =
Instantiate(messageBox, penaltySpawnLoc.position, penaltyPrefab.transform.rotation, transform);
var scr = timeBox.GetComponent<MessageBox>();
scr.OnCreated(message);
Run Code Online (Sandbox Code Playgroud)
OnCreated进行初始化,显示动画,基本上所有内容。但它需要一个string输入知道什么露面,我不希望设置“对飞”的文本价值-它将使显得有些怪异闪烁时在MessageBox可见,但文本未设置。
编辑2:
最后一个参数transform的Instantiation是Canvas这个剧本是因为这是一个UI脚本。该参数意味着刚实例化的GameObject是该子对象。
编辑3:
timeBox只是的一个实例messageBox,它们是GameObject。一个消息框仅使用一次。其目的是与消息一起出现,并在0.5秒后消失并移开。离开后,它会自我毁灭。
instantiation ×10
c# ×3
c++ ×2
java ×2
.net ×1
abstract ×1
ada ×1
class ×1
creation ×1
generics ×1
instance ×1
instanceof ×1
manifest ×1
new-operator ×1
object ×1
pointers ×1
python ×1
reflection ×1
return ×1
scala ×1
syntax ×1
value-type ×1