标签: default-implementation

接口方法可以有一个体?

我知道界面就像100%纯抽象类.所以,它不能有方法实现.但是,我看到了一个奇怪的代码.有人能解释一下吗?

代码片段:

 interface Whoa {
        public static void doStuff() {
            System.out.println("This is not default implementation");
        }
 }
Run Code Online (Sandbox Code Playgroud)

编辑:

我的IDE是Intellij Idea 13.1.项目SDK是java 7 <1.7.0_25>.IDE未显示任何编译器错误.但是,当我在命令行编译代码时,我收到以下消息.

Whoa.java:2: error: modifier static not allowed here
    public static void doStuff() {
                       ^
Run Code Online (Sandbox Code Playgroud)

java interface default-implementation java-8

67
推荐指数
3
解决办法
7万
查看次数

这个赋值运算符后的&符是什么意思?

我正在阅读关于"五条规则"的这个很好的答案,我注意到一些我以前没有记得的事情:

class C {
  ...
  C& operator=(const C&) & = default;
  C& operator=(C&&) & = default;
  ...
};
Run Code Online (Sandbox Code Playgroud)

&放置在= default复制赋值运算符和移动赋值运算符前面的字符的用途是什么?有没有人有这方面的参考?

c++ assignment-operator default-implementation c++11

48
推荐指数
2
解决办法
3271
查看次数

JSON.NET中使用的默认JsonConverter是什么?

我正在尝试编写一个自定义JsonConverter,用于一个人子类化列表或集合,但随后为子类添加额外属性的情况(参见此处).JSON.NET的当前实现只是将列表更改为子对象数组,并忽略所有添加的属性.所以我想编写一个新的JsonConverter,它将对象看作一个List并且只是像往常一样序列化其他所有东西,然后在序列化中添加一个名为'_Items'的新属性,其中实际的数据数组是存储.

现在我已经为我们的特定List子类编写了一个类,但我不得不逐个手动指定所有属性.但是,如果我可以编写一个转换器,将其视为普通对象,那么手动处理这些项目,我就是金色的.我甚至不在乎我是否最终复制另一个班级的一半(甚至更多!)但我很乐意为这些案例制作一个可重复使用的转换器.但是,正如我所说,我找不到默认转换器来启动.

那么......有谁知道那是哪里?

base-class json.net default-implementation

24
推荐指数
1
解决办法
6849
查看次数

如何为Objective-C协议提供默认实现?

我想指定一个带有可选例程的Objective-C协议.当例程没有由符合协议的类实现时,我想在其位置使用默认实现.协议本身是否存在我可以定义此默认实现的位置?如果没有,减少复制和粘贴此默认实现的最佳做法是什么?

protocols objective-c standards-compliance default-implementation overhead-minimization

22
推荐指数
2
解决办法
1万
查看次数

在源文件中放置模板成员函数(without default body)的特化定义是否安全?

这就是我的意思:

// test.h
class cls
{
public:
    template< typename T >
    void f( T t );
};
Run Code Online (Sandbox Code Playgroud)

-

// test.cpp
template<>
void cls::f( const char* )
{
}
Run Code Online (Sandbox Code Playgroud)

-

// main.cpp
int main()
{
    cls c;

    double x = .0;
    c.f( x ); // gives EXPECTED undefined reference (linker error)

    const char* asd = "ads";
    c.f( asd ); // works as expected, NO errors

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这完全没问题吧?

我开始怀疑这一点,因为我只是碰到了specialization of '...' after instantiation错误,这对我来说是新的.所以,我"解决"这个错误,现在一切似乎工作正常,但仍然..

这是明确定义的行为吗?


编辑:对于非成员模板函数(前向声明的非成员模板函数)也是如此.

c++ templates member-functions template-specialization default-implementation

8
推荐指数
2
解决办法
326
查看次数

C# 8.0 - 不能使用默认接口实现

我最近读到有关 C# 8.0 具有接口默认实现的信息,所以我进入了我的项目并尝试了它,但我遇到了一个错误。Target runtime doesn't support default interface implementation. 有没有办法解决这个问题?

string CommandName { get => CommandName.ToUpperInvariant(); }
Run Code Online (Sandbox Code Playgroud)

编辑 我正在使用Console

c# interface default-implementation c#-8.0

8
推荐指数
1
解决办法
4877
查看次数

开放封闭原则与默认实现

Java 8 引入了接口默认实现的概念?这是否违反了开放封闭原则,因为基于https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html上的示例,您似乎始终可以打开界面以添加新功能?

architecture open-closed-principle default-implementation java-8 default-method

5
推荐指数
1
解决办法
321
查看次数

如何为UIPageViewControllerDataSource提供默认实现?

我认为这个问题的答案一般会解决Objective-C协议的问题,但这是我遇到的第一个这类问题.

我期望在实现UIPageViewControllerDataSourceWithConnections时使用这些方法.

import UIKit

protocol UIPageViewControllerDataSourceWithConnections: UIPageViewControllerDataSource {
    var connectedViewControllers: [UIViewController] {get}
}

extension UIPageViewControllerDataSourceWithConnections {
    func pageViewController(pageViewController: UIPageViewController,
        viewControllerBeforeViewController viewController: UIViewController
    ) -> UIViewController? {return connectedViewController(
        current: viewController,
        adjustIndex: -
    )}

    func pageViewController(pageViewController: UIPageViewController,
        viewControllerAfterViewController viewController: UIViewController
    ) -> UIViewController? {return connectedViewController(
        current: viewController,
        adjustIndex: +
    )}

    private func connectedViewController(
        current viewController: UIViewController,
        adjustIndex: (Int, Int) -> Int
    ) -> UIViewController? {
        let requestedIndex = adjustIndex(connectedViewControllers.indexOf(viewController)!, 1)
        return connectedViewControllers.indices.contains(requestedIndex) ?
            connectedViewControllers[requestedIndex] : nil
    }

    func presentationCountForPageViewController(pageViewController: UIPageViewController) …
Run Code Online (Sandbox Code Playgroud)

protocols default-implementation uipageviewcontroller swift

3
推荐指数
1
解决办法
616
查看次数

Rust 在专用版本中调用函数的默认实现

我在 Rust 中有一个特性,它为其函数提供了一些默认实现。

trait MyTrait {
    fn do_something(&self);
    fn say_hello(&self) {
        println!("Hello I am default");
    }
}
Run Code Online (Sandbox Code Playgroud)

一些实现者扩展了这个特性并使用提供的默认值

struct MyNormalImplementor {}

impl MyTrait for MyNormalImplementor {
    fn do_something(&self) {
        // self.doing_some_normal_stuff();
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我想要一个扩展特性行为的实现器,但有时仍然使用默认实现。当然默认实现更复杂,我想遵循DRY原则。

struct MySpecializedImplementor(bool)

impl MyTrait for MySpecializedImplementor {
    fn do_something(&self) {
        // self.doing_some_wild_stuff();
    }
    fn say_hello(&self) {
        if self.0 {
            println!("hey, I am special");
        } else {
           MyTrait::say_hello(self);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这里MyTrait::say_hello(self);立即在无限循环中调用专用函数。我没有找到任何方法来限定函数调用,以便调用 in 中的默认实现MyTrait。有什么方法可以实现这一点,还是我必须为这种情况创建一个代理函数(也将在我的特征的公共接口中)?

polymorphism traits rust default-implementation

3
推荐指数
1
解决办法
196
查看次数