|
SageTV Studio Discussion related to the SageTV Studio application produced by SageTV. Questions, issues, problems, suggestions, etc. relating to the Studio software application should be posted here. |
|
Thread Tools | Search this Thread | Display Modes |
#1
|
|||
|
|||
Easy way to dynamically build classes/objects
So wondering if there is an easier way to dynamically define and build custom class's which are for javaobjects.
For instance say I have Code:
class BookObject{ private string Bookname; private int BookLocation; public String GetBookname(){ return Bookname;} public void SetBookname(String Value){ this.Bookname=Value} public Integer GetBookLocation(){ return BookLocation;} public void setBookLocation(int Value){ this.BookLocation=Value;} } maybe not.... cheers and TIA pluckyhd |
#2
|
||||
|
||||
In what sense would this be dynamic? Presumably you're not planning to generate new classes and methods at runtime. You're going to code them once and that's it. So what you're looking for (if I understand right) is a way to save some typing when coding up new classes.
Seems to me the simplest solution is to make those fields public instead of private, so clients of your class can access them directly instead of through getter/setter methods. Or is there some other reason you need explicit getter/setter methods?
__________________
-- Greg |
#3
|
|||
|
|||
Quote:
Quote:
No other reason than that is how I learned how to do it...Is there a way to set those variables without set methods? |
#4
|
||||
|
||||
There's really no reason to use Get and Sets if all they really do is manipulate the internal variable without any other checks/functions. It's easier to just make the variable itself public. Just make it as so:
Code:
class BookObject{ public string Bookname; public int BookLocation; }
__________________
Buy Fuzzy a beer! (Fuzzy likes beer) unRAID Server: i7-6700, 32GB RAM, Dual 128GB SSD cache and 13TB pool, with SageTVv9, openDCT, Logitech Media Server and Plex Media Server each in Dockers. Sources: HRHR Prime with Charter CableCard. HDHR-US for OTA. Primary Client: HD-300 through XBoxOne in Living Room, Samsung HLT-6189S Other Clients: Mi Box in Master Bedroom, HD-200 in kids room |
#5
|
||||
|
||||
Pluckly, most IDEs will do this for your.
Just create, Code:
public class class BookObject { private string Bookname; private int BookLocation; } Later when you add Code:
private long price; If you use netbeans, then it has the same tools, but may be called something slightly different. Keep in mind, that java ides (like most ides) will generate tons of boilerplate code. nobody write that stuff anymore.
__________________
Batch Metadata Tools (User Guides) - SageTV App (Android) - SageTV Plex Channel - My Other Android Apps - sagex-api wrappers - Google+ - Phoenix Renamer Downloads SageTV V9 | Android MiniClient |
#6
|
||||
|
||||
object.field = value;
(Assuming the field is public of course.)
__________________
-- Greg |
#7
|
|||
|
|||
Please do not use public properties for your class, especially if the class is to be part of a public API that you plan to release. Call me "old school", but simply making your properties public will undoubtedly cost you down the road.
Code:
public class Book { public String title; public String content; // Assume content is just one big string for simplicity public int numPages; public int location; public Book(String title, String content, int numPages, int location) { this.title = title; this.content = content; this.numPages = numPages; this.location = location; } } Code:
Book b = new Book("My First Novel", "Blah blah blah", -1, 200); Code:
Book b = new Book("My Novel", "Blah, blah, blah", 5, 200); // Valid object instantiation b.numPages = -1; // Completely valid since numPages is public, but now numPages is negative, which will cause that runtime error to resurface Code:
public class Book { private String title; private String content; // Assume content is just one big string for simplicity private int numPages; private int location; public Book(String title, String content, int numPages, int location) { if(numPages < 0) throw new IllegalArgumentException("Number of pages cannot be negative!"); this.title = title; this.content = content; this.numPages = numPages; this.location = location; } public int getNumPages() { return numPages; } public void setNumPages(int num) { if(num < 0) throw new IllegalArgumentException("Number of pages cannot be negative!"); numPages = num; } } My CS201 Prof would be so proud of me right now. And this also proves, despite his claim, that one can learn something even while napping during those 8am lectures!
__________________
Twitter: @ddb_db Server: Intel i5-4570 Quad Core, 16GB RAM, 1 x 128GB OS SSD (Win7 Pro x64 SP1), 1 x 2TB media drive Capture: 2 x Colossus STB Controller: 1 x USB-UIRT Software:Java 1.7.0_71; SageTV 7.1.9 Clients: 1 x HD300, 2 x HD200, 1 x SageClient, 1 x PlaceShifter Plugins: Too many to list now... Last edited by Slugger; 02-23-2010 at 10:26 PM. |
#8
|
||||
|
||||
Quote:
Point taken, but to my mind this is a deficiency in Java, for which the workaround is to pre-emptively use getter/setter methods and force consumers to use method call syntax instead of the more natural assignment syntax. Other languages (such as VB and C#) handle this situation better by allowing consumers to be agnostic about whether they're accessing a property directly or through getter/setter methods and use assignment syntax in either case. This gives the class designer the freedom to change the implementation without breaking existing consumers. So while obsessive use of getters/setters may be appropriate for Java, it's not a habit one should carry over to other languages that don't require it.
__________________
-- Greg |
#9
|
||||
|
||||
I agree with Slugger here... do start out by exposing properties, it's just wrong, for an object.
Now, if you are truly making a structure (ie, what's we'd call a struct in c), then I could see, perhaps, not creating the setters and getters. There are cases where i've done this for inner classes that are being uses only to hold data, ie, there are no operations in the structs. My general rule of thumb here, is the struct instances are never exposed beyond my class, and that the structs do not contain methods. So, if you objects are purely structs, then I could see someone just using properties. The problem is your objects are going to start out as structs, and then you are going to add in constructors with parameters and method that calculate values, and now you have this crud of an object/struct that doesn't make sense to anyone. Greg's point above is valid, ie, he way that c# defines setters and getters is better. But given that an ide will generate setters and getters for you in a couple seconds... is there really any reason to choose poor coding conventions, when doing it right, is just so darn easy
__________________
Batch Metadata Tools (User Guides) - SageTV App (Android) - SageTV Plex Channel - My Other Android Apps - sagex-api wrappers - Google+ - Phoenix Renamer Downloads SageTV V9 | Android MiniClient |
#10
|
||||
|
||||
Quote:
__________________
Batch Metadata Tools (User Guides) - SageTV App (Android) - SageTV Plex Channel - My Other Android Apps - sagex-api wrappers - Google+ - Phoenix Renamer Downloads SageTV V9 | Android MiniClient |
#11
|
|||
|
|||
Did not know about the auto set/getter coding do you know how much time that would have saved me 3 months ago if I would have asked I prefer to leave them private and my reasons are different from above and I didn't mean to start an argument. The method I read and use it to make all of the private in the object class and have another class for calling the methods it is allot of coding but it is easier for me to keep track of that way. thanks again and I can't believe I didn't know about the automatic set/get methods of the ide.... |
#12
|
||||
|
||||
@Plucky - don't feel bad. I've pointed out features of eclipse to people that have been using for longer than I have Keep in mind, then only reason to use an IDE is increase productivity over a regular power editor like vim. I spent years in vim, so when I moved to eclipse, I needed to prove that it would actually increase my productivity. While initially, learning every aspect of the IDE decreased my productivity, doing so has much improved my overall productivity, and then I stopped using vim (i still miss vim )
Bottom line keep posting your questions... whenever you think that "there's got to be an easier way..." there probably is... because we also hate typing
__________________
Batch Metadata Tools (User Guides) - SageTV App (Android) - SageTV Plex Channel - My Other Android Apps - sagex-api wrappers - Google+ - Phoenix Renamer Downloads SageTV V9 | Android MiniClient |
#13
|
|||
|
|||
Quote:
Thanks, Jeff |
#14
|
||||
|
||||
Wow.. this discussion really has pointed out how happy i am that I use .net languages.. ;-) I make a custom class for just about every object I use, and the agnostic nature of properties and variables are so much more evolved than the older java and c methods. I hadn't realized the difference when I recommended just majing the variables themselves public earlier. If you can't use them in the same way as get/set methods in subsequent code, then, yes, it does only make sense to ONLY use get and set methods.
In .NET, however, if you are not going to be doing any checking of a value before setting it, or not performing any action when get or set, then there really is no harm in making the variables themselves public. If at a later time, you decide there are some checks and what-not that you wish to perform, you can easily create a private variable to take it's place, and make a public property (the .net name for getters and setters) with the same name... makes it a transparent change to the users of the class.
__________________
Buy Fuzzy a beer! (Fuzzy likes beer) unRAID Server: i7-6700, 32GB RAM, Dual 128GB SSD cache and 13TB pool, with SageTVv9, openDCT, Logitech Media Server and Plex Media Server each in Dockers. Sources: HRHR Prime with Charter CableCard. HDHR-US for OTA. Primary Client: HD-300 through XBoxOne in Living Room, Samsung HLT-6189S Other Clients: Mi Box in Master Bedroom, HD-200 in kids room |
#15
|
|||
|
|||
Hopefully no one else is seeing this as an argument, but rather a valid discussion on coding practices. I hope my tone is not being read as anything other than that.
Quote:
Code:
// Not real var names, bare with me ;) MyObj *obj; MyObj *obj2; ... obj->clone(obj2);
__________________
Twitter: @ddb_db Server: Intel i5-4570 Quad Core, 16GB RAM, 1 x 128GB OS SSD (Win7 Pro x64 SP1), 1 x 2TB media drive Capture: 2 x Colossus STB Controller: 1 x USB-UIRT Software:Java 1.7.0_71; SageTV 7.1.9 Clients: 1 x HD300, 2 x HD200, 1 x SageClient, 1 x PlaceShifter Plugins: Too many to list now... |
#16
|
|||
|
|||
Quote:
Again, I guess I'm speaking more from my day job when I talk about this kind of thing and I'm sure if you go looking through my source code for all my SageTV plugins you'll find some direct contradictions to my advice (though I'd be shocked if you found a public property defined in any public or pkg protected class that I've written), but, imho, when it comes to Java there's just no reason not to create getters and setters for all class properties. If nothing else, it's preventative maintenance and will save a (possible) API breaking fix later on down the road.
__________________
Twitter: @ddb_db Server: Intel i5-4570 Quad Core, 16GB RAM, 1 x 128GB OS SSD (Win7 Pro x64 SP1), 1 x 2TB media drive Capture: 2 x Colossus STB Controller: 1 x USB-UIRT Software:Java 1.7.0_71; SageTV 7.1.9 Clients: 1 x HD300, 2 x HD200, 1 x SageClient, 1 x PlaceShifter Plugins: Too many to list now... |
#17
|
||||
|
||||
well, to be fair, there's a HUGE difference in the 'sugar' in C++ (which is very inconsistant, and almost 'tacked on' feeling) and .NET's much more natural way of doing things. Of course, a lot of the power of .net IS the visual studio IDE. Why have the code 'self-documenting' just through comments and method names, when you can have the applicable documentation popping up as you are coding?
__________________
Buy Fuzzy a beer! (Fuzzy likes beer) unRAID Server: i7-6700, 32GB RAM, Dual 128GB SSD cache and 13TB pool, with SageTVv9, openDCT, Logitech Media Server and Plex Media Server each in Dockers. Sources: HRHR Prime with Charter CableCard. HDHR-US for OTA. Primary Client: HD-300 through XBoxOne in Living Room, Samsung HLT-6189S Other Clients: Mi Box in Master Bedroom, HD-200 in kids room |
#18
|
||||
|
||||
Quote:
I think "natural" is a term that people use to describe the way that they currently do things
__________________
Batch Metadata Tools (User Guides) - SageTV App (Android) - SageTV Plex Channel - My Other Android Apps - sagex-api wrappers - Google+ - Phoenix Renamer Downloads SageTV V9 | Android MiniClient |
#19
|
||||
|
||||
I was in no way stating that eclipse can't do this, just that THIS is how code should be documented, not just with Method names. I actually think Stuff = GetThis and SetThat(stuff) are an extra level of unnecessary complexity... I much prefer Stuff = This and That = Stuff... Much easier to read through the code. Never liked that Java forced you to use two completly different styles for variables and functions..
__________________
Buy Fuzzy a beer! (Fuzzy likes beer) unRAID Server: i7-6700, 32GB RAM, Dual 128GB SSD cache and 13TB pool, with SageTVv9, openDCT, Logitech Media Server and Plex Media Server each in Dockers. Sources: HRHR Prime with Charter CableCard. HDHR-US for OTA. Primary Client: HD-300 through XBoxOne in Living Room, Samsung HLT-6189S Other Clients: Mi Box in Master Bedroom, HD-200 in kids room |
#20
|
||||
|
||||
Quote:
__________________
Clients: 1xHD200 Connected to 50" TH-50PZ750U Plasma Server : Shuttle SFF SSH55J2 w/ Win7 Home, SageTV v7, Core i3 540, 2GB RAM, 30GB SSD for OS, 1.5TB+2x1TB WDGP for Recordings, BluRay, 2xHDHR, 1xFirewire SageTV : PlayOn, SJQ, MediaShrink, Comskip, Jetty, Web Client, BMT Having a problem? Don't forget to include a log! (Instructions for: PlayOn For SageTV v1.5, MediaShrink) |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Any experience in Sun online classes? | PLUCKYHD | SageTV Studio | 5 | 03-23-2010 03:20 AM |
Moving objects leaving trails(after images) | xxilikedirtxx | Hardware Support | 1 | 02-20-2006 05:58 PM |
Dynamically loading/unloading STV | MadAxeMan | SageTV Studio | 0 | 01-26-2006 05:05 AM |
Moving objects flashing - interlace issue? | lbeagley79 | Hardware Support | 15 | 10-14-2005 08:34 AM |
Dynamically Select Live TV Capture Device | kkoz | SageTV Software | 0 | 07-14-2005 11:27 AM |