我知道界面就像100%纯抽象类.所以,它不能有方法实现.但是,我看到了一个奇怪的代码.有人能解释一下吗?
代码片段:
 interface Whoa {
        public static void doStuff() {
            System.out.println("This is not default implementation");
        }
 }
编辑:
我的IDE是Intellij Idea 13.1.项目SDK是java 7 <1.7.0_25>.IDE未显示任何编译器错误.但是,当我在命令行编译代码时,我收到以下消息.
Run Code Online (Sandbox Code Playgroud)Whoa.java:2: error: modifier static not allowed here public static void doStuff() { ^
我正在阅读关于"五条规则"的这个很好的答案,我注意到一些我以前没有记得的事情:
class C {
  ...
  C& operator=(const C&) & = default;
  C& operator=(C&&) & = default;
  ...
};
&放置在= default复制赋值运算符和移动赋值运算符前面的字符的用途是什么?有没有人有这方面的参考?
我正在尝试编写一个自定义JsonConverter,用于一个人子类化列表或集合,但随后为子类添加额外属性的情况(参见此处).JSON.NET的当前实现只是将列表更改为子对象数组,并忽略所有添加的属性.所以我想编写一个新的JsonConverter,它将对象看作是一个List并且只是像往常一样序列化其他所有东西,然后在序列化中添加一个名为'_Items'的新属性,其中实际的数据数组是存储.
现在我已经为我们的特定List子类编写了一个类,但我不得不逐个手动指定所有属性.但是,如果我可以编写一个转换器,将其视为普通对象,那么手动处理这些项目,我就是金色的.我甚至不在乎我是否最终复制另一个班级的一半(甚至更多!)但我很乐意为这些案例制作一个可重复使用的转换器.但是,正如我所说,我找不到默认转换器来启动.
那么......有谁知道那是哪里?
我想指定一个带有可选例程的Objective-C协议.当例程没有由符合协议的类实现时,我想在其位置使用默认实现.协议本身是否存在我可以定义此默认实现的位置?如果没有,减少复制和粘贴此默认实现的最佳做法是什么?
protocols objective-c standards-compliance default-implementation overhead-minimization
这就是我的意思:
// test.h
class cls
{
public:
    template< typename T >
    void f( T t );
};
-
// test.cpp
template<>
void cls::f( const char* )
{
}
-
// 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;
}
这完全没问题吧?
我开始怀疑这一点,因为我只是碰到了specialization of '...' after instantiation错误,这对我来说是新的.所以,我"解决"这个错误,现在一切似乎工作正常,但仍然..
这是明确定义的行为吗?
编辑:对于非成员模板函数(前向声明的非成员模板函数)也是如此.
c++ templates member-functions template-specialization default-implementation
我最近读到有关 C# 8.0 具有接口默认实现的信息,所以我进入了我的项目并尝试了它,但我遇到了一个错误。Target runtime doesn't support default interface implementation. 有没有办法解决这个问题?
string CommandName { get => CommandName.ToUpperInvariant(); }
编辑
我正在使用Console
Java 8 引入了接口默认实现的概念?这是否违反了开放封闭原则,因为基于https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html上的示例,您似乎始终可以打开界面以添加新功能?
architecture open-closed-principle default-implementation java-8 default-method
我认为这个问题的答案一般会解决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) …我在 Rust 中有一个特性,它为其函数提供了一些默认实现。
trait MyTrait {
    fn do_something(&self);
    fn say_hello(&self) {
        println!("Hello I am default");
    }
}
一些实现者扩展了这个特性并使用提供的默认值
struct MyNormalImplementor {}
impl MyTrait for MyNormalImplementor {
    fn do_something(&self) {
        // self.doing_some_normal_stuff();
    }
}
现在我想要一个扩展特性行为的实现器,但有时仍然使用默认实现。当然默认实现更复杂,我想遵循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);
        }
    }
}
这里MyTrait::say_hello(self);立即在无限循环中调用专用函数。我没有找到任何方法来限定函数调用,以便调用 in 中的默认实现MyTrait。有什么方法可以实现这一点,还是我必须为这种情况创建一个代理函数(也将在我的特征的公共接口中)?
c++ ×2
interface ×2
java-8 ×2
protocols ×2
architecture ×1
base-class ×1
c# ×1
c#-8.0 ×1
c++11 ×1
java ×1
json.net ×1
objective-c ×1
polymorphism ×1
rust ×1
swift ×1
templates ×1
traits ×1