我对为什么我们将代码实现到接口的设计方法有疑问。这在原始数据类型中非常常见。就像我没有得到这两者之间的区别:
Map<Integer, String> mymap = new HashMap<Integer, String>();
Run Code Online (Sandbox Code Playgroud)
和
HashMap<Integer, String> mymap = new HashMap<Integer, String>();
Run Code Online (Sandbox Code Playgroud)
这两者有什么区别吗?我的意思是,mymap在这两种情况下,我们将要使用的每个地方都将保持不变。
如果问题似乎没有用,我很抱歉,但我真的不明白这将如何在以后使用的地方产生任何影响mymap。请帮忙?
谢谢..
注意 - 我已经在 SO 上看到了这个问题,但它没有给出我想要的。
我有一些工作代码,其中使用泛型已经开始使代码看起来比它应该的更混乱。
我是否有可能取消泛型并使用成员类型、构造函数类型、方法参数类型和方法返回类型的接口?
通用提供什么,上面使用接口不会提供什么?我知道泛型在检索元素时会进行编译时检查(以停止错误的转换 - 就像过去那样),但是编译器不会检测到我是否尝试插入不是所需接口的子类型的类型?
我知道为什么要使用接口和包装器。
但是我混淆了命名包装器类......(“谁是包装器?”我看我不太了解......)
public Interface A {
void method();
}
public Class B implements A {
void method() {
doSomething();
}
}
Run Code Online (Sandbox Code Playgroud)
我对两件事感到困惑......
Wrapper Class是 class ,所以 B 是包装器。
我们通常看到(或想到?)a.method() 而不是 b.method(),所以 A 是包装器。
什么是包装器?
一种?乙?
和...
如何使用“Wrapper”或“W”命名 A、B 好?
A、包装器?或B,BWrapper?还是其他人……?
我有一个UserControl有几个孩子UserControl的,而那些UserControl有孩子的 UserControl。
考虑一下:
MainUserControl
TabControl
TabItem
UserControl
UserControl
UserControl : ISomeInterface
TabItem
UserControl
UserControl
UserControl : ISomeInterface
TabItem
UserControl
UserControl
UserControl : ISomeInterface
TabItem
UserControl
UserControl
UserControl : ISomeInterface
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止所拥有的,但没有发现ISomeInterface:
PropertyInfo[] properties = MainUserControl.GetType().GetProperties();
foreach (PropertyInfo property in properties)
{
if (typeof(ISomeInterface).IsAssignableFrom(property.PropertyType))
{
property.GetType().InvokeMember("SomeMethod", BindingFlags.InvokeMethod, null, null, null);
}
}
Run Code Online (Sandbox Code Playgroud)
是否有可能通过反射UserControl从MainUserControl该实现中找到所有 childISomeInterface并void SomeMethod()在该接口上调用一个方法()?
我熟悉type castingin inheritance model.
让SuperClass and SubClass是父子类;
SuperClass superClass = new SubClass(); -- 这里object实例化的是一个subclass object; 但它reference type是SuperClass;这仅仅是对那些methods的SuperClass可以在被调用subclass object; 任何methods不在inherited/overridden中的subclass都不能被调用(这是 的任何唯一methods的subclass)。
我观察到与上面相同的行为 ifSuperClass is an interface和SubClass implements it。也就是说,只有在SuperClass interface中声明的方法才能在SubClass对象上调用。我的理解正确吗?但是对于一些casting,我可以调用methods它不是接口的一部分,我在下面的示例代码中观察到了这一点;
我已经对我的理解做了一些评论,它是如何工作的;但我想知道这是否有意义,或者我的解释是否错误;
class Animals {
public void bark(){
System.out.println("animal is barking"); …Run Code Online (Sandbox Code Playgroud) 我想了解一下 Bob 叔叔在Clean Code书中关于接口命名的内容:
这些有时是编码的特例。例如,假设您正在构建一个用于创建形状的抽象工厂。这个工厂将是一个接口,并由一个具体的类实现。你应该给它们起什么名字?IShapeFactory 和 ShapeFactory?我更喜欢让界面保持朴素。前面的 I,在今天的遗留文件中很常见,充其量是一种干扰,最坏的情况是信息过多。我不希望我的用户知道我正在给他们一个界面。我只是想让他们知道这是一个 ShapeFactory。因此,如果我必须对接口或实现进行编码,我会选择实现。称其为 ShapeFactoryImp,甚至是可怕的 CShapeFactory,都比对接口进行编码更可取。
他不以“I”开头的界面是什么想法,为什么他不希望用户知道他正在处理一个界面?
看起来您通常在java.lang.Comparable不指定类型参数的情况下实现接口。
public abstract class Area implements Comparable {
@Override
public int compareTo(Object other) {
if (other instanceof Area)
return new Double(getArea()).compareTo(other.getArea());
return -1; // or something else
}
abstract public double getArea();
}
Run Code Online (Sandbox Code Playgroud)
由于我只想将苹果与苹果进行比较,因此我认为指定类型是有意义的。
public abstract class Area implements Comparable<Area> {
@Override
public int compareTo(Area other) {
// ...
Run Code Online (Sandbox Code Playgroud)
如果我想介绍另一个类进行比较Area,我想我可以这样做:
public abstract class Area implements Comparable<Area>, Comparable<Volume> {
@Override
public int compareTo(Area other) {
// ...
}
@Override
public int compareTo(Volume other) {
// ...
} …Run Code Online (Sandbox Code Playgroud) 我有一个DTO,用于通过其键之一进行映射
DTO和SecurityMode枚举:
enum SecurityMode {
mode1 = 'mode1’,
mode2 = 'mode2’,
mode3 = 'mode3’
}
export default SecurityMode;
Run Code Online (Sandbox Code Playgroud)
import SecurityMode from 'shared/common/enums/SecurityMode';
export default interface IAuthUser {
security: Record<SecurityMode, boolean>;
}
Run Code Online (Sandbox Code Playgroud)
错误:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Record<SecurityMode, boolean>'.
No index signature with a parameter of type 'string' was found on type 'Record<SecurityMode, boolean>'.ts(7053)
Run Code Online (Sandbox Code Playgroud)
这是代码的一部分,出现错误:
{user && user.security && user.security[securityKey] && ( // The error is …Run Code Online (Sandbox Code Playgroud) 所以我想为我的对象type和data对象建立某种接口。所以我有
data Tile = Tile {
coord :: Coord,
fixed :: Bool,
wiel :: Bool
} deriving (Show)
type Source = (Coord , Bool)
type Sink = (Coord , Bool)
Run Code Online (Sandbox Code Playgroud)
并想为所有这些接口创建一个全局接口,所以我希望该接口具有另一个字段,rotating :: Bool然后带有the Tile或the Source或the Sink。
如果它们在哪里都实现了相同的接口,那么它们就具有额外的字段。这样,我也可以将它们放在我也需要的列表中。(也许是一个Nothing选择,以防万一我遍历列表时没有任何东西)。
我首先尝试这样做而没有像这样的多余字段
data newInterface = Source | Sink | Tile | Nothing
Run Code Online (Sandbox Code Playgroud)
但是,这无效,因为我得到了“ Tile多次定义”错误。
我将如何解决呢?谢谢
我想为默认函数实现一个相等和比较功能的接口。
如果我从成员中删除IKeyable<'A>除Key成员之外的所有内容,则它是有效接口,只要不添加默认实现即可。从中删除其他接口实现IKeyable<'A>,并仅保留默认成员将以相同的结果结束。
type IKeyable<'A when 'A: equality and 'A :> IComparable> =
abstract member Key : 'A
default this.Equals obj = // hidden for clarity
default this.GetHashCode () = // hidden for clarity
interface IEquatable<'A> with
member this.Equals otherKey = // hidden for clarity
interface IComparable<'A> with
member this.CompareTo otherKey = // hidden for clarity
interface IComparable with
member this.CompareTo obj = // hidden for clarity
type Operation =
{ Id: Guid }
interface …Run Code Online (Sandbox Code Playgroud) interface ×10
java ×5
oop ×2
.net ×1
c# ×1
casting ×1
coding-style ×1
comparable ×1
compare ×1
enums ×1
f# ×1
generics ×1
haskell ×1
inheritance ×1
javascript ×1
list ×1
reactjs ×1
reflection ×1
types ×1
typescript ×1
wpf ×1
wrapper ×1