Or does it not work that way? do the subclasses get priority?
The Big Programming Thread - Page 689
Forum Index > General Forum |
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. | ||
Deleted User 3420
24492 Posts
Or does it not work that way? do the subclasses get priority? | ||
ShAsTa
Belgium2841 Posts
| ||
Deleted User 3420
24492 Posts
so is this how I am supposed to do it if I want to print the fields that are both in the subclass and in the superclass: public String getInfo(){ return this.getArtist()+this.getSongs()+super.getTitle()+super.getCopies(); } | ||
Acrofales
Spain17842 Posts
On December 07 2015 02:10 travis wrote: But in that case, any time I do Animal.talk(); won't it just say "Animal" regardless of if it is a cat or a dog? Or does it not work that way? do the subclasses get priority? That's how polymorphism works. Just use the example above, and try the following:
And watch the magic happen. And connecting this back to our discussion on setters in the constructor, this is the only reason I can really think of why calling setters from the constructor can be a problem. If you override the setters in your subclasses, then you run a (very real and hard to debug) risk of breaking the superclass' constructor. So in general, my answer earlier went a bit overboard on the use of setters (and getters): the constructor is probably the only place where you shouldn't do that... although there is a caveat in that if the way you are overriding getters and setters in the subclass breaks superclass functionality, you are probably messing stuff up in any case, whether that is in the constructor or otherwise and you should rethink your design. | ||
Blisse
Canada3710 Posts
--- You should be doing that in the subclass where you have access to the subclass and superclass methods. | ||
Deleted User 3420
24492 Posts
but this is just like the coolest thing in the world lol I am really happy with my decision to go into computer science lol this is so much more interesting than other career paths I could have gone down | ||
spinesheath
Germany8679 Posts
On December 07 2015 02:17 Acrofales wrote: And connecting this back to our discussion on setters in the constructor, this is the only reason I can really think of why calling setters from the constructor can be a problem. If you override the setters in your subclasses, then you run a (very real and hard to debug) risk of breaking the superclass' constructor. So in general, my answer earlier went a bit overboard on the use of setters (and getters): the constructor is probably the only place where you shouldn't do that... although there is a caveat in that if the way you are overriding getters and setters in the subclass breaks superclass functionality, you are probably messing stuff up in any case, whether that is in the constructor or otherwise and you should rethink your design. Generally, calling virtual methods from inside a constructor is a potential risk and not recommended. Some languages can deal with it, some can't, at least not always (an overriden method might access a field of the subclass while the subclass hasn't been constructed/initialized yet). And if they can deal with it, it usually is still prone to errors or not working as you'd expect. And if I remember correctly, methods in Java are virtual by default, so you have to be careful with just about any method call in a constructor. | ||
Acrofales
Spain17842 Posts
On December 07 2015 02:16 travis wrote: hmm okay. so is this how I am supposed to do it if I want to print the fields that are both in the subclass and in the superclass: public String getInfo(){ return this.getArtist()+this.getSongs()+super.getTitle()+super.getCopies(); } Try to avoid calling super where necessary. Imagine the following:
Now, lets say you instantiate a Movie called "Terminator". If you explicitly call super.getTitle(), you will get "Generic media: Terminator" instead of "Movie: Terminato" from just calling getTitle(). A subclass inherits all public and protected methods and fields from its parent, so unless you explicitly want to refer to something that is being done differently in the parent and the child, and you want the parent's way of doing it, don't use super. | ||
spinesheath
Germany8679 Posts
On December 07 2015 02:44 Acrofales wrote: Try to avoid calling super where necessary. Imagine the following:
Now, lets say you instantiate a Movie called "Terminator". If you explicitly call super.getTitle(), you will get "Generic media: Terminator" instead of "Movie: Terminato" from just calling getTitle(). A subclass inherits all public and protected methods and fields from its parent, so unless you explicitly want to refer to something that is being done differently in the parent and the child, and you want the parent's way of doing it, don't use super. Protected fields aren't such a great idea either. That's just another way of putting the superclass into an invalid state. Either have the superclass handle all title formatting itself (maybe using the template method pattern if you need to) or handle the title logic in each subclass individually. | ||
Acrofales
Spain17842 Posts
On December 07 2015 02:41 spinesheath wrote: Generally, calling virtual methods from inside a constructor is a potential risk and not recommended. Some languages can deal with it, some can't, at least not always (an overriden method might access a field of the subclass while the subclass hasn't been constructed/initialized yet). And if they can deal with it, it usually is still prone to errors or not working as you'd expect. And if I remember correctly, methods in Java are virtual by default, so you have to be careful with just about any method call in a constructor. Okay. I'm convinced. I convinced myself with the following (silly) code ![]() + Show Spoiler [breaks with setters in constructor] +
| ||
obesechicken13
United States10467 Posts
The controller is where I map all the rest endpoints. The service is where the logic gets done and the stuff is put into a db. I kinda want to know what the response times of the controller methods are because that's what users use. It's possible for multiple controller methods to call one service and I want the granularity to see which one was called. But at the same time it's possible for 2 service methods to be mapped to 1 controller. :/ | ||
phar
United States1080 Posts
Actually I'd advocate for more monitoring as well - get some client side stuff in your js (e.g. google analytics or something like that). | ||
Manit0u
Poland17187 Posts
It's also nice because you can then move your logic away from controllers - they only do basic stuff and dispatch events and all the heavy lifting is done by services triggered by event listeners. This gives you nice flexibility since you no longer call the services directly. With this approach you also get nice separation between the MVC layers. | ||
obesechicken13
United States10467 Posts
| ||
Manit0u
Poland17187 Posts
I'm trying to parse a csv file. The problem is that some fields in it are really big ints like 1870000000011550. When such line is read and cast to int it obviously gets truncated (and I can't have that). One possible solution here would be changing all such fields to be explicit strings "1870000000011550". When I try to change their type from number to string in office it changes them to stuff like 1,87000000000570E+015. Does anyone know of a good way to use some editor (vim, notepad++ whatever) to pre/append the quotes to all numbers in a file that's over 20k lines long? ![]() | ||
Ropid
Germany3557 Posts
| ||
Manit0u
Poland17187 Posts
Search: ([0-9]{16}) Replace: "\1" Phew! Edit: Scratch that. Both office and file reading functions in PHP still treat those fields as numbers instead of text ![]() | ||
![]()
tofucake
Hyrule18969 Posts
| ||
Ropid
Germany3557 Posts
![]() I searched around with regards to Office. When working inside Excel, the trick you'd use to make it treat a bunch of digits as a string instead of a number is putting a ' in front of it. Instead of 123 you'd type '123 into the cell. Perhaps that trick also works when you make Excel import CSV data? I also tried this '123 with LibreOffice and it's the same behavior there. If this is some sort of standard, perhaps the CSV reading thingy you use in your PHP code also behaves similarly? | ||
BByte
Finland49 Posts
What are you using to read the file in PHP? Both fgetcsv and str_getcsv should return an array of strings whether you have quotes around numbers or not. | ||
| ||