In this question there is one line,
findViewById(R.id.go_to_play_store).setOnClickListener(this::goToPlayStore);
how does this line is exactly handling the click listener ?
Java8 introduces concept of Method references and Functional interfaces. If function onClickListener requires a function with one argument(a.k.a Functional Interface) then if return types and argument types match your function(which is goToPlayStore) then you can pass its reference as functional interface.
This is the new JAVA 8 language feature Lambda Expressions.
:: refer to a new syntax in Java 8 known as method references. You can reference a class or instance and pass along the method that will handle the event
On click its calling function called goToPlayStore () located in that activity or fragment.
its Method references in Java 8
Which allows us to refer to an existing method by name. Method references can be used in place of lambda expressions as long as they satisfy the requirements of the functional interface.
For static methods the syntax is
Classname::methodName
Related
Here you can see that I can't pass that string parameter
I somehow learned how to pass a method in the same class but, when I want to call it from another class, I can't pass the arguments.
Several issues here:
the introduceMyself method accepts a higher-order function, but in its usage it seems that you don't want the method itself, but its result. I'd suggest replacing the getName parameter with name parameter and convert it to a regular string which will be provided externally by an invocation of said method.
In line 6, you have a syntax error. Since you're using an instance - person, you can replace the :: with a . like so: person.myNameIs("michael"). It's not working because :: passes the method itself, which you don't really want. Again, your code implies that you need to pass the result of the method and not the method itself.
Finally, you can pass higher order functions, but you can't pass them "with an argument". You can do one of 3 things:
Invoke the HOF locally and pass the result of the invocation
Pass the HOF and the argument and invoke them wherever it is you need to.
Pass the HOF and let the target pass whatever argument it chooses.
I'm using Kotlin for development. When I reached the official Android tutorial's fragment section, I came across the supportFragmentManager. Which is available to use as a variable in Kotlin, whereas in java we can call its equivalent method getSupportFragmentManager().
I wonder where is the supportFragmentManager variable defined as I could not see anything like a variable declaration with that name, however clicking that variable took me to the following method inside fragmentActivity.java class.
/**
* Return the FragmentManager for interacting with fragments associated
* with this activity.
*/
public FragmentManager getSupportFragmentManager() {
return mFragments.getSupportFragmentManager();
}
How does this method come to be accessible as a variable in Kotlin whereas in java we've to access like a regular method? Any help would be appreciated.
Actually, in Kotlin when you call supportFragmentManager it's not variable, Any Java method contains get prefix (without argument) in the method in Kotlin it will be called like a variable without get word
Methods that follow the Java conventions for getters and setters (no-argument methods with names starting with get and single-argument methods with names starting with set) are represented as properties in Kotlin. Boolean accessor methods (where the name of the getter starts with is and the name of the setter starts with set) are represented as properties which have the same name as the getter method.
Read more about here
This is something like the getter and setter method in Kotlin. You don't need to describe getProperty() and setProperty() method to access/update value of property.
You can know more about how it actually works with an example from this question/answers.
I hope it will help you.
Happy coding..!
I have learned that Open Close Principle is allowing extension to classes and restricting from modification. So in Kotlin, when we use extension function
Are we extending a class
Or are we modifying a class
Can extension functions in kotlin be an example for Open/Close Principle?
I assume extension means to apply inheritance and modification means to add or change code of existing class.
Thanks
The extenstion function is designed for situations where you want to add a function to a built-in or third-party class. You cannot do this by default because built-in functions are not modifiable.
An example implementation to add a toUnsigned method to the built-in Byte class:
fun Byte.toUnsigned(): Int {
return if (this < 0) this + 256 else this.toInt()
}
As Byte is a built-in class you cannot modify it directly. However, you can define an extension function as per above code. You can then call the extension function in the following way:
val x: Byte = -1
println(x.toUnsigned()) // Prints 255
Keep in mind that this is just syntactic sugar - you're not actually modifying the class or its instances. Therefore, you have to import an extension function/property wherever you want to use it (since it isn't carried along with the instances of the class).
Source : https://kotlinlang.org/docs/tutorials/kotlin-for-py/extension-functionsproperties.html
'Extension' in the context of the Open Closed Principle usually does not mean inheritance, it means somehow extending the class with new functionality. 'Modification' does refer to changing the code of the class, as you say.
The extension facility of Kotlin allows you to add a method to a class without editing the code of the class. This is perfectly in keeping with the Open Closed Principle -- the class is extended with new functionality without the class itself being changed.
I want to test some method, for example:
public class testObj {
..
public void upload(Context context, Data data, Info info, Listener listener, Bla bla, ....) {
...
}
}
now in some cases i just want to know that this method was called, but i do not care about anyy of the arguments passed.
Now calling Mockito.any(Foo.class) is very discouraging, I know i can also use matchers but it's not that great also.
Is there some cleaner way to achive this?
No; verify needs to identify the method you're referring to, which means you'll need to call the correct method signature. Keeping an actual method call will also allow IDEs and automated refactoring tools to search for and modify the calls appropriately.
If you're running your tests from a Java 8 source environment, you can use any() with no argument; Java 8 has improved the ability to infer generic types when given as a parameter.
Though it usually makes more sense just to use matchers and explicit calls, you do have a few similar capabilities:
For stubbing, you can sometimes use a default answer to avoid specifying a lot of redundant calls and method values, but that won't help you with verification.
For verification, you can use MockingDetails.getInvocations() to inspect calls without using the built-in Mockito capabilities.
PowerMockito has private method verification by name, but not the same for public methods.
is there Any Way To Make my Method Take many input variable but with out overloading ... could be my question not clear ... I mean Like That :
if I Have This Method
public void setValues (int val1,int val2 ,String val3){
}
what I want is : use this method with many way
setValues (val1,val2)
OR
setValues (val3)
why I want to do that with out overloading : Because if i have as example 10 variable i want to add many method with overloading but i don't like that ...
is there any way helps me to check variable or skip it in the same method ..
Thanks for help .
You can use varargs future to partially solve your problem http://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html
This can be done if you have parameters of a same type.
But it will require that you will pass variables with the same type as a last param. It is not completely what you want, but it is a small workaround.
No there's no way to do that in Java without method overloading. One alternative would be to group those parameters which are related and make them fields of a class. Then the method would take an instance of that class as a parameter, and which ever parameters were optional would then be null.