初学者界面问题 [英] Beginner interface question

查看:57
本文介绍了初学者界面问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读接口,我遇到的一个例子显示

你可以隐藏从类中接口实现的方法

实现它。要使用它,你必须转换为界面类型

例如:


界面IFoo

{

void display();

}


class Blah:IFoo

{

void IFoo.display()

{

System.Console.WriteLine(" hello");

}

}


类MainApp

{

public static void Main()

{

Blah myBlah = new Blah();


myBlah.display(); //错误

((IFoo)myBlah)。display(); //作品

}

}


在什么情况下你会想要这样做?我找到的地方

这并没有解释为什么会这样做。

对不起,如果这些问题遗漏明显:)。


克里斯

I''ve been reading up on interfaces, one example I came across showed
that you can hide a method implemented from an interface from the class
which implements it. To use it, you must cast to the interface type
e.g.:

interface IFoo
{
void display();
}

class Blah : IFoo
{
void IFoo.display()
{
System.Console.WriteLine("hello");
}
}

class MainApp
{
public static void Main()
{
Blah myBlah = new Blah();

myBlah.display(); //error
((IFoo)myBlah).display(); //works
}
}

In what circumstances would you ever want to do this? The place I found
this didn''t explain why this would ever be done.
Sorry if these questions are missing the obvious :).

Chris

推荐答案

以这种方式使用界面的情况是什么时候

需要限制方法的可访问性。


例如,假设你的Blah class是一个

数据库的抽象。该类中的内容将是删除,创建,更新,

插入,选择等。功能。现在谁使用Blah对象可以

如果不小心,会对数据库造成严重损害。所以

限制你可能想要创建界面的伤害。可能的

接口如下:


IDbAdmin - >可以访问所有功能

IReadOnlyUser - >可以访问选择仅限功能。

IUpdateOnlyUser - >可以访问更新 /插入仅限功能。

ICreateOnlyUser - >可以访问创建仅限功能。

等..


您也可以选择不创建删除按钮。界面,因为你可以

决定只有管理员可以访问该功能。


我相信这就是他们所谓的FACADE模式。


希望这是一个很好的实际例子。


享受,

Josh Go

The circumstances you would use an Interface in such a manner, is when
there is a need to limit accessibility to methods.

For example, suppose your "Blah" class was an abstraction of a
Database. Inside that class would be "Delete", "Create", "Update",
"Insert", "Select" functions. Now whoever uses the "Blah" object can
do some serious damage to the Database if they are not careful. So to
limit damage you might want to create an interface. Possible
interfaces would be as follows :

IDbAdmin -> Has access to all functions
IReadOnlyUser -> Has access to "Select" functions only.
IUpdateOnlyUser -> Has access to "Update" / "Insert" functions only.
ICreateOnlyUser -> Has access to "Create" functions only.
etc..

You can also chose to not create a "Delete" interface because you may
decide only admins have access to that feature.

I believe this is what they call a FACADE pattern.

Hope this was a good practical example.

enjoy,
Josh Go


我有同样的问题,并且无法弄明白我是新来的

接口,


类:


1-DataRepository类 - 包含(GetClientInfo方法,GetOrderInfo

方法)

2 - 客户类

3阶类


DataRepository类具有处理

数据库的所有函数,例如(Client& Order)数据库方法。


必需:


使客户端类只能访问GetClientInfo方法

而不是GetOrderInfo方法和副反之亦然


如何使用界面?


你可以在代码中显示它,因为我接口新手


分享让所有与众不同


-

通过.NET新闻组发送
http://www.dotnetnewsgroups.com
I have the same Problem, and cannt figure it out as i am new to
Interfaces,

Classes:

1-DataRepository Class - Contains (GetClientInfo Method, GetOrderInfo
Method)
2-Client Class
3-Order Class

The DataRepository Class has all the Functions that deals with the
Database such as (Client & Order) database methods.

Required:

make the Client Class have only Access to the GetClientInfo Method
and not also the GetOrderInfo Method and vice versa

How to do it using Interfaces??

can u please show it in code, as i am new to Interfaces

Sharing makes All the Difference

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com


嘿戴夫,


我回复了你关于FACADE的原帖。


这是一些示例代码。在这个例子中,我创建了一个TextFile。对象

这是磁盘上普通文本文件的抽象。现在,您计算机上的每个文件

都有访问权限。权利。我用接口模拟了那些访问权限

。因此,在该示例中,存在IReadOnlyUser,

IWriteOnlyUser和IFileAdmin。接口。前两个接口

非常明显,而IFileAdmin只是一个具有读/写功能的接口




使用系统;


命名空间CS_InterfaceTest

