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()
:
class A {
class save {
function B() {};
function C() {};
}
}
I would like to be able to call them like so: A.save.B();
, or A.save.C();
I think what you're looking for is the keyword 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
. And the methods within the save
class
being static
mean they can be invoked without an instance of the 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.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.