到目前为止,我知道我们实现接口,但今天有人(你知道是谁,我猜)告诉我,如果我编写一个接口,那么我们在类中实现它,但如果它是一个系统接口,那么我们可以说,INotifyPropertyChanged我们称之为 A 类继承 Interface INotifyPropertyChanged。
虽然我感觉是对的,但我不确定也不知道如何向他解释。
我需要在我的设计文档中指定。所以想知道我该提什么,继承还是实现。
我正在尝试用 Java 解析 XML 文件,在获取DocumentBuilder对象后,我调用它的 parse 方法来获取对象Document。例如Document dom = docbuild.parse(fileName);
然后,为了获取 XML 文件的根,我使用方法dom.getDocumentElement();。既然Documentjavadoc中定义了一个接口,那么我们如何能够在不先定义它的情况下调用它的方法呢?
我的主要目标是创建一个继承 Document 接口的类,因此我必须实现它。我该怎么做呢?
我在不同的命名空间下有以下类。我的意思是,相同的 3 个类存在于不同的命名空间下。
public class A
{
public int a { get; set; }
}
public class B
{
public A objA { get; set; }
}
public class C
{
public List<B> listBinC { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
为了在这些类的对象之间利用/操作,我想到编写一个接口包装器,例如
public interface iA
{
int a { get; set; }
}
public interface iB<T> where T: iA
{
T objA { get; set; }
}
public interface iC<T> where T : iB<iA>
{
List<T> listBinC {get; set; }
}
Run Code Online (Sandbox Code Playgroud)
之后我将我的类定义更改为 …
我在 goQA 包中定义了一个简单的接口,并将其与实现该接口的结构体一起使用:
type ReportWriter interface {
Name() string
Init(parent ITestManager)
onManagerStatistics(report *ManagerResult, stats *ReporterStatistics, name, msg string)
}
type MongoReporter struct {
}
func (t *MongoReporter) Name() string {
}
func (t *MongoReporter) Init(parent ITestManager) {
}
func (t *MongoReporter) onManagerStatistics(report *ManagerResult, stats *ReporterStatistics, name, msg string) {
}
Run Code Online (Sandbox Code Playgroud)
然后我可以在示例文件中创建一个变量,一切正常:
var mr goQA.ReportWriter
mr = &goQA.MongoReporter{}
Run Code Online (Sandbox Code Playgroud)
当将结构移动到它自己的包 mongo 并导入 goQA 包时,问题就出现了。除了使用包名称之外,一切都相同:
type MongoReporter struct {
}
func (t *MongoReporter) Name() string {
}
func (t *MongoReporter) Init(parent goQA.ITestManager) { …Run Code Online (Sandbox Code Playgroud) 我希望能够将接口的 Type 对象作为 ConverterParameter 传递给我的类型转换器。我的想法是,我希望转换器根据源值是否实现特定接口返回一个值。
如果我将类型名称作为字符串传递,我有一个方法可以工作,但我宁愿让它成为一个强类型值,以避免潜在的维护问题,并进行编译时检查,而不是依赖运行时的故障。
这是我使用字符串的转换器:
class InterfaceToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null)
{
var type = value.GetType();
if (type.GetInterface(parameter as string) != null)
{ return Visibility.Visible; }
}
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Run Code Online (Sandbox Code Playgroud)
除了上述潜在的维护问题之外,它的效果很好。
这是您在 xaml 中使用它的方式:
<TabItem Header="General"
Visibility="{Binding ConverterParameter=IMyType, Converter={StaticResource InterfaceToVisibilityConverter}}">
<TextBlock Text="Whatever Content Goes here."/>
</TabItem>
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,它检查当前的 DataContext …
考虑以下接口
public interface ISample
public interface ISample2 : ISample
public class A
{
[Field]
ISample SomeField {get; set;}
[Field]
ISample2 SomeOtherField {get; set; }
}
Run Code Online (Sandbox Code Playgroud)
假设有各种类(如 A 类)和各种字段(如 SomeField 和 SomeOtherField)。如何获取 ISample 类型或从 ISample 派生的其他接口(如 ISample2)的所有此类字段的列表
我有一个下面的函数,我想使其通用:
func genericUnmarshalForType1(file string) Type1 {
raw, err := ioutil.ReadFile(file)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
var type1 Type1
json.Unmarshal(raw, &type1)
}
Run Code Online (Sandbox Code Playgroud)
我想创建一个接受 Type1 或 Type2 的函数,而不需要为每种类型创建一个函数。我怎样才能做到这一点?
我一直在研究 IEqualityComparer 和 IEquitable。
来自诸如IEqualityComparer<T> 和 IEquatable<T> 之间有什么区别?两者之间的区别现在很明显了。“IEqualityComparer 是一个对象接口,用于对 T 类型的两个对象执行比较。”
按照https://msdn.microsoft.com/en-us/library/ms132151(v=vs.110).aspx中的示例,IEqualityComparer 的目的非常明确且简单。
我按照https://dotnetcodr.com/2015/05/05/implementing-the-iequatable-of-t-interface-for-object-equality-with-c-net/上的示例来了解如何使用它,我得到以下代码:
class clsIEquitable
{
public static void mainLaunch()
{
Person personOne = new Person() { Age = 6, Name = "Eva", Id = 1 };
Person personTwo = new Person() { Age = 7, Name = "Eva", Id = 1 };
//If Person didn't inherit from IEquatable, equals would point to different points in memory.
//This means this would be false as both …Run Code Online (Sandbox Code Playgroud) 正在努力使用打字稿为我的 Angular2 服务设置一些接口。遇到配置问题。
我希望能够像这样访问我的数据myItem['chickens']并取回以下对象:
{
name:"chicken",
price:1000,
names:["Harry", "Barry", "Larry"]
}
Run Code Online (Sandbox Code Playgroud)
如何以正确的方式编写界面,以便能够像前面提到的那样取回数据?
export interface StoreItem {
itemName:{
itemName: string;
price: number;
nameList: Array<string>;
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个类似的界面{} -
Rows interface{}
Run Code Online (Sandbox Code Playgroud)
在Rows界面中,我放置了ProductResponse结构。
type ProductResponse struct {
CompanyName string `json:"company_name"`
CompanyID uint `json:"company_id"`
CompanyProducts []*Products `json:"CompanyProducts"`
}
type Products struct {
Product_ID uint `json:"id"`
Product_Name string `json:"product_name"`
}
Run Code Online (Sandbox Code Playgroud)
我想访问 Product_Name 值。如何访问这个。我可以使用“ reflect ”pkg访问外部值(CompanyName、CompanyID)。
value := reflect.ValueOf(response)
CompanyName := value.FieldByName("CompanyName").Interface().(string)
Run Code Online (Sandbox Code Playgroud)
我无法访问Products结构值。怎么做?