{

#region接口


interface IFileAdmin:IReadOnlyUser,IWriteOnlyUser {}

interface IReadOnlyUser

{

void PrepareForRead();

string ReadLine();

void CloseRead();

}

interface IWriteOnlyUser

{

void PrepareForWrite();

void WriteLine(string p_String);

void CloseWrite();

}


#endregion


#region textfile类


类TextFile:IFileAdmin

{

私有字符串m_Filename;

私有System.IO.StreamReader m_StreamReader;

私有System.IO.StreamWriter m_StreamWriter;


#region properties


公共字符串文件名

{

get {return m_Filename;}

set {m_Filename = value; }

}


#endregion


#region读取权限


void IReadOnlyUser.PrepareForRead()

{

m_StreamReader = new System.IO.StreamReader(this.Filename);

}

string IReadOnlyUser.ReadLine()

{

string buff = null;

if(m_StreamReader.Peek()! = -1)

{

buff = m_StreamReader.ReadLine();

}

返回buff;

}

void IReadOnlyUser.CloseRead()

{

m_StreamReader.Close();

}


#endregion


#region写访问权限

void IWriteOnlyUser.PrepareForWrite()

{

m_StreamWriter = new System.IO.StreamWriter(this.Filename,

true);

}

void IWriteOnlyUser.WriteLine(string p_String)

{
m_StreamWriter.WriteLine(p_String);

}

void IWriteOnlyUser.CloseWrite()

{

m_StreamWriter.Close();

}

#endregion


}


#endregion


#region main /测试代码

class MainClass

{

[STAThread()]

public static void Main()

{

TextFile file = new TextFile();

file.Filename =" foo.txt";


Console.WriteLine(" ---- Admin Test ----");

FullyTrustedUser((IFileAdmin)file);


Console.WriteLine("");


Console.WriteLine( ----写测试----;;

SemiTrustedUser((IWriteOnlyUser)文件);


Console.WriteLine(" ");


Console.WriteLine(" ---- Read Test ----");

UntrustedUser((IReadOnlyUser)file );

}


//演示读/写a ccess

public static void FullyTrustedUser(IFileAdmin p_Admin)

{

//写点什么

p_Admin.PrepareForWrite( );

p_Admin.WriteLine(我是管理员);

p_Admin.CloseWrite();


/ /读出来

p_Admin.PrepareForRead();

string buff = p_Admin.ReadLine();


while(buff != null)

{

Console.WriteLine(buff);

buff = p_Admin.ReadLine();

};

p_Admin.CloseRead();

}


//演示读取权限

public static void UntrustedUser(IReadOnlyUser p_Read)

{

p_Read.PrepareForRead();

string buff = p_Read.ReadLine();


while(buff!= null)

{

Console.WriteLine(buff);

buff = p_Read.ReadLine();

};

p_Read.CloseRead();

}


//演示写访问权限

public static v oid SemiTrustedUser(IWriteOnlyUser p_Write)

{

p_Write.PrepareForWrite();

p_Write.WriteLine(我不完全信任) ;

p_Write.CloseWrite();

}

}

#endregion

}

Hey Dave,

I replied to your original post about FACADEs.

Here is some sample code. In this example I create a "TextFile" object
which is an abstraction of a normal text file on disk. Now every file
on your computer has "Access" rights. I simulated those access rights
with interfaces. So in this example, there is a "IReadOnlyUser",
"IWriteOnlyUser", and "IFileAdmin" interface. The first 2 interfaces
are pretty self-explanatory and the IFileAdmin is just an interface
that has both read/write capabilities.

using System;

namespace CS_InterfaceTest
{
#region interfaces

interface IFileAdmin : IReadOnlyUser, IWriteOnlyUser{}

interface IReadOnlyUser
{
void PrepareForRead();
string ReadLine();
void CloseRead();
}
interface IWriteOnlyUser
{
void PrepareForWrite();
void WriteLine(string p_String);
void CloseWrite();
}

#endregion

#region textfile class

class TextFile : IFileAdmin
{
private string m_Filename;
private System.IO.StreamReader m_StreamReader;
private System.IO.StreamWriter m_StreamWriter;

#region properties

public string Filename
{
get{return m_Filename;}
set{m_Filename = value;}
}

#endregion

#region read access

void IReadOnlyUser.PrepareForRead()
{
m_StreamReader = new System.IO.StreamReader(this.Filename);
}
string IReadOnlyUser.ReadLine()
{
string buff = null;
if( m_StreamReader.Peek() != -1 )
{
buff = m_StreamReader.ReadLine();
}
return buff;
}
void IReadOnlyUser.CloseRead()
{
m_StreamReader.Close();
}

#endregion

#region write access
void IWriteOnlyUser.PrepareForWrite()
{
m_StreamWriter = new System.IO.StreamWriter(this.Filename,
true);
}
void IWriteOnlyUser.WriteLine(string p_String)
{
m_StreamWriter.WriteLine(p_String);
}
void IWriteOnlyUser.CloseWrite()
{
m_StreamWriter.Close();
}
#endregion

}

#endregion

#region main/testing code
class MainClass
{
[STAThread()]
public static void Main()
{
TextFile file = new TextFile();
file.Filename = "foo.txt";

Console.WriteLine("---- Admin Test ----");
FullyTrustedUser((IFileAdmin)file);

Console.WriteLine("");

Console.WriteLine("---- Write Test ----");
SemiTrustedUser((IWriteOnlyUser)file);

Console.WriteLine("");

Console.WriteLine("---- Read Test ----");
UntrustedUser((IReadOnlyUser)file);
}

//demonstrate read/write access
public static void FullyTrustedUser(IFileAdmin p_Admin)
{
//Write something
p_Admin.PrepareForWrite();
p_Admin.WriteLine("I am the admin");
p_Admin.CloseWrite();

//Read it out
p_Admin.PrepareForRead();
string buff = p_Admin.ReadLine();

while( buff != null)
{
Console.WriteLine(buff);
buff = p_Admin.ReadLine();
};
p_Admin.CloseRead();
}

//demonstrate read access
public static void UntrustedUser(IReadOnlyUser p_Read)
{
p_Read.PrepareForRead();
string buff = p_Read.ReadLine();

while( buff != null)
{
Console.WriteLine(buff);
buff = p_Read.ReadLine();
};
p_Read.CloseRead();
}

//demonstrate write access
public static void SemiTrustedUser(IWriteOnlyUser p_Write)
{
p_Write.PrepareForWrite();
p_Write.WriteLine("I am not fully trusted");
p_Write.CloseWrite();
}
}
#endregion
}


这篇关于初学者界面问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