为什么通过反射调用方法比创建一个接口然后通过反射调用它要慢得多.第一个版本显示了其他版本显示增强方式的繁琐方式?
// first version
class A
{
public void fn()
{
}
}
void Main(String[]x)
{
Type type = typeof(A);
object obj = Activator.CreateInstance(type);
type.InvokeMember("fn", BindingFlags.Public, null, obj, null);
}
//second verison
interface IA
{
void fn();
}
class A :IA
{
public void fn()
{
}
}
void Main(String []x)
{
Type type = typeof(A);
IA obj =(IA) Activator.CreateInstance(type);
obj.fn();
}
Run Code Online (Sandbox Code Playgroud) 出于学习目的,我试图从头开始构建一个"SingleView"应用程序.
几乎 - 我使用"EmptyApp"模板.我这样做是为了理解在iPhone应用程序中实例化视图的方式.
到目前为止,我得到以下内容:
我从"SingleViewApp模板"复制了脚本.但是当我测试应用程序时,我的视图仍未显示.
看起来我错过了一些东西.它必须是绑定或类似的东西,因为脚本本身应该是相同的.控制台也给我以下消息:
"应用程序在应用程序启动结束时应该有一个根视图控制器"
好.我如何告诉我的应用,采取我的观点并以root身份使用它?
谢谢伙计......感谢每一位帮助
mogio
我在我的应用程序中有以下代码.
public class GeneralInfo
{
private string _id;
private string _name;
public string id
{
set
{
_id = value;
}
get
{
return _id;
}
}
public string name
{
set
{
_name = value;
}
get
{
return _name;
}
}
}
public class SecureInfo
{
private string _password;
public string password
{
set
{
_password = value;
}
get
{
return _password;
}
}
}
public class User
{
}
Run Code Online (Sandbox Code Playgroud)
我需要在上面的代码中应用多重继承,即.应该可以在用户类中访问类GeneralInfo,SecureInfo属性.
我知道使用接口可以实现多重继承.但我需要在接口中限制的基类中定义属性.
我怎么能做到这一点?
我从"PersonFuntionality"界面创建了"personFuntionality"对象.Interface有一个保存人员详细信息的方法.问题是personFuntionality总是有一个空值.
public PersonFuntionality personFuntionality;
try
{
List<beans.Person> prsn = new List<beans.Person>();
beans.Person psn = new beans.Person();
psn.PersonId = Convert.ToInt32(txtId.Text.Trim());
psn.PersonName = txtName.Text.Trim();
psn.PersonCity = txtCity.Text.Trim();
prsn.Add(psn);
//prsn.PersonId = Convert.ToInt32(txtId.Text.Trim());
//prsn.PersonName = txtName.Text.Trim();
//prsn.PersonCity = txtCity.Text.Trim();
if (personFuntionality != null)
{
bool success = personFuntionality.SavePersonDetails(prsn);
if (success == true)
{
lblResult.Text = "success";
}
else
{
lblResult.Text = "Failed";
}
}
else
{
lblResult.Text = "Object Null";
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
Run Code Online (Sandbox Code Playgroud) 我是C#的新手.现在在阅读Interfaces时.我很困惑.我在MSDN上读到我们无法直接实例化接口.后来他们写了下面的例子.
Public interface ICat
{
meow();
}
Public class Cat : ICat
{
meow(){//do something }
}
////////////////////
static void main(){
Icat cat = new Cat();
Cat cat1 = nes Cat();
}
Run Code Online (Sandbox Code Playgroud)
如果我们不能直接实例化Interfaces那么Icat cat = new Cat();这一行的含义是什么.那么这两者之间有什么区别?
我在我的对象中使用接口方法时遇到问题.我是一个快速的例子,没有所有的补充.
public class Item{}
public interface IFruit
{
void MethodExample();
}
public class Apple : Item, IFruit
{
public void IFruit.MethodExample(){}
}
// put this in a run method somewhere
var example_item = new Apple();
//Here comes the problem.
example_item.MethodExample();
// this will return an error saying that it cant find the method.
Run Code Online (Sandbox Code Playgroud)
无论如何要做到这一点?我知道它实现了i_fruit.并有方法.但我无法访问它?
让我们说在第三方库中我们有一个接口和一个实现这个接口的结构.我们还假设有一个函数将ParentInterface作为参数,它对不同类型具有不同的行为.
type ParentInterface interface {
SomeMethod()
}
type ParentStruct struct {
...
}
func SomeFunction(p ParentInterface) {
switch x := p.Type {
case ParentStruct:
return 1
}
return 0
}
Run Code Online (Sandbox Code Playgroud)
在我们的代码中,我们想要使用这个接口,但是使用我们的增强行为,所以我们将它嵌入到我们自己的结构中.编译器实际上允许我们ParentInterface直接在我的struct上调用函数:
type MyStruct struct {
ParentInterface
}
parentStruct := ParentStruct{...}
myStruct := MyStruct{parentStruct}
parentStruct.SomeMethod() // Compiler OK.
myStruct.SomeMethod() // Compiler OK. Result is same. Great.
SomeFunction(parentStruct) // Compiler OK. Result is 1.
SomeFunction(myStruct.ParentInterface) // Compiler OK. Result is 1.
SomeFunction(myStruct) // Compiler OK. Result is 0. (!)
Run Code Online (Sandbox Code Playgroud)
最后一例不是问题吗?我不止一次遇到过这种错误.因为我愉快地使用 …
让我们假设我有一个接口,其中包含以下几种方法:
public interface Person {
public void printName(String name);
public int getAge(String name);
public String getAddress();
}
Run Code Online (Sandbox Code Playgroud)
现在,实现上述接口的Test类如下所示:
public class Test implements Person{
}
Run Code Online (Sandbox Code Playgroud)
现在eclipse向我显示错误并强迫我添加未实现的方法.
现在我想在类中仅实现 接口printName()方法.我该怎么做.我的意思是问如何只实现所需的方法?PersonTest
注意我不想使用Abstract类.
如以下代码所示:is n := localplugin.NewPluginNet()的类型,它是由struct指针实现的接口。函数返回指向的nil指针。nlocalnet.Net*localnet.DomainSocketfunc NewPluginNet() localnet.Netn
var n localnet.Net
n = localplugin.NewPluginNet()
fmt.Println("----> ", n)
if n == nil {
fmt.Println("n is nil")
}else{
fmt.Println("n is not nil : ", n)
}
fmt.Println(reflect.TypeOf(n), reflect.TypeOf(nil), n)
Run Code Online (Sandbox Code Playgroud)
以下是上面代码的输出。
----> <nil>
n is not nil : <nil>
*localnet.DomainSocket <nil> <nil>
Run Code Online (Sandbox Code Playgroud)
为什么n不为nil?
==================================
var n1 *localnet.DomainSocket
n1 = nil
fmt.Println(">> ", n1)
if n1 == nil {
fmt.Println(">>n1 is nil")
}else{
fmt.Println(">>n1 is not nil : ", n) …Run Code Online (Sandbox Code Playgroud) 令我惊讶的是,这在 Go 1.13 中编译(并运行)得很好:
var v interface{}
if v == "" { // false
fmt.Println("v is empty string")
}
Run Code Online (Sandbox Code Playgroud)
我总是觉得我需要在进行此类测试之前进行类型切换和/或断言。并在Go 操场上多戳一下:
v = 0
if v == 0 { // True
fmt.Println("v is 0")
}
type myType struct {
a string
b int
}
v = myType{}
// Only works if myType is comparable
if v == (myType{}) { // true
fmt.Println("v is empty myType")
}
Run Code Online (Sandbox Code Playgroud)
有人能告诉我为什么这是合法的吗?依赖这种行为是否安全?例如,如果我想测试一个变量是空字符串、零整数还是浮点数:
func isZero(v interface{}) bool {
return v == "" || v …Run Code Online (Sandbox Code Playgroud)