Extract Superclass
The Extract Superclass refactoring enables extracting certain members of a class into a superclass. In CLion, this refactoring is available for C++, Objective-C/C++, Python and JavaScript code.
Examples
Before | After |
---|---|
class SomeClass {
char SomeChar = 'a';
long d22;
//This function will be extracted to a base class
void exFunc(int);
}; |
class SuperClass {
void exFunc(int);
};
class SomeClass : public SuperClass {
char SomeChar = 'a';
long d22;
}; |
Before | After |
---|---|
@interface SСlass : NSObject {
int v; //This variable will be extracted to a superclass
}
- (void)initV;
@end
@implementation SСlass
- (void)initV {
v = 20;
}
@end |
@interface SuperClass : NSObject { int v; }
@end
@interface SСlass : SuperClass
- (void)initV;
@end
@implementation SСlass
- (void)initV {
v = 20;
}
@end |
Before | After |
---|---|
class BaseClass:
# This function will be extracted to a base class
def eval_smth(self, a, b, c):
x = 2*b - c
y = 2*b + c
return x, y |
class SuperClass:
def eval_smth(self, a, b, c):
x = 2*b - c
y = 2*b + c
return x, y
class BaseClass(SuperClass):
pass |
Before | After |
---|---|
class Editor {
//This function will be extracted to a superclass
View() {
console.log(this.name + ' can view');
}
Edit() {
console.log(this.name + ' can edit');
}
} |
class User {
View() {
console.log(this.name + ' can view');
}
}
class Editor extends User {
Edit() {
console.log(this.name + ' can edit');
}
} |
Extracting a superclass
To extract a superclass
Select the desired class in one of the views, or just open it in the editor.
On the on the main menu or on the context menu, choose
.- In the Extract Superclass dialog box that appears, specify the following information:
Name of the new superclass.
Members to be included in the superclass
Proceed with the refactoring.
Extracting the class dependent members
Let's consider the following sample of code:int d1;
class XClass {
static const int d2 = 13;
int exRes = d2 * d1;
};
d1
and exRes
. If you apply Extract Superclass refactoring to the above class and try to select exRes
to be moved, CLion highlights the d2
variable with blue: Trying to proceed with extract, you will get the following warning message:
Choose Continue to ignore the problem and proceed with refactoring, or Cancel to return back and resolve it. Also you can observe the conflict in Find Tool Window.