Say I have one base class, and serveral sub class will override the method f() in the base class. I move mouse on the method f() in base class, is there any shortcut to show me the methods in its subclasses. Thanks.
Navigate > Implementations (Ctrl + Alt + B in default keymap for Windows/Linux, Cmd + Alt + B for Mac) will show you the methods that either override or implement (if the method is in a interface, not class) the currently selected method.
Related
For example: I have some classes that all inherit from the same class.
Public Class MasterClass
' content
End Class
Public Class ClassA
Inherits MasterClass
'content
End Class
Public Class ClassB
Inherits MasterClass
'content
End Class
Public Class ClassC
Inherits MasterClass
'content
End Class
And I want decide on runtime which one I need. Then I can do something like this:
Private myInstance As MasterClass
If conditionA Then
myInstance = New ClassA
ElseIf conditionB Then
myInstance = New ClassB
Else
myInstance = New ClassC
End
But it can get quite long, and I still have to cast it evertime I use it.
I can assing a type to a variable, but I don't know how to use that type to create a new instance of that type..
Dim storedType As Type = GetType(ClassA)
Dim someInstance = New storedType 'Does not work
Is there a better way? Can you change the type of a variable at runtime?
I still have to cast it evertime I use it.
The idea around polymorphism and inheritance is that you don't have to cast them to use them. You can write things in such a way that the master class has all the functions etc that you need (whether or not they do anything) and then you call things as if you were just dealing with the master class but, because each child implements a different behavior, the end result is different - your program might not even know (if the child implementation came from a third party dll) what is going on but it doesn't matter
Can you change the type of a variable at runtime?
Sure, but you have to use it how it appears. Long chains of "if my object is an instance of x then cast my object as x and use method X1, else if my object is a y then cast as y and call y1" are not polymorphic/not leveraging inheritance principles properly - you're supposed to call myobject.whatever, and if my object is an x, then x1 happens and if it's a y then y1 happens
I want decide on runtime which one I need
But you don't have to do that in the class that knows about class a/b/c - each of class a/b/c can do that and hence become self contained. You can have all your instances in an array of the parent type, and visit each one asking them if they handle the condition and use the one that says it can
Consider a slightly better real world example than this artificial class a/b/c trope:
You are tasked with writing an app that can download an image (png, jpg or gif) from somewhere (http or ftp or disk location), rotate it and upload it to somewhere else
You decide to have an ImageRotator parent that specifies a CanHandle function and a Rotate function. It has 3 subclasses, one that handles jpg, one that rotates gif and one that does png. When presented with a PNG filename the JpgRotator says No when asked if it can handle it etc.
Separately you have a FileMover parent that CanHandle and has Download/Upload functions. Again, the parent doesn't implement these at all. The three subclasses implement the ability up/down an http, an ftp and a disk location
You create an instance of each rotator and put it into an array of ImageRotator type. You also create an instance of each mover onto an array that is a FileMover parent type.
Your user specifies a jpg in a http url, and to store it in a disk location at the end. You loop your FileMovers and ask each if they support the location the user provided. The http mover says yes, you invoke its download to a temp path. Then you pass he path to each rotator, the jpg rotator says yes, you call rotate. Finally, you look for another mover that can handle an output path of local disk...
Someone decides to extend your program with a plug-in dll that adds he ability to put files in and out of a db, and support tiff images.. ignoring the magic of how instances of their classes come to be in your arrays, you can see that your program can now move these new locations and types because the logic for whether they handle db/tiff is not a part of your code.. your code just treats everything consistently
In this case Interfaces are the best choice
Interface ABC
Property Text As String
Property Value As Integer
End Interface
Public Class MasterClass
End Class
Public Class ClassA
Inherits MasterClass : Implements ABC
Property Mystr As String Implements ABC.Text
Property Sum As Integer Implements ABC.Value
End Class
Public Class ClassB
Inherits MasterClass : Implements ABC
Property MyText As String Implements ABC.Text
Property Value As Integer Implements ABC.Value
End Class
Public Class ClassC
Inherits MasterClass : Implements ABC
Property str As String Implements ABC.Text
Property Value As Integer Implements ABC.Value
End Class
usage
Dim myABC As ABC
If conditionA Then
myABC = New ClassA
ElseIf conditionB Then
myABC = New ClassB
Else
myABC = New ClassC
End If
myABC.Text = "Interface"
suppose i have a interface like this:
public interface Dumb{
String getDumbName();
}
Is there any shortcut or menu in intellij-idea to create new classes implementing the interface with dummy implement methods like this:
public class Dumber{
public String getDumbName(){
return null;
}
}
There are multiple ways to go about this.
On the interface name itself, you can hit Alt+Enter (Option+Enter on Mac), then pick 'Implement interface'. IDEA will prompt for a class name and a package to put the new class in, then generate an implementation class.
Alternatively, create the class, then add implements Dumb after the name (im<tab> Dumb). IDEA will complain that your class doesn't implement the correct methods, and offer (Alt+Enter Enter Enter) to generate them for you. Hitting Ctrl+I or clicking 'Implement methods' in the Code menu also works.
i need to implement the message markedSubclass in pharo that works just like subclass but i need the class that gets created to be somehow marked,for example i tried adding a unique instance variable to it after creating it but it's just not working,maybe i'm adding it to a wrong place.
the requirments are:
every subclass of this marked class should also be marked even if it
was created via subclass (not markedSubclass).
other than that a marked class should function just as a regular class should.
any help would be appreciated.
example:
User markedSubclass: #MarkedUser
User subClass: #UnmarkedUser
MarkedUser subclass: #MarkerUser2
i need to somehow know that MarkedUser and UnmarkedUser are both marked classes.
what i thought of lately is adding the method "isMarked" to Class class and this way all
the classes will have it, and each class will override it accordingly so if we write
User class isMarked.
it will return false but if we write:
MarkedUser class isMarked.
MarkedUser2 class isMarked.
it will return true for both.
but where can i add this method?and how can i make a class override the method in runtime?
Add a class method like the following to your User class:
markedSubclass: className
| subclass |
subclass := self subclass: className asSymbol.
subclass class compile: 'isMarked', String cr, String tab, ' ^ true'.
^ subclass
Then try in a workspace:
User markedSubclass: 'MyMarkedSubclass'
Add an #unmarkedSubclass: class method accordingly.
You could then override the general #subclass: method in your User class to set the same marker as the receiver.
I've started to learn VBA in Access. I have read that the language has no inheritance.
And then I read an example code which seems like it actually has inheritance:
Dim ctrl As Control
...
If TypeOf ctrl Is TextBox Then ...
If TypeOf ctrl Is ListBox Then ...
It seems to me as the TextBox, ListBox were inherited from the Control. Could somebody explain this?
No. They are not derived from the Control class. They implement Control's definition/ methods and properties signatures. The way TypeOf and Is operators work it's they check whether the instance of a class Implements one of 3 categories (listed below).
open a new workbook
Go to VBE and add
a class module and name it: MyClass
in the code view only add Implements MyInterface
a class module and name it: MyInterface
in the code view - do nothing/leave empty
a module and copy paste the below code and run it
Sub Main()
Dim cls As MyClass
Set cls = New MyClass
Debug.Print TypeOf cls Is MyClass
Debug.Print TypeOf cls Is MyInterface
End Sub
The result may be surprising
True
True
cls variable is of two types - MyClass and MyInterface
as you can see cls doesn't inherit nothing from MyInterface but definition. When using TypeOf and Is it actually shows true because MyClass implements MyInterface. Not because it's derived from the MyInterface class but because it implements it.
Now, suppose
result = TypeOf objectexpression Is typename
The TypeOf operator determines whether the run-time type of variable is compatible with typename. The compatibility depends on the type category of typename. There are three categories
Class objectexpression is of type typename or inherits from typename
Structure objectexpression is of type typename
Interface objectexpression implements typename or inherits from a class that implements typename
Specifically try to understand the 3rd category - Interface.
I think at this point you should really understand why TypeOf varName Is varType shows True for the TextBox and ListBox...
When you do VBA inheritance, you can only use Implements keyword to
implements a class definition. That is, the class to be implemented is
equivalent to C++/C#'s abstract class: only having property/method
definition.
Generally, your example isn't a form of a class polymorphism. Class polymorphism occurs when you are actually deriving an instance of one to class to another. This isn't the case. Even though TextBox and ListBox are both of a Control type they aren't actually derived from Control class. Note: they may as well be members of another collection - they would be TypeOf the higher in the object hierarchy type ( forms, also Component and IComponent becuase Forms implements that).
Im trying to test class from within of SAP class builder. I pressed F8 (Test) but Create instance menu item is not active on test screen:
My class has both static and instance constructors. Where is my mistake? How to force this menu item to be active?
Is this parameter set to public?
Moreover, if it is, then it is probably because your class implements an interface. Just click the magnifying glass icon next to the interface name and you will both be able to execute static and instance methods.