问题
I just watched a youtube tutorial called "many methods and instances". He made a program in which you enter something and it says "your first gf was _". But it was way too overcomplicated. First is the main class:
import java.util.Scanner;
public class MethodsInstances2 {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
MethodsInstances object = new MethodsInstances();
System.out.println("Enter name of first gf here: ");
String temp = input.nextLine();
object.setName(temp);
object.saying();
}
}
Next is the class it makes an object from:
public class MethodsInstances {
private String girlName;
public void setName (String name){
girlName=name;
}
public String getName (){
return girlName;
}
public void saying(){
System.out.printf("Your first gf was %s", getName());
}
}
It seemed WAY too overcomplicated, and the title is all of the stuff i didn't understand, considering I'm still a newbie at Java. Here's what I typed which took 4 times faster:
import java.util.Scanner;
public class programtest {
public static void main(String args[]){
Scanner test = new Scanner(System.in);
String name;
System.out.println("Enter the name of your first girfriend: ");
name = test.next();
System.out.println("Your first girlfriend was " + name);
}
}
Can someone tell me the point of doing what the tut said to do, and what the title words mean?
Thanks a lot, Dan
回答1:
The tutorial is named "many methods and instances" and from my POV shows very simply how to create a class, instantiate it and call its methods. It would have been called "get some console input and spit it back out" if it was meant to do things the easiest way possible.
The point of the video tutorial was not to create an overly complicated program but rather to demonstrate how to call methods on objects.
You have a class called MethodInstances (not a great example name, btw) which you instantiate and then call methods on it to save state (gf name) and the get it back and display it. (I would suggest refactoring the example to have it make more sense. The verbiage as it is causes confusion.)
Try this on:
import java.util.Scanner;
public class ObjectInstanceMethodExample {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
FirstGirlfriend firstGF = new FirstGirlfriend();
System.out.println("Enter name of first gf here: ");
String temp = input.nextLine();
firstGF.setName(temp);
firstGF.sayFirstGirlfriendName();
}
}
class FirstGirlfriend {
private String girlName;
public void setName (String name){
girlName=name;
}
public String getName (){
return girlName;
}
public void sayFirstGirlfriendName(){
System.out.printf("Your first gf was %s", getName());
}
}
You short circuited the process by avoiding creating a separate class and using a local variable instead. It works, it's easier, but that's not what the tutorial was trying to teach.
I suggest finding even simpler examples for starters before getting into OOP stuff or... embracing OOP and running with it.
Btw, you will not progress as a programmer by always doing things the easiest way. Easiest only seems easiest in the near term. Any less-than-trivial project with even a bit of complexity will quickly become untenable by doing it the easiest way.
回答2:
It looks like a tutorial to demonstrate getter and setter methods in a class, i.e., data hiding. You add something to the object using the setter method, then retrieve it using the getter method.
And yes, this is overly complicated. But the technique is very useful for larger, more real-world kinds of problems.
回答3:
For starters your code uses test.next(); in place of the original input.nextLine();
printf is one of the oldest functions known to programmers and is about 30 years old!
It uses a well-known, universal style of formatting parameters to 'prettify' your text output and insert different types into a string.
回答4:
The second class contains only a String called "girlName". Because this Field has private access it can´t be accessed from outside its own class (see data hiding).
So instead you use so called getter and setter methodes which can be accessed from outside - to set the String value or to receive it.
You write instead of:
MethodsInstances.girlName = "myGirlsName";
MethodsInstances.setName("myGirlName");
来源:https://stackoverflow.com/questions/6851749/printf-saying-getname-setname