![]() |
|
[Java] Methods / Class - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [Java] Methods / Class (/showthread.php?tid=5759011) |
Methods / Class - CompSec - Nov 27, 2017 Can anyone help me understand Methods and Classes in the most simple way? public class Car { private double mileage; public String manufacturer = ""; private double petrol; public Car(String manufacturer) { this.manufacturer = manufacturer; mileage = 0; petrol = 0; } What is the purpose in the second part of this? Why not just declare your String in the main method? RE: Methods / Class - Aussie - Nov 27, 2017 What you have there is a class called Car and a CONSTRUCTOR Traditionally speaking, it is good software design to not have main class setting values of other types. Code #shit pseudocodethe reason why we have constructors is so we can make classes and effectively give them attributes (e.g. manufacturer, brand... etc), that pertain to the OBJECT type you are modelling (i.e. cars). This might seem foreign or counterintuitive to you initially, but when you develop a game or a bigger project it'll be consolidated, THIS is very important. Imagine instantiating an enemy like... in mario e.g. Enemy enemy = new Enemy(Type.GOOMBA, "bobby"); //declaring an enemy type goomba, his name bobby (irrelevant), rather than doing enemy.type = TYPE.GOOMBA; enemy.name = "bobby" which is inefficient, also remember getters and setters for better encapsulation RE: Methods / Class - CompSec - Nov 27, 2017 (Nov 27, 2017 06:38 AM)n31ln3t Wrote: What you have there is a class called Car and a CONSTRUCTORAhh that definitely helps. Thank you 😉 RE: Methods / Class - Ciphered - Nov 27, 2017 PHP Code PHP Code This is how you would use this class in your public static void main(String[] args) method. PHP Code The point of coding like this is because if we wanted to have multiple cars, it makes it easy to keep track of the different makes and speeds. PHP Code RE: Methods / Class - king mala - Nov 29, 2017 One of the best explanations of classes I ever received was this: Assume you have a general item like a bike. Using the class Bike, we can set some specifics to create different types of bikes. For example, we may want this bike to have many spokes, or perhaps we want a bike to have a larger seat. This would be an iteration of that case: public class Bicycle { private int numSpokes; private String bikeName; private int numWheels; public Bicycle (int spokes, String name, int numWheels) { numSpokes = spokes; bikeName = name; this.numWheels = numWheels; } //Accessor Methods public int getSpokes() { return numSpokes; } public String getName() { return bikeName; } public int getNumWheels() { return numWheels; } } Now below, suppose we have our main class that we want to create instances of this bike class within: public class BikeMaker { public static void main(String[] args) { Bicycle bike1 = new Bicycle(10, "Tricycle", 3); Bicycle bike2 = new Bicycle(3, "Motorbike", 2); System.out.println("Bike 1 is a " + bike1.getName() + " with " + bike1.getNumWheels() + " wheels and " + bike1.getSpokes() + " spokes."); System.out.println("Bike 2 is a " + bike2.getName() + " with " + bike2.getNumWheels() + " wheels and " + bike2.getSpokes() + " spokes."); } } This makes our main program much easier to read and simplifies the main method. You can imagine in a much more complicated program, you'll have 10s or even hundreds of classes working together, and if we were to force those all into one class, we'd have a very long and very complex program to follow should there be any bugs to fix or things to update. RE: Methods / Class - CompSec - Dec 1, 2017 (Nov 29, 2017 09:46 PM)king mala Wrote: One of the best explanations of classes I ever received was this: Ahh wow, that is a great way of simplifying it for me. Thanks a lot 😉 RE: Methods / Class - matt - Dec 1, 2017 (Dec 1, 2017 08:29 AM)CompSec Wrote: Ahh wow, that is a great way of simplifying it for me. Thanks a lot 😉 That was the absolute worst way of describing it wat RE: Methods / Class - CompSec - Dec 1, 2017 (Dec 1, 2017 08:34 AM)Midnight Blue™ Wrote: That was the absolute worst way of describing it Haha 🎉 I must be backwards, it actually helped |