简体   繁体   English

如何创建链接多个访问器的函数?

[英]How do I create a function that chains multiple accessors?

I want to create a function where I can chain accessors, but don't know how.我想创建一个可以链接访问器的函数,但不知道如何。

For example, I have a class A which has a subclass save , and attached to that save class I have two methods B() and C() :例如,我有一个A类,它有一个子类save ,并且附加到该save类我有两个方法B()C()

class A {
    class save {
       function B() {};
       function C() {};
 }
}

I would like to be able to call them like so: A.save.B();我希望能够像这样调用它们: A.save.B(); , or A.save.C(); , 或A.save.C();

I think what you're looking for is the keyword static .我认为您正在寻找的是关键字static This (roughly) allows things to exist without an instance (or without an instance of the enclosing class in your case).这(大致)允许事物在没有实例的情况下存在(或者在您的情况下没有封闭类的实例)。

Something like:就像是:

class A {
    static class save {
        static void A() {
            // ...
        }

        static void B() {
            // ...
        }
    }
}

would allow you to call the defined methods as:将允许您将定义的方法调用为:

A.save.A(); /* or */ A.save.B();

The point being that the save class being static means that you can access it without having an instance of the enclosing A class .关键是save classstatic意味着您可以在没有封闭A class的实例的情况下访问它。 And the methods within the save class being static mean they can be invoked without an instance of the save class .的方法save class存在static意味着他们可以在没有实例调用save class

(As a side note, classes are almost universally given names starting with a capital, and methods names starting not with a capital. Your naming convention will confuse anyone coming to use your code.) (作为旁注,类的名称几乎普遍以大写开头,方法名以大写开头。你的命名约定会让任何使用你的代码的人感到困惑。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM