I'm having two activities, activity A and activity B.
I've defined a method in a class inside activity A.
Activity B has a class and its subclass.
Now I need to call the method from subclass.
As I cannot create the same method again within the subclass.
Declare your method as protected static. Then, from B, call it so: A.myMethod();
Related
How does a main method gets called in scala ? Why does a main method gets called in when it is written only in object but not in class ?
Because the specification says so:
A program is a top-level object that has a member method main of type (Array[String])Unit. Programs can be executed from a command shell. The program's command arguments are passed to the main method as a parameter of type Array[String].
The main method of a program can be directly defined in the object, or it can be inherited.
It speaks only about top-level objects, not classes. If you define a main method in a class, then it will be just an ordinary method that you can invoke on the instances of this class. Unless you define a top-level object that inherits the main from this class / trait, this method called main will not be treated as an entry point of the application.
The main method must be a static method. In Scala to create a static method you put it in an object. Methods in a class are not static.
In the scala language they decided to separate class, which hold only instance behavior and state, and object which hold static behavior and state. This is different from java where classes hold both instance and static members where something is made static using the static keyword.
It is because in scala the only way to define a method static is object class. And also it is necessary only one instance of main class is created , not multiple instances. That's why it is object class
How to select a specific class from set of classes which are implementing same interface class ?
You don't call a method directly from an interface, you call it on a reference that points to an instance of a class. Whichever class that is determines which method gets called.
My original question id here.
Actually I need more clarification about this problem.
Consider the following scenario:
There are three classes in my project. Say A, B & C.
These 3 classes have some common methods with them. So I created an another class (Say D) in which, I added those common methods.
Now Class D is the parent class and Class A,B & C are child classes. That is, Class A,B & C inherits the properties of D.
I'm having three UIButton's for each child class. Each button in each sub class has some action. But the button's properties like color, frame, image are same in all three classes.
So, I need to do the following things for the buttons in each sub class's:
init the buttons
alloc them
set frame
set color
set action (using [button addTarget:self action:#selector(touchUpInsideAction:) forControlEvents:UIControlEventTouchUpInside])
etc
Except the "set action", everything is same in all child classes (Class A,B & C)
Now my question is, Can I create the button, its properties and action in Class D (Parent class) and Can I write the action method's (touchUpInsideAction:) implementation in each child class (Class A,B & C)?
So that, I can reduce my repeated code in my project. Here I'll call the child class's method touchUpInsideAction: from the parent class.
Is this right way or should I write button creation and properties for each Class separately?
Thanks in Advance
Create a class method in Class D. Set all button properties in that method.
Call class method from child class say A. Class method will return object of type UIButton.
Assign the selector and then add that object to the view.
As My Understanding D class should have button with setting all common property and action methods
. And Child class should be responsible to handle the Action if you want different functionality for every class.
Assume you have added target self and selector to button in supper class. you have created object of A, B or C. target for button in supper class(D) is self. Self is a representation of object So self will refer to objet of Class A, or B , or c.
I have two classes, both are subclasses of CCLayer,
I want to call a method of first class into second class, what should I code?
Your question is not providing much detail, but from my understanding of what you say, you need the following:
a selector in the public interface of your first class;
a pointer ivar in the second class that you will properly initialize so that it points to an instance of the first class;
In this way you will be able to call the first class' method from the second class.
I have a SenTest class I am attempting to write which uses a Category to override a method in a controller class. I want my mock controller class to call a method in the SenTest class to determine how to behave for various tests.
For example:
Set a var in the testFoo method.
In testFoo, call my mock controller's overridden method.
Have it call back up to the SenTest singleton to figure out how to behave for testFoo.
Is this possible, or am I an idiot?
Turns out the trick to doing this is to to make the associated reference IN the category code, not in the SenTest case.
So for example, declare this in category:
-(void)fakeMethodToSetTestData:(NSDictionary *)data;
And in that method set the associative reference to store your data "associated" with "self".
Call this in your test case on your object, passing in your data.
Then in your test case, call your:
-(void)realMethodInCategoryWhichIsBeingOverridden;
And in your category implementation of this method, retrieve the data from the associative reference you set in the first method.
This will let you set test data on a Controller so that you can hijack the delegation chain, for example, to prevent the second method from going to the network and returning your test data into your controller's code.