I got the following problem:
I made a custom list CustomList which extends ArrayList and added a new method to it:
public class CustomList extends ArrayList<CustomObj> {
public CustomObj get(String searchName) {
...
}
}
now in my MainActivity.java I make a new object of this CustomList, but I'm using:
List<CustomObj> list = new CustomList(); (1)
and NOT:
CustomList list = new CustomList(); (2)
so far so good!
but when I try to access the function get(String searchName), there is no function I could use!
why? cause when I call it when creating the CustomList via (2) it'll totally work
its is because you are making List(Interface) has no any such method defined , you are making object of CustomList but reference type is List .so it will give you compile time errer if you force to call this method on List reference.This is an example of simple Polymorphism.
You can imagine a case where Mordern car extends Old age car and mordern car has GPS navigation system where as old doesn't in this case if you try to get the detail of navigation system by old car reference which doesn't know about GPS , you wont get anything.
Related
i really dont understand the following behavior:
3 Code snippets to explain
sqlconnection.h
class SqlConnection
{
public:
SqlConnection();
QSqlDatabase db;
} ;
sqlconnection.cpp
SqlConnection::SqlConnection()
{ if (!db.open()){ //no different with or without that
db = QSqlDatabase::addDatabase("QODBC");
...
}
artikelverwaltung.cpp
Artikelverwaltung::Artikelverwaltung(){
SqlConnection().db.open();
...(code to do works fine)
}
void Artikelverwaltung::on_pushButton_2_clicked()
{
SqlConnection().db.close(); // "HERE"
}
"HERE" it cycles the full Contructor of the Class again, but why it don't just close the connection? (this is the Main question about and how to fix that.)
I can't even access "db" from another Method inside the Class but outside of the Constructor, like:
sqlconnection.cpp:
void closeDB() {db.close();} is not possible.`
What i am doing wrong?
Thank you guys
In general, you do not need to use any wrapper class to hold database connections in Qt. You should create a database connection once using QSqlDatabase::addDatabase and open it. After that you can get a database connection anywhere by calling QSqlDatabase::database static method. So to close the database connection you should simply call QSqlDatabase::database().close().
Hello and welcome to Stackoverflow!
If you call SqlConnection().db.close();, what you are actually doing is creating a new instance of your SqlConnection class and then calling close() on its variable db. This is not the same one you opened earlier.
What you want to do is inside of your Artikelverwaltung class:
SqlConnection sqlConnection = new SqlConnection();
and then later
sqlConnection.db.open();
and afterwards
sqlConnection.db.close();
The reason why you cant add that closeDB() method is that you need to either put the method inside of the SqlConnection class or you need to operate on an instance of the class.
You cannot operate on a class like this without instanciating it (creating an instance of it).
You should look at class instantiation to understand how the concept works.
If you call SqlConnection() you are creating a new instance of the class (you can look at it like a copy of the class).
If you call SqlConnection() somewhere else you are simply creating a new instance.
These instances do NOT share the variables or the contents in them. They are completely independent.
You cannot access a member of a class (like your db variable) without creating an instance of the class first and then accessing the member of that instance.
If a page or component class has one instance field which is a non-synchronized object, f.ex. an ArrayList, and the application has code that structurally modifies this field, should the access to this field be synchronized ?
F.ex.:
public class MyPageOrComponent
{
#Persist
private List<String> myList;
void setupRender()
{
if (this.myList == null)
{
this.myList = new ArrayList<>();
}
}
void afterRender(MarkupWriter writer)
{
// Should this be synchronized ?
if (someCondition)
{
this.myList.add(something);
}
else
{
this.myList.remove(something);
}
}
}
I'm asking because I seem to understand that Tapestry creates only one instance of a page or component class and it uses this instance for all the connected clients (but please correct me if this is not true).
In short the answer is no, you don't have to because Tapestry does this for you. Tapestry will transform your pages and classes for you at runtime in such a way that wherever you interact with your fields, they will not actually be working on the instance variable but on a managed variable that is thread safe. The full inner workings are beyond me, but a brief reference to the transformation can be found here.
One warning, don't instantiate your page/component variables at decleration. I have seen some strange behaviour around this. So don't do this:
private List<String> myList = new ArrayList<String>;
Tapestry uses some runtime byte code magic to transform your pages and components. Pages and components are singletons but the properties are transformed so that they are backed by a PerThreadValue. This means that each request gets it's own copy of the value so no synchronization is required.
As suggested by #joostschouten you should never initialize a mutable property in the field declaration. The strange behaviour he discusses is caused beacause this will be shared by all requests (since the initializer is only fired once for the page/component singleton). Mutable fields should instead be initialized in a render method (eg #SetupRender)
Im studying C# and came across a piece of code that Im quite confused about
private static List<Customer> CreateCustomerList()
List<Customer> customers = new List<Customer>{new Customer{FirstName="Orlando"}
};
return customers;
}
Im confused with the line where it starts
private static List<Customer> CreateCustomerList()....
Im used to seeing for example
private static class CreateCustomerList()....
Why would I need to use List instead of declaring the method as a class?
The line where you are confuses is the method signature and the return type of the method is of List, means that method will return the list of customers entity.
Thaanks
So I'm going along with a lynda.com video on creating a custom list layout. I have gone along with the video but I am not able to change my constructor in order to change the code so it is an array of strings. In both places where I have written MyAdapter, there is a red squiggle below and the top one tells me MyAdapter cannot be resolved to a type. Please assist.
setListAdapter(new MyAdapter<String>(CustomList2Activity.this,
android.R.layout.simple_list_item_1, R.id.textView1, getResources().getStringArray(R.array.companies2)));
private class MyAdapter extends ArrayAdapter<String>{
public MyAdapter(Context context, int resource,
int textViewResourceId, List<String> objects) {
super(context, resource, textViewResourceId, objects);
// TODO Auto-generated constructor stub
}
Make sure you are declaring the MyAdapter class in the right place. Make sure you're not declaring it within a function. Also, go to the source menu (assuming you're using eclipse) and press "organize imports" to make sure your imports are right.
I have three files:
ScriptProcessor.java:
public interface ScriptProcessor {
public String someMethod();
}
ScriptProcessorDummy.java:
public class ScriptProcessorDummy implements ScriptProcessor {
public String someMethod{
return "some string";
}
}
In the main method, the code does the following:
URLClassLoader loader = null;
loader = new URLClassLoader(new URL[] {new URL("JARFILE")});
if (loader != null) {
ScriptProcessor processor = (ScriptProcessor) loader.loadClass("ScriptProcessorDummy").newInstance();
}
"JARFILE" contains class files of ScriptProcessor and ScriptProcessorDummy.
The code works fine when using JDK 1.4 but when using JDK 1.5, the typecast (to ScriptProcessor) fails with java.lang.ClassCastException.
Could somebody please tell me how to fix this.
Thanks,
Raj
You need to make your current class loader the parent of the new class loader you are loading, and make sure that there isn't a copy of the interface in your jar.
The problem you have suggests that you have two copies of the interface: one in the main class loader, one in your new one. The object returned uses the one in the separate jar, but your class is using the main one. They aren't the same. You have to make sure that Java uses the same '.class' for the interface when processing the loaded class as it did when it compiled your code.
The first thing to do is 'jar tf' on the jar and see if my hypothesis of two copies is correct. If so, remove it. Try running. If you now get a NoClassDef, fix the construction of the loader.
new URLClassLoader(urlArray, Thread.currentThread().getContextClassLoader());
assuming that your environment maintains the context class loader. Alternatively,
new URLClassLoader(urlArray, ScriptProcessor.class.getClassLoader());
Try using:
loader.loadClass("ScriptProcessorDummy", true).newInstance();
the boolean tell the classloader to resolve the class name.